/**
  @module ember-flexberry
*/

import Ember from 'ember';

/**
  Injects 'after' method into 'Ember.run' namespace.

  @for ApplicationInitializer
  @method emberRunAfter.initialize
  @param {<a href="http://emberjs.com/api/classes/Ember.Application.html">Ember.Application</a>} application Ember application.
*/
export function initialize(application) {
  Ember.run.after = function(context, condition, handler) {
    let checkIntervalId;
    let checkInterval = 50;

    // Wait for condition fulfillment.
    Ember.run(() => {
      checkIntervalId = window.setInterval(() => {
        let conditionFulfilled = false;

        try {
          conditionFulfilled = condition.call(context) === true;
        } catch (e) {
          // Exception occurred while evaluating condition.
          // Clear interval & rethrow error.
          window.clearInterval(checkIntervalId);
          throw e;
        }

        if (!conditionFulfilled) {
          return;
        }

        // Condition is fulfilled.
        // Stop interval.
        window.clearInterval(checkIntervalId);

        // Call handler.
        Ember.run(() => {
          handler.call(context);
        });
      }, checkInterval);
    });
  };
}

export default {
  name: 'ember-run-after',
  initialize
};

/**
  @module ember-flexberry
*/

import Ember from 'ember';

/**
  Injects 'after' method into 'Ember.run' namespace.

  @for ApplicationInitializer
  @method emberRunAfter.initialize
*/
export function initialize() {
  Ember.run.after = function(context, condition, handler) {
    const checkInterval = 50;

    const checkCondition = () => {
      if (condition.call(context) === true) {
        Ember.run(context, handler);
      } else {
        Ember.run.later(checkCondition, checkInterval);
      }
    };

    Ember.run.later(checkCondition, checkInterval);
  };
}

export default {
  name: 'ember-run-after',
  initialize
};

Fix falling out of the ember run loop in run.after method