<commit_before>var ResultCollector = require('./result_collector');
var work = require('webworkify');

var workers = [];

Object.defineProperty(module.exports, 'length', {
  get: function(){
    return workers.length;
  }
});

module.exports.init = function(workersCount){
  while (workersCount--) {
    var worker = work(require('./worker.js'));
    workers.push(worker);
  }
};

module.exports.terminate = function(){
  workers.forEach(function(w){
    w.terminate();
  });

  workers = [];
};

module.exports.sendPacks = function(packs, callback){
  var collector = new ResultCollector(workers.length, callback);

  packs.forEach(function(pack, index){
    var onMessageHandler = function (event){
      event.target.removeEventListener('error', onErrorHandler);
      event.target.removeEventListener('message', onMessageHandler);
      return collector.onPart(event.data);
    };

    var onErrorHandler = function (event){
      event.preventDefault();
      event.target.removeEventListener('error', onErrorHandler);
      event.target.removeEventListener('message', onMessageHandler);
      return collector.onError(event.message);
    };

    workers[index].addEventListener('error', onErrorHandler);
    workers[index].addEventListener('message', onMessageHandler);

    workers[index].postMessage(pack);
  });
};<commit_msg>Fix for typed arrays not being transferred<commit_after>var ResultCollector = require('./result_collector');
var work = require('webworkify');

var workers = [];

Object.defineProperty(module.exports, 'length', {
  get: function(){
    return workers.length;
  }
});

module.exports.init = function(workersCount){
  while (workersCount--) {
    var worker = work(require('./worker.js'));
    workers.push(worker);
  }
};

module.exports.terminate = function(){
  workers.forEach(function(w){
    w.terminate();
  });

  workers = [];
};

module.exports.sendPacks = function(packs, callback){
  var collector = new ResultCollector(workers.length, callback);

  packs.forEach(function(pack, index){
    var onMessageHandler = function (event){
      event.target.removeEventListener('error', onErrorHandler);
      event.target.removeEventListener('message', onMessageHandler);
      return collector.onPart(event.data);
    };

    var onErrorHandler = function (event){
      event.preventDefault();
      event.target.removeEventListener('error', onErrorHandler);
      event.target.removeEventListener('message', onMessageHandler);
      return collector.onError(event.message);
    };

    workers[index].addEventListener('error', onErrorHandler);
    workers[index].addEventListener('message', onMessageHandler);

    if (pack.buffer){
      workers[index].postMessage(pack, [ pack.buffer ]);
    } else {
      workers[index].postMessage(pack);
    }
  });
};
{'fraction_non_alphanumeric': 0.1475526973919257, 'fraction_numerical': 0.0, 'mean_word_length': 4.384615384615385, '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': '1595178', 'n_tokens_mistral': 780, 'n_tokens_neox': 767, 'n_words': 137}