<commit_before>import _ from 'underscore';
import Marionette from 'backbone.marionette';
import AbstractApp from './abstract-app';

/**
 * AbstractApp with an "App Manager" functionality mixed in for adding and removing child `App`s.
 *
 * @public
 * @class App
 * @memberOf Toolkit
 * @memberOf Marionette
 */
var App = AbstractApp.extend({

  /**
   * @public
   * @constructs App
   * @param {Object} [options] - Settings for the App.
   * @param {Object} [options.childApps] - Hash for setting up child apps.
   *
   * ```js
   * childApps: {
   *   appName: {
   *     AppClass: MyChildAppClass,
   *     fooOption: true,
   *     startWithParent: true
   *   },
   *   barApp: MyOtherChildAppClass
   * }
   * ```
   */
  constructor: function(options) {
    options = options || {};

    this._childApps = {};

    _.extend(this, _.pick(options, ['childApps']));

    this._initChildApps();

    // The child apps should be handled while the app is running;
    // After start, before stop, and before destroy.
    this.on({
      'start'          : this._startChildApps,
      'before:stop'    : this._stopChildApps,
      'before:destroy' : this._destroyChildApps
    });

    AbstractApp.call(this, options);
  },

  /**
   * Initializes `childApps` option
   *
   * @private
   * @method _initChildApps
   * @memberOf App
   */
  _initChildApps: function() {
    if(this.childApps) {
      this.addChildApps(_.result(this, 'childApps'));
    }
  },

  /**
   * Starts `childApps` if allowed by child
   *
   * @private
   * @method _startChildApps
   * @memberOf App
   */
  _startChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'startWithParent')) {
        childApp.start();
      }
    });
  },

  /**
   * Stops `childApps` if allowed by child
   *
   * @private
   * @method _stopChildApps
   * @memberOf App
   */
  _stopChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'stopWithParent')) {
        childApp.stop();
      }
    });
  },

  /**
   * Destroys `childApps` if allowed by child
   *
   * @private
   * @method _destroyChildApps
   * @memberOf App
   */
  _destroyChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'preventDestroy')) {
        childApp.destroy();
      }
    });
  },

  /**
   * Internal helper to instantiate and `App` from on `Object`
   *
   * @private
   * @method _buildAppFromObject
   * @memberOf App
   * @param {Object} [appConfig] - `AppClass` and any other option for the `App`
   * @returns {App}
   */
  _buildAppFromObject: function(appConfig) {
    var AppClass = appConfig.AppClass;
    var options = _.omit(appConfig, 'AppClass');

    return new AppClass(options);
  },

  /**
   * Builds an App and return it
   *
   * @public
   * @method buildApp
   * @memberOf App
   * @param {App} [AppClass] - An App Class
   * @param {Object} [AppClass] - Optionally passed as an appConfig Object
   * @param {Object=} [options] - options for the AppClass
   * @returns {App}
   */
  buildApp: function(AppClass, options) {
    if(_.isObject(AppClass)) {
      return this._buildAppFromObject(AppClass);
    }

    if(_.isFunction(AppClass)) {
      return new AppClass(options);
    }
  },

  /**
   * Internal helper to verify `appName` is unique and not in use
   *
   * @private
   * @method _ensureAppIsUnique
   * @memberOf App
   * @param {String} [appName] - Name of app to test
   * @throws DuplicateChildAppError - Thrown if `App` already has an `appName` registered
   */
  _ensureAppIsUnique: function(appName) {
    if(this._childApps[appName]) {
      throw new Marionette.Error({
        name: 'DuplicateChildAppError',
        message: 'A child App with that name has already been added.'
      });
    }
  },

  /**
   * Add child `App`s to this `App`
   *
   * @public
   * @method addChildApps
   * @memberOf App
   * @param {Object} [childApps] - Hash of names and `AppClass` or `appConfig`
   */
  addChildApps: function(childApps) {
    _.each(childApps, function(childApp, appName) {
      this.addChildApp(appName, childApp);
    }, this);
  },

  /**
   * Build's childApp and registers it with this App
   * Starts the childApp, if this app is running and child is `startWithParent`
   *
   * @public
   * @method addChildApp
   * @memberOf App
   * @param {String} [appName] - Name of App to register
   * @param {App} [AppClass] - An App Class
   * @param {Object} [AppClass] - Optionally passed as an appConfig Object
   * @param {Object=} [options] - options for the AppClass
   * @throws AddChildAppError - Thrown if no childApp could be built from params
   * @returns {App}
   */
  addChildApp: function(appName, AppClass, options) {
    this._ensureAppIsUnique();

    var childApp = this.buildApp(AppClass, options);

    if(!childApp){
      throw new Marionette.Error({
        name: 'AddChildAppError',
        message: 'App build failed.  Incorrect configuration.'
      });
    }

    this._childApps[appName] = childApp;

    // When the app is destroyed remove the cached app.
    childApp.on('destroy', _.partial(this._removeChildApp, appName), this);

    if(this.isRunning() && _.result(childApp, 'startWithParent')) {
      childApp.start();
    }

    return childApp;
  },<commit_msg>Add argument to function call<commit_after>import _ from 'underscore';
import Marionette from 'backbone.marionette';
import AbstractApp from './abstract-app';

/**
 * AbstractApp with an "App Manager" functionality mixed in for adding and removing child `App`s.
 *
 * @public
 * @class App
 * @memberOf Toolkit
 * @memberOf Marionette
 */
var App = AbstractApp.extend({

  /**
   * @public
   * @constructs App
   * @param {Object} [options] - Settings for the App.
   * @param {Object} [options.childApps] - Hash for setting up child apps.
   *
   * ```js
   * childApps: {
   *   appName: {
   *     AppClass: MyChildAppClass,
   *     fooOption: true,
   *     startWithParent: true
   *   },
   *   barApp: MyOtherChildAppClass
   * }
   * ```
   */
  constructor: function(options) {
    options = options || {};

    this._childApps = {};

    _.extend(this, _.pick(options, ['childApps']));

    this._initChildApps();

    // The child apps should be handled while the app is running;
    // After start, before stop, and before destroy.
    this.on({
      'start'          : this._startChildApps,
      'before:stop'    : this._stopChildApps,
      'before:destroy' : this._destroyChildApps
    });

    AbstractApp.call(this, options);
  },

  /**
   * Initializes `childApps` option
   *
   * @private
   * @method _initChildApps
   * @memberOf App
   */
  _initChildApps: function() {
    if(this.childApps) {
      this.addChildApps(_.result(this, 'childApps'));
    }
  },

  /**
   * Starts `childApps` if allowed by child
   *
   * @private
   * @method _startChildApps
   * @memberOf App
   */
  _startChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'startWithParent')) {
        childApp.start();
      }
    });
  },

  /**
   * Stops `childApps` if allowed by child
   *
   * @private
   * @method _stopChildApps
   * @memberOf App
   */
  _stopChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'stopWithParent')) {
        childApp.stop();
      }
    });
  },

  /**
   * Destroys `childApps` if allowed by child
   *
   * @private
   * @method _destroyChildApps
   * @memberOf App
   */
  _destroyChildApps: function() {
    _.each(this._childApps, function(childApp) {
      if(_.result(childApp, 'preventDestroy')) {
        childApp.destroy();
      }
    });
  },

  /**
   * Internal helper to instantiate and `App` from on `Object`
   *
   * @private
   * @method _buildAppFromObject
   * @memberOf App
   * @param {Object} [appConfig] - `AppClass` and any other option for the `App`
   * @returns {App}
   */
  _buildAppFromObject: function(appConfig) {
    var AppClass = appConfig.AppClass;
    var options = _.omit(appConfig, 'AppClass');

    return new AppClass(options);
  },

  /**
   * Builds an App and return it
   *
   * @public
   * @method buildApp
   * @memberOf App
   * @param {App} [AppClass] - An App Class
   * @param {Object} [AppClass] - Optionally passed as an appConfig Object
   * @param {Object=} [options] - options for the AppClass
   * @returns {App}
   */
  buildApp: function(AppClass, options) {
    if(_.isObject(AppClass)) {
      return this._buildAppFromObject(AppClass);
    }

    if(_.isFunction(AppClass)) {
      return new AppClass(options);
    }
  },

  /**
   * Internal helper to verify `appName` is unique and not in use
   *
   * @private
   * @method _ensureAppIsUnique
   * @memberOf App
   * @param {String} [appName] - Name of app to test
   * @throws DuplicateChildAppError - Thrown if `App` already has an `appName` registered
   */
  _ensureAppIsUnique: function(appName) {
    if(this._childApps[appName]) {
      throw new Marionette.Error({
        name: 'DuplicateChildAppError',
        message: 'A child App with that name has already been added.'
      });
    }
  },

  /**
   * Add child `App`s to this `App`
   *
   * @public
   * @method addChildApps
   * @memberOf App
   * @param {Object} [childApps] - Hash of names and `AppClass` or `appConfig`
   */
  addChildApps: function(childApps) {
    _.each(childApps, function(childApp, appName) {
      this.addChildApp(appName, childApp);
    }, this);
  },

  /**
   * Build's childApp and registers it with this App
   * Starts the childApp, if this app is running and child is `startWithParent`
   *
   * @public
   * @method addChildApp
   * @memberOf App
   * @param {String} [appName] - Name of App to register
   * @param {App} [AppClass] - An App Class
   * @param {Object} [AppClass] - Optionally passed as an appConfig Object
   * @param {Object=} [options] - options for the AppClass
   * @throws AddChildAppError - Thrown if no childApp could be built from params
   * @returns {App}
   */
  addChildApp: function(appName, AppClass, options) {
    this._ensureAppIsUnique(appName);

    var childApp = this.buildApp(AppClass, options);

    if(!childApp){
      throw new Marionette.Error({
        name: 'AddChildAppError',
        message: 'App build failed.  Incorrect configuration.'
      });
    }

    this._childApps[appName] = childApp;

    // When the app is destroyed remove the cached app.
    childApp.on('destroy', _.partial(this._removeChildApp, appName), this);

    if(this.isRunning() && _.result(childApp, 'startWithParent')) {
      childApp.start();
    }

    return childApp;
  },
{'fraction_non_alphanumeric': 0.12963481834314, 'fraction_numerical': 0.0, 'mean_word_length': 2.745365512416929, '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': '1109957', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3467, 'n_words': 892}