<commit_before>import Ember from 'ember';
import RSVP from 'ember/rsvp';
import { computed, inspect, get } from 'ember';
import { Promise } from 'ember/rsvp';
import { withPromise } from './utils';

var CALLBACKS = [
  'beforeEvent',
  '_activateTransition_',
  'willExit',
  'willEnter',
  '_setNewState_',
  'didExit',
  'didEnter',
  '_deactivateTransition_',
  'afterEvent'
];

var EXT_CALLBACK_SOURCES = {
  willExit: 'fromState',
  didExit: 'fromState',
  willEnter: 'toState',
  didEnter: 'toState',
  beforeEvent: 'event',
  afterEvent: 'event'
};

export default Ember.Object.extend({
  target:      null,
  machine:     null,
  fromState:   null,
  toState:     null,
  event:       null,
  eventArgs:   null,
  beforeEvent: null,
  willEnter:   null,
  didEnter:    null,
  willExit:    null,
  didExit:     null,
  afterEvent:  null,
  isAborted:   null,
  isResolving: null,
  isResolved:  computed.not('isResolving'),
  isRejected:  null,

  init: function() {
    this.set('resolutions', {});
    this.set('rejections',  {});
  },

  abort: function() {
    this.set('isAborted', true);
  },

  perform: function() {
    var transition = this;
    var promise;

    promise = new Promise(function(resolve, reject) {
      var currentCallbackIndex = 0;

      function next() {
        var cb = CALLBACKS[currentCallbackIndex++];

        if (!cb) {
          resolve(transition);
        } else {
          transition.callback(cb).then(next, reject);
        }
      }

      next();
    });

    this.set('isResolving', true);

    promise.then(function() {
      transition.set('isRejected', false);
    });

    promise.catch(function() {
      transition.set('isRejected', true);
    });

    promise.finally(function() {
      transition.set('isResolving', false);
    });

    return promise;
  },

  callbacksFor: function(transitionEvent) {
    var callbacks = [];
    var machine   = this.get('machine');
    var def       = machine.definition;
    var target    = this.get('target');
    var sources   = [this];
    var sourceCallbackNames;
    var extSource;
    var source;
    var callbackVia;
    var callbackName;
    var callbackFn;
    var i;
    var j;

    function fetchCallback(name) {
      var fn = get(target, name);

      if (!fn) {
        throw new Error('did not find callback "' + name + '" on target: ' +
        target);
      }

      return function() {
        return fn.apply(target, arguments);
      };
    }

    if ((extSource = EXT_CALLBACK_SOURCES[transitionEvent])) {
      if (extSource === 'event') {
        sources.push(def.lookupEvent(this.get(extSource)));
      } else {
        sources.push(def.lookupState(this.get(extSource)));
      }
    }

    for (i = 0; i < sources.length; i++) {
      source = sources[i];
      sourceCallbackNames = (source[transitionEvent] || []);

      for (j = 0; j < sourceCallbackNames.length; j++) {
        callbackName = sourceCallbackNames[j];
        callbackFn   = fetchCallback(callbackName);
        callbackVia  = source === this ? 'transition' : 'state';

        callbacks.push({
          via:  callbackVia,
          name: callbackName,
          fn:   callbackFn,
          key:  (callbackVia + ':' + callbackName)
        });<commit_msg>Allow for inline definitions of callbacks<commit_after>import Ember from 'ember';
import RSVP from 'ember/rsvp';
import { computed, inspect, get, typeOf, assert } from 'ember';
import { Promise } from 'ember/rsvp';
import { withPromise } from './utils';

var CALLBACKS = [
  'beforeEvent',
  '_activateTransition_',
  'willExit',
  'willEnter',
  '_setNewState_',
  'didExit',
  'didEnter',
  '_deactivateTransition_',
  'afterEvent'
];

var EXT_CALLBACK_SOURCES = {
  willExit: 'fromState',
  didExit: 'fromState',
  willEnter: 'toState',
  didEnter: 'toState',
  beforeEvent: 'event',
  afterEvent: 'event'
};

export default Ember.Object.extend({
  target:      null,
  machine:     null,
  fromState:   null,
  toState:     null,
  event:       null,
  eventArgs:   null,
  beforeEvent: null,
  willEnter:   null,
  didEnter:    null,
  willExit:    null,
  didExit:     null,
  afterEvent:  null,
  isAborted:   null,
  isResolving: null,
  isResolved:  computed.not('isResolving'),
  isRejected:  null,

  init: function() {
    this.set('resolutions', {});
    this.set('rejections',  {});
  },

  abort: function() {
    this.set('isAborted', true);
  },

  perform: function() {
    var transition = this;
    var promise;

    promise = new Promise(function(resolve, reject) {
      var currentCallbackIndex = 0;

      function next() {
        var cb = CALLBACKS[currentCallbackIndex++];

        if (!cb) {
          resolve(transition);
        } else {
          transition.callback(cb).then(next, reject);
        }
      }

      next();
    });

    this.set('isResolving', true);

    promise.then(function() {
      transition.set('isRejected', false);
    });

    promise.catch(function() {
      transition.set('isRejected', true);
    });

    promise.finally(function() {
      transition.set('isResolving', false);
    });

    return promise;
  },

  callbacksFor: function(transitionEvent) {
    var callbacks = [];
    var machine   = this.get('machine');
    var def       = machine.definition;
    var target    = this.get('target');
    var sources   = [this];
    var sourceCallbackNames;
    var extSource;
    var source;
    var callbackVia;
    var callbackName;
    var callbackFn;
    var i;
    var j;

    if ((extSource = EXT_CALLBACK_SOURCES[transitionEvent])) {
      if (extSource === 'event') {
        sources.push(def.lookupEvent(this.get(extSource)));
      } else {
        sources.push(def.lookupState(this.get(extSource)));
      }
    }

    for (i = 0; i < sources.length; i++) {
      source = sources[i];
      sourceCallbackNames = (source[transitionEvent] || []);

      for (j = 0; j < sourceCallbackNames.length; j++) {
        callbackName = sourceCallbackNames[j];

        if (typeOf(callbackName) === 'function') {
          callbackFn   = callbackName;
          callbackName = '-inline-';
        } else {
          callbackFn   = get(target, callbackName);
          assert('Callback "' + name + '" on target ' + target + ' should be a function, but is a ' + typeOf(callbackFn), typeOf(callbackFn) === 'function');
        }

        callbackVia  = source === this ? 'transition' : 'state';

        callbacks.push({
          via:  callbackVia,
          name: callbackName,
          fn:   callbackFn.bind(target),
          key:  (callbackVia + ':' + callbackName)
        });
{'fraction_non_alphanumeric': 0.14082158556919813, 'fraction_numerical': 0.0009095043201455207, 'mean_word_length': 2.576151761517615, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7103790', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2055, 'n_words': 464}