<commit_before>import _ from 'lodash-fp';

export default (() => {
  return {
    initialize,
    spawn
  };
})();

/**
  * Spawn a dedicated worker.
  * @param  {string} script - worker script.
  * @return {object} worker wrapper  -  worker wrapper object.
  */
function spawn(script) {
  let w = new Worker(script);
  return {
    subscribe(callback) {
      w.onmessage = callback;
    }
  };
}

/**
 * Check worker support
 * @return {Bool} suppport
 */
function initialize() {
  return _.isFunction(Worker);
}<commit_msg>refactor core spawn funtion using rxjs observable<commit_after>import _ from 'lodash-fp';
import Rx from 'rxjs';

export default (() => {
  return {
    initialize,
    spawn
  };
})();

/**
 * Check worker support
 * @return {Bool} suppport
 */
function initialize() {
  return _.isFunction(window.Worker);
}

/**
  * Spawn a dedicated worker.
  * @param  {string} script - worker script.
  * @return {object} worker wrapper  -  worker wrapper object.
  */
function spawn(script) {
  let w = new window.Worker(script);
  let observer = new Rx.Subject();
  w.onmessage = _Callback;
  let ctx = {
    map
  };
  return ctx;

  /**
   * Map over stream of worker event data.
   * @param  {function} callback .
   * @return {object} token - subscription token.
   */
  function map(callback) {
    return observer.subscribe(callback);
  }

  /**
   * Worker message event callback.
   * @param  {object} event .
   */
  function _Callback(event) {
    observer.next(event.data);
  }
}
{'fraction_non_alphanumeric': 0.1386861313868613, 'fraction_numerical': 0.0, 'mean_word_length': 2.88659793814433, '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': '1602061', 'n_tokens_mistral': 488, 'n_tokens_neox': 479, 'n_words': 134}