<commit_before>var _ = require('underscore');
var uuid = require('./utils/uuid');
var Instances = require('./instances');
var Dispatcher = require('./dispatcher');

class Context {
  constructor(container, req) {
    this.req = req;
    this.instances = {};
    this.id = uuid.type('Context');
    this.dispatcher = new Dispatcher();

    Instances.add(this);

    _.each((container || {}).types, (classes, type) => {
      var options = {
        context: this,
        dispatcher: this.dispatcher
      };

      this.instances[type] = {};

      _.each(classes, (clazz) => {
        this.instances[type][clazz.id] = container.resolve(
          type,
          clazz.id,
          options
        );
      });
    });
  }

  fetchData(cb) {
    var result;
    var fetchFinished;
    var instance = getInstance(this);

    instance.fetchCount = 0;
    instance.deferredFetchFinished = deferred();
    fetchFinished = instance.deferredFetchFinished.promise;

    try {
      result = cb.call(this);
      fetchFinished = fetchFinished.then(function () {
        return result;
      });
    } catch (e) {
      instance.deferredFetchFinished.reject(e);

      return fetchFinished;
    }

    if (instance.fetchCount === 0) {
      instance.deferredFetchFinished.resolve();
    }

    return fetchFinished;
  }

  fetchStarted() {
    getInstance(this).fetchCount++;
  }

  fetchFinished() {
    var instance = getInstance(this);
    instance.fetchCount--;
    if (instance.fetchCount === 0) {
      instance.deferredFetchFinished.resolve();
    }
  }

  dispose() {
    Instances.dispose(this);

    _.each(this.instances, (instances) => {
      _.each(instances, (instance) => {
        if (_.isFunction(instance.dispose)) {
          instance.dispose();
        }
      });
    });

    this.instances = null;
    this.dispatcher = null;
  }

  resolve(obj) {
    if (!obj.constructor) {
      throw new Error('Cannot resolve object');
    }

    var id = obj.constructor.id;
    var type = obj.constructor.type;

    if (!this.instances[type]) {
      throw new Error(`Context does not have any instances of ${type}`);
    }

    if (!this.instances[type][id]) {
      throw new Error(`Context does not have an instance of the ${type} id`);
    }

    return this.instances[type][id];
  }

  getAll(type) {
    return _.values(this.instances[type]);
  }

  getAllStores() {
    return this.getAll('Stores');
  }

  getAllStateSources() {
    return this.getAll('StateSources');
  }

  getAllActionCreators() {
    return this.getALl('ActionCreators');
  }
}

module.exports = Context;

function getInstance(context) {
  return Instances.get(context);
}

function deferred() {
  var result = {};
  result.promise = new Promise(function (resolve, reject) {
    result.resolve = resolve;
    result.reject = reject;
  });
  return result;
}<commit_msg>:bug: Getting plural of type in stores<commit_after>var _ = require('underscore');
var uuid = require('./utils/uuid');
var Instances = require('./instances');
var Dispatcher = require('./dispatcher');

class Context {
  constructor(container, req) {
    this.req = req;
    this.instances = {};
    this.id = uuid.type('Context');
    this.dispatcher = new Dispatcher();

    Instances.add(this);

    _.each((container || {}).types, (classes, type) => {
      var options = {
        context: this,
        dispatcher: this.dispatcher
      };

      this.instances[type] = {};

      _.each(classes, (clazz) => {
        this.instances[type][clazz.id] = container.resolve(
          type,
          clazz.id,
          options
        );
      });
    });
  }

  fetchData(cb) {
    var result;
    var fetchFinished;
    var instance = getInstance(this);

    instance.fetchCount = 0;
    instance.deferredFetchFinished = deferred();
    fetchFinished = instance.deferredFetchFinished.promise;

    try {
      result = cb.call(this);
      fetchFinished = fetchFinished.then(function () {
        return result;
      });
    } catch (e) {
      instance.deferredFetchFinished.reject(e);

      return fetchFinished;
    }

    if (instance.fetchCount === 0) {
      instance.deferredFetchFinished.resolve();
    }

    return fetchFinished;
  }

  fetchStarted() {
    getInstance(this).fetchCount++;
  }

  fetchFinished() {
    var instance = getInstance(this);
    instance.fetchCount--;
    if (instance.fetchCount === 0) {
      instance.deferredFetchFinished.resolve();
    }
  }

  dispose() {
    Instances.dispose(this);

    _.each(this.instances, (instances) => {
      _.each(instances, (instance) => {
        if (_.isFunction(instance.dispose)) {
          instance.dispose();
        }
      });
    });

    this.instances = null;
    this.dispatcher = null;
  }

  resolve(obj) {
    if (!obj.constructor) {
      throw new Error('Cannot resolve object');
    }

    var id = obj.constructor.id;
    var type = obj.constructor.type;

    if (!this.instances[type]) {
      throw new Error(`Context does not have any instances of ${type}`);
    }

    if (!this.instances[type][id]) {
      throw new Error(`Context does not have an instance of the ${type} id`);
    }

    return this.instances[type][id];
  }

  getAll(type) {
    return _.values(this.instances[type]);
  }

  getAllStores() {
    return this.getAll('Store');
  }

  getAllStateSources() {
    return this.getAll('StateSource');
  }

  getAllActionCreators() {
    return this.getAll('ActionCreators');
  }
}

module.exports = Context;

function getInstance(context) {
  return Instances.get(context);
}

function deferred() {
  var result = {};
  result.promise = new Promise(function (resolve, reject) {
    result.resolve = resolve;
    result.reject = reject;
  });
  return result;
}
{'fraction_non_alphanumeric': 0.14407814407814407, 'fraction_numerical': 0.0010465724751439038, 'mean_word_length': 2.6268184693232133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '453699', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1762, 'n_words': 352}