<commit_before>import deepMerge from 'deepmerge';
import deepFreeze from 'deep-freeze';

const DEFAULT_STATE = {
  masterSpinnerFinished: false,
};

const Store = class Store {
  constructor(initialState) {
    this.state = deepFreeze(deepMerge(DEFAULT_STATE, initialState));
    this.subscriptions = [];
    this.masterSpinnerSubscriptions = {};
    this.setStoreState = this.setStoreState.bind(this);
    this.getStoreState = this.getStoreState.bind(this);
    this.subscribe = this.subscribe.bind(this);
    this.unsubscribe = this.unsubscribe.bind(this);
    this.updateSubscribers = this.updateSubscribers.bind(this);
    this.subscribeMasterSpinner = this.subscribeMasterSpinner.bind(this);
    this.unsubscribeMasterSpinner = this.unsubscribeMasterSpinner.bind(this);
    this.unsubscribeAllMasterSpinner = this.unsubscribeAllMasterSpinner.bind(this);
    this.masterSpinnerSuccess = this.masterSpinnerSuccess.bind(this);
    this.masterSpinnerError = this.masterSpinnerError.bind(this);
  }

  setStoreState(newState, cb) {
    this.state = deepFreeze(deepMerge(this.state, newState));
    this.updateSubscribers(cb);
  }

  getStoreState() {
    return deepMerge({}, this.state);
  }

  subscribe(func) {
    this.subscriptions.push(func);
  }

  unsubscribe(func) {
    const index = this.subscriptions.indexOf(func);
    if (index !== -1) this.subscriptions.splice(index, 1);
  }

  updateSubscribers(cb) {
    this.subscriptions.forEach(func => func());
    if (typeof cb === 'function') cb(this.getStoreState());
  }

  subscribeMasterSpinner(src) {
    const index = Object.keys(this.masterSpinnerSubscriptions).indexOf(src);
    if (index === -1) {
      this.masterSpinnerSubscriptions[src] = {
        success: false,
        error: false,
        complete: false,
      };
    }
  }

  unsubscribeMasterSpinner(src) {
    const index = Object.keys(this.masterSpinnerSubscriptions).indexOf(src);
    if (index === -1) {
      return false;
    }
    this.setMasterSpinnerFinished();
    return delete this.masterSpinnerSubscriptions[src];
  }

  unsubscribeAllMasterSpinner() {
    this.masterSpinnerSubscriptions = {};
    this.setMasterSpinnerFinished();
  }

  masterSpinnerSuccess(src) {
    this.masterSpinnerSubscriptions[src].success = true;
    this.masterSpinnerSubscriptions[src].complete = true;
    this.setMasterSpinnerFinished();
  }

  masterSpinnerError(src) {
    this.masterSpinnerSubscriptions[src].error = true;
    this.masterSpinnerSubscriptions[src].complete = true;
    this.setMasterSpinnerFinished();
  }

  setMasterSpinnerFinished() {
    this.setStoreState({
      masterSpinnerFinished: this.isMasterSpinnerFinished(),
    });
  }

  isMasterSpinnerFinished() {
    // console.log('MASTER SPINNER SUBSCRIPTIONS', this.masterSpinnerSubscriptions);
    return !Object.keys(this.masterSpinnerSubscriptions).find(
      src => this.masterSpinnerSubscriptions[src].complete !== true,
    );
  }
};

export default Store;
<commit_msg>fix: Remove Array.find from Store.jsx (#306)<commit_after>import deepMerge from 'deepmerge';
import deepFreeze from 'deep-freeze';

const DEFAULT_STATE = {
  masterSpinnerFinished: false,
};

const Store = class Store {
  constructor(initialState) {
    this.state = deepFreeze(deepMerge(DEFAULT_STATE, initialState));
    this.subscriptions = [];
    this.masterSpinnerSubscriptions = {};
    this.setStoreState = this.setStoreState.bind(this);
    this.getStoreState = this.getStoreState.bind(this);
    this.subscribe = this.subscribe.bind(this);
    this.unsubscribe = this.unsubscribe.bind(this);
    this.updateSubscribers = this.updateSubscribers.bind(this);
    this.subscribeMasterSpinner = this.subscribeMasterSpinner.bind(this);
    this.unsubscribeMasterSpinner = this.unsubscribeMasterSpinner.bind(this);
    this.unsubscribeAllMasterSpinner = this.unsubscribeAllMasterSpinner.bind(this);
    this.masterSpinnerSuccess = this.masterSpinnerSuccess.bind(this);
    this.masterSpinnerError = this.masterSpinnerError.bind(this);
  }

  setStoreState(newState, cb) {
    this.state = deepFreeze(deepMerge(this.state, newState));
    this.updateSubscribers(cb);
  }

  getStoreState() {
    return deepMerge({}, this.state);
  }

  subscribe(func) {
    this.subscriptions.push(func);
  }

  unsubscribe(func) {
    const index = this.subscriptions.indexOf(func);
    if (index !== -1) this.subscriptions.splice(index, 1);
  }

  updateSubscribers(cb) {
    this.subscriptions.forEach(func => func());
    if (typeof cb === 'function') cb(this.getStoreState());
  }

  subscribeMasterSpinner(src) {
    const index = Object.keys(this.masterSpinnerSubscriptions).indexOf(src);
    if (index === -1) {
      this.masterSpinnerSubscriptions[src] = {
        success: false,
        error: false,
        complete: false,
      };
    }
  }

  unsubscribeMasterSpinner(src) {
    const index = Object.keys(this.masterSpinnerSubscriptions).indexOf(src);
    if (index === -1) {
      return false;
    }
    this.setMasterSpinnerFinished();
    return delete this.masterSpinnerSubscriptions[src];
  }

  unsubscribeAllMasterSpinner() {
    this.masterSpinnerSubscriptions = {};
    this.setMasterSpinnerFinished();
  }

  masterSpinnerSuccess(src) {
    this.masterSpinnerSubscriptions[src].success = true;
    this.masterSpinnerSubscriptions[src].complete = true;
    this.setMasterSpinnerFinished();
  }

  masterSpinnerError(src) {
    this.masterSpinnerSubscriptions[src].error = true;
    this.masterSpinnerSubscriptions[src].complete = true;
    this.setMasterSpinnerFinished();
  }

  setMasterSpinnerFinished() {
    this.setStoreState({
      masterSpinnerFinished: this.isMasterSpinnerFinished(),
    });
  }

  isMasterSpinnerFinished() {
    return Object.keys(this.masterSpinnerSubscriptions).filter(
      src => this.masterSpinnerSubscriptions[src].complete !== true,
    ).length === 0;
  }
};

export default Store;

{'fraction_non_alphanumeric': 0.12301184433164128, 'fraction_numerical': 0.0020304568527918783, 'mean_word_length': 4.131076388888889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6676327', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1810, 'n_words': 263}