<commit_before>"use strict";

ficsClient.factory("Proxy", function($rootScope) {
  var socket = new SockJS("/socket");
  var socketOpen = false;

  var queue = [];
  var messageHandlers = {};

  socket.onopen = function() {
    socketOpen = true;

    while (queue.length > 0) {
      queue.shift()();
    }
  };

  socket.onclose = function() {
    socketOpen = false;
  };

  socket.onmessage = function(e) {
    if (e.type !== "message") {
      return;
    }

    var message = JSON.parse(e.data);

    $rootScope.$applyAsync(function() {
      messageHandlers[message.operation](message.data);
    });
  }

  return {
    registerMessage: function(operation, callback) {
      messageHandlers[operation] = callback;
    },

    sendMessage: function(operation, params) {
      ensureSocket(function() {
        socket.send(JSON.stringify({ operation: operation, params: (params || {}) }));
      });
    }
  };

  function ensureSocket(callback) {
    if (socketOpen) {
      callback();
    } else {
      queue.push(callback);
    }
  }
});
<commit_msg>factor websocket connection into function<commit_after>"use strict";

ficsClient.factory("Proxy", function($rootScope) {
  var socket;
  var socketOpen = false;

  var queue = [];
  var messageHandlers = {};

  connect();

  return {
    registerMessage: function(operation, callback) {
      messageHandlers[operation] = callback;
    },

    sendMessage: function(operation, params) {
      ensureSocket(function() {
        socket.send(JSON.stringify({ operation: operation, params: (params || {}) }));
      });
    }
  };

  function connect() {
    socket = new SockJS("/socket");

    socket.onopen = function() {
      socketOpen = true;

      while (queue.length > 0) {
        queue.shift()();
      }
    };

    socket.onclose = function() {
      socketOpen = false;
    };

    socket.onmessage = function(e) {
      if (e.type !== "message") {
        return;
      }

      var message = JSON.parse(e.data);

      $rootScope.$applyAsync(function() {
        messageHandlers[message.operation](message.data);
      });
    }
  }

  function ensureSocket(callback) {
    if (socketOpen) {
      callback();
    } else {
      queue.push(callback);
    }
  }
});

{'fraction_non_alphanumeric': 0.15281501340482573, 'fraction_numerical': 0.0008936550491510277, 'mean_word_length': 2.605475040257649, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2208389', 'n_tokens_mistral': 689, 'n_tokens_neox': 668, 'n_words': 128}