<commit_before>//
// The OpenDiamond Platform for Interactive Search
//
// Copyright (c) 2012 Carnegie Mellon University
// All rights reserved.
//
// This software is distributed under the terms of the Eclipse Public
// License, Version 1.0 which can be found in the file named LICENSE.
// ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
// RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
//

function JSONBlasterSocket(url, search_key) {
  // Make sure "new" was used
  if (!(this instanceof arguments.callee)) {
    throw new Error('Constructor called as a function');
  }

  // Private vars
  var blaster = this;
  var sock = new SockJS(url);
  var handlers = {};
  var paused = false;

  // Private methods
  function run_handler(name) {
    if (typeof(handlers[name]) === 'function') {
      var handler_args = Array.prototype.slice.call(arguments, 1)
      handlers[name].apply(blaster, handler_args);
    }
  }

  // Public methods
  this.on = function(event, func) {
    handlers[event] = func;
  };

  this.onopen = function(func) {
    blaster.on('__open', func);
  };

  this.onclose = function(func) {
    blaster.on('__close', func);
  };

  this.emit = function(event, data)  {
    sock.send(JSON.stringify({
      'event': event,
      'data': data
    }));
  };

  this.pause = function() {
    if (!paused) {
      paused = true;
      blaster.emit('pause');
    }
  };

  this.resume = function() {
    if (paused) {
      paused = false;
      blaster.emit('resume');
    }
  };

  // SockJS callbacks
  sock.onopen = function() {
    blaster.emit('start', {
      'search_key': search_key
    });
    run_handler('__open');
  };

  sock.onmessage = function(ev) {
    var msg = JSON.parse(ev.data);
    run_handler(msg.event, msg.data);
  };

  sock.onclose = function() {
    run_handler('__close');
  };

  // Default event handlers
  blaster.on('ping', function(data) {
    blaster.emit('pong');
  });

  blaster.on('error', function(data) {
    alert(data.message);
  });
}<commit_msg>json-blaster.js: Properly handle pause/resume when not connected<commit_after>//
// The OpenDiamond Platform for Interactive Search
//
// Copyright (c) 2012 Carnegie Mellon University
// All rights reserved.
//
// This software is distributed under the terms of the Eclipse Public
// License, Version 1.0 which can be found in the file named LICENSE.
// ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
// RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
//

function JSONBlasterSocket(url, search_key) {
  // Make sure "new" was used
  if (!(this instanceof arguments.callee)) {
    throw new Error('Constructor called as a function');
  }

  // Private vars
  var blaster = this;
  var sock = new SockJS(url);
  var handlers = {};
  var connected = false;
  var paused = false;

  // Private methods
  function run_handler(name) {
    if (typeof(handlers[name]) === 'function') {
      var handler_args = Array.prototype.slice.call(arguments, 1)
      handlers[name].apply(blaster, handler_args);
    }
  }

  // Public methods
  this.on = function(event, func) {
    handlers[event] = func;
  };

  this.onopen = function(func) {
    blaster.on('__open', func);
  };

  this.onclose = function(func) {
    blaster.on('__close', func);
  };

  this.emit = function(event, data)  {
    sock.send(JSON.stringify({
      'event': event,
      'data': data
    }));
  };

  this.pause = function() {
    if (connected && !paused) {
      blaster.emit('pause');
    }
    paused = true;
  };

  this.resume = function() {
    if (connected && paused) {
      blaster.emit('resume');
    }
    paused = false;
  };

  // SockJS callbacks
  sock.onopen = function() {
    connected = true;
    blaster.emit('start', {
      'search_key': search_key
    });
    if (paused) {
      blaster.emit('pause');
    }
    run_handler('__open');
  };

  sock.onmessage = function(ev) {
    var msg = JSON.parse(ev.data);
    run_handler(msg.event, msg.data);
  };

  sock.onclose = function() {
    connected = false;
    run_handler('__close');
  };

  // Default event handlers
  blaster.on('ping', function(data) {
    blaster.emit('pong');
  });

  blaster.on('error', function(data) {
    alert(data.message);
  });
}
{'fraction_non_alphanumeric': 0.14622641509433962, 'fraction_numerical': 0.0033018867924528303, 'mean_word_length': 2.876599634369287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '516149', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1373, 'n_words': 344}