<commit_before>import Ember from 'ember-metal/core';
import {
  apply,
  GUID_KEY
} from 'ember-metal/utils';
import { indexOf } from "ember-metal/array";
import {
  beginPropertyChanges,
  endPropertyChanges
} from 'ember-metal/property_events';
import Backburner from 'backburner';

function onBegin(current) {
  run.currentRunLoop = current;
}

function onEnd(current, next) {
  run.currentRunLoop = next;
}

// ES6TODO: should Backburner become es6?
var backburner = new Backburner(['sync', 'actions', 'destroy'], {
  GUID_KEY: GUID_KEY,
  sync: {
    before: beginPropertyChanges,
    after: endPropertyChanges
  },
  defaultQueue: 'actions',
  onBegin: onBegin,
  onEnd: onEnd,
  onErrorTarget: Ember,
  onErrorMethod: 'onerror'
});
var slice = [].slice;

// ..........................................................
// run - this is ideally the only public API the dev sees
//

/**
  Runs the passed target and method inside of a RunLoop, ensuring any
  deferred actions including bindings and views updates are flushed at the
  end.

  Normally you should not need to invoke this method yourself. However if
  you are implementing raw event handlers when interfacing with other
  libraries or plugins, you should probably wrap all of your code inside this
  call.

  ```javascript
  run(function() {
    // code to be execute within a RunLoop
  });
  ```

  @class run
  @namespace Ember
  @static
  @constructor
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} return value from invoking the passed function.
*/
export default run;
function run() {
  return backburner.run.apply(backburner, arguments);
}

/**
  If no run-loop is present, it creates a new one. If a run loop is
  present it will queue itself to run on the existing run-loops action
  queue.

  Please note: This is not for normal usage, and should be used sparingly.

  If invoked when not within a run loop:

  ```javascript
  run.join(function() {
    // creates a new run-loop
  });
  ```

  Alternatively, if called within an existing run loop:

  ```javascript
  run(function() {
    // creates a new run-loop
    run.join(function() {
      // joins with the existing run-loop, and queues for invocation on
      // the existing run-loops action queue.
    });
  });
  ```

  @method join
  @namespace Ember
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} Return value from invoking the passed function. Please note,
  when called within an existing loop, no return value is possible.
*/
run.join = function() {
  return backburner.join.apply(backburner, arguments);
};

/**
  Allows you to specify which context to call the specified function in while
  adding the execution of that function to the Ember run loop. This ability
  makes this method a great way to asynchronusly integrate third-party libraries
  into your Ember application.

  `run.bind` takes two main arguments, the desired context and the function to
  invoke in that context. Any additional arguments will be supplied as arguments
  to the function that is passed in.

  Let's use the creation of a TinyMCE component as an example. Currently,
  TinyMCE provides a setup configuration option we can use to do some processing
  after the TinyMCE instance is initialized but before it is actually rendered.
  We can use that setup option to do some additional setup for our component.
  The component itself could look something like the following:

  ```javascript
  App.RichTextEditorComponent = Ember.Component.extend({
    initializeTinyMCE: function(){
      tinymce.init({
        selector: '#' + this.$().prop('id'),
        setup: Ember.run.bind(this, this.setupEditor)
      });
    }.on('didInsertElement'),

    setupEditor: function(editor) {
      this.set('editor', editor);
      editor.on('change', function(){ console.log('content changed!')} );
    }
  });
  ```

  In this example, we use Ember.run.bind to bind the setupEditor message to the
  context of the App.RichTextEditorComponent and to have the invocation of that
  method be safely handled and excuted by the Ember run loop.

  @method bind
  @namespace Ember
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} return value from invoking the passed function. Please note,
  when called within an existing loop, no return value is possible.
  @since 1.4.0
*/
run.bind = function(target, method /* args */) {
  var args = slice.call(arguments);
  return function() {
    return run.join.apply(run, args.concat(slice.call(arguments)));
  };
};

run.backburner = backburner;
run.currentRunLoop = null;
run.queues = backburner.queueNames;

/**
  Begins a new RunLoop. Any deferred actions invoked after the begin will
  be buffered until you invoke a matching call to `run.end()`. This is
  a lower-level way to use a RunLoop instead of using `run()`.

  ```javascript
  run.begin();
  // code to be execute within a RunLoop
  run.end();
  ```

  @method begin
  @return {void}
*/
run.begin = function() {
  backburner.begin();
};

/**
  Ends a RunLoop. This must be called sometime after you call
  `run.begin()` to flush any deferred actions. This is a lower-level way
  to use a RunLoop instead of using `run()`.

  ```javascript
  run.begin();
  // code to be execute within a RunLoop
  run.end();
  ```

  @method end
  @return {void}
*/
run.end = function() {
  backburner.end();
};

/**
  Array of named queues. This array determines the order in which queues
  are flushed at the end of the RunLoop. You can define your own queues by
  simply adding the queue name to this array. Normally you should not need
  to inspect or modify this property.

  @property queues
  @type Array
  @default ['sync', 'actions', 'destroy']
*/

/**
  Adds the passed target/method and any optional arguments to the named
  queue to be executed at the end of the RunLoop. If you have not already
  started a RunLoop when calling this method one will be started for you<commit_msg>[DOC beta] "to be execute" -> "to be executed"<commit_after>import Ember from 'ember-metal/core';
import {
  apply,
  GUID_KEY
} from 'ember-metal/utils';
import { indexOf } from "ember-metal/array";
import {
  beginPropertyChanges,
  endPropertyChanges
} from 'ember-metal/property_events';
import Backburner from 'backburner';

function onBegin(current) {
  run.currentRunLoop = current;
}

function onEnd(current, next) {
  run.currentRunLoop = next;
}

// ES6TODO: should Backburner become es6?
var backburner = new Backburner(['sync', 'actions', 'destroy'], {
  GUID_KEY: GUID_KEY,
  sync: {
    before: beginPropertyChanges,
    after: endPropertyChanges
  },
  defaultQueue: 'actions',
  onBegin: onBegin,
  onEnd: onEnd,
  onErrorTarget: Ember,
  onErrorMethod: 'onerror'
});
var slice = [].slice;

// ..........................................................
// run - this is ideally the only public API the dev sees
//

/**
  Runs the passed target and method inside of a RunLoop, ensuring any
  deferred actions including bindings and views updates are flushed at the
  end.

  Normally you should not need to invoke this method yourself. However if
  you are implementing raw event handlers when interfacing with other
  libraries or plugins, you should probably wrap all of your code inside this
  call.

  ```javascript
  run(function() {
    // code to be executed within a RunLoop
  });
  ```

  @class run
  @namespace Ember
  @static
  @constructor
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} return value from invoking the passed function.
*/
export default run;
function run() {
  return backburner.run.apply(backburner, arguments);
}

/**
  If no run-loop is present, it creates a new one. If a run loop is
  present it will queue itself to run on the existing run-loops action
  queue.

  Please note: This is not for normal usage, and should be used sparingly.

  If invoked when not within a run loop:

  ```javascript
  run.join(function() {
    // creates a new run-loop
  });
  ```

  Alternatively, if called within an existing run loop:

  ```javascript
  run(function() {
    // creates a new run-loop
    run.join(function() {
      // joins with the existing run-loop, and queues for invocation on
      // the existing run-loops action queue.
    });
  });
  ```

  @method join
  @namespace Ember
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} Return value from invoking the passed function. Please note,
  when called within an existing loop, no return value is possible.
*/
run.join = function() {
  return backburner.join.apply(backburner, arguments);
};

/**
  Allows you to specify which context to call the specified function in while
  adding the execution of that function to the Ember run loop. This ability
  makes this method a great way to asynchronusly integrate third-party libraries
  into your Ember application.

  `run.bind` takes two main arguments, the desired context and the function to
  invoke in that context. Any additional arguments will be supplied as arguments
  to the function that is passed in.

  Let's use the creation of a TinyMCE component as an example. Currently,
  TinyMCE provides a setup configuration option we can use to do some processing
  after the TinyMCE instance is initialized but before it is actually rendered.
  We can use that setup option to do some additional setup for our component.
  The component itself could look something like the following:

  ```javascript
  App.RichTextEditorComponent = Ember.Component.extend({
    initializeTinyMCE: function(){
      tinymce.init({
        selector: '#' + this.$().prop('id'),
        setup: Ember.run.bind(this, this.setupEditor)
      });
    }.on('didInsertElement'),

    setupEditor: function(editor) {
      this.set('editor', editor);
      editor.on('change', function(){ console.log('content changed!')} );
    }
  });
  ```

  In this example, we use Ember.run.bind to bind the setupEditor message to the
  context of the App.RichTextEditorComponent and to have the invocation of that
  method be safely handled and excuted by the Ember run loop.

  @method bind
  @namespace Ember
  @param {Object} [target] target of method to call
  @param {Function|String} method Method to invoke.
    May be a function or a string. If you pass a string
    then it will be looked up on the passed target.
  @param {Object} [args*] Any additional arguments you wish to pass to the method.
  @return {Object} return value from invoking the passed function. Please note,
  when called within an existing loop, no return value is possible.
  @since 1.4.0
*/
run.bind = function(target, method /* args */) {
  var args = slice.call(arguments);
  return function() {
    return run.join.apply(run, args.concat(slice.call(arguments)));
  };
};

run.backburner = backburner;
run.currentRunLoop = null;
run.queues = backburner.queueNames;

/**
  Begins a new RunLoop. Any deferred actions invoked after the begin will
  be buffered until you invoke a matching call to `run.end()`. This is
  a lower-level way to use a RunLoop instead of using `run()`.

  ```javascript
  run.begin();
  // code to be executed within a RunLoop
  run.end();
  ```

  @method begin
  @return {void}
*/
run.begin = function() {
  backburner.begin();
};

/**
  Ends a RunLoop. This must be called sometime after you call
  `run.begin()` to flush any deferred actions. This is a lower-level way
  to use a RunLoop instead of using `run()`.

  ```javascript
  run.begin();
  // code to be executed within a RunLoop
  run.end();
  ```

  @method end
  @return {void}
*/
run.end = function() {
  backburner.end();
};

/**
  Array of named queues. This array determines the order in which queues
  are flushed at the end of the RunLoop. You can define your own queues by
  simply adding the queue name to this array. Normally you should not need
  to inspect or modify this property.

  @property queues
  @type Array
  @default ['sync', 'actions', 'destroy']
*/

/**
  Adds the passed target/method and any optional arguments to the named
  queue to be executed at the end of the RunLoop. If you have not already
  started a RunLoop when calling this method one will be started for you
{'fraction_non_alphanumeric': 0.0958842827318819, 'fraction_numerical': 0.0007456009543692216, 'mean_word_length': 3.346403110823072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1064596', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3727, 'n_words': 1712}