/**
  @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
};

@@ -182,136 +182,8 @@
 ize%0A
-  @param %7B%3Ca href=%22http://emberjs.com/api/classes/Ember.Application.html%22%3EEmber.Application%3C/a%3E%7D application Ember application.%0A
 */%0Ae
@@ -212,19 +212,8 @@
 ize(
-application
 ) %7B%0A
@@ -280,256 +280,80 @@
     
-let checkIntervalId;%0A    let checkInterval = 50;%0A%0A    // Wait for condition fulfillment.%0A    Ember.run(() =%3E %7B%0A      checkIntervalId = window.setInterval(() =%3E %7B%0A        let conditionFulfilled = false;%0A%0A        try %7B%0A          conditionFulfilled = 
+const checkInterval = 50;%0A%0A    const checkCondition = () =%3E %7B%0A      if (
 cond
@@ -384,28 +384,8 @@
 true
-;%0A        %7D catch (e
 ) %7B%0A
@@ -396,338 +396,92 @@
     
-  
-// Exception occurred while evaluating condition.%0A          // Clear interval & rethrow error.%0A          window.clearInterval(checkIntervalId);%0A          throw e;%0A        %7D%0A%0A        if (!conditionFulfilled) %7B%0A          return;%0A        %7D%0A%0A        // Condition is fulfilled.%0A        // Stop interval.%0A        window.clearInterval(
+Ember.run(context, handler);%0A      %7D else %7B%0A        Ember.run.later(checkCondition, 
 chec
@@ -493,43 +493,27 @@
 rval
-Id
 );%0A
-%0A
       
-  // Call handler.
+%7D
 %0A    
+%7D;%0A%0A
     
@@ -525,69 +525,29 @@
 .run
-(() =%3E %7B%0A          handler.call(context);%0A        %7D);%0A      %7D
+.later(checkCondition
 , ch
@@ -564,16 +564,8 @@
 l);%0A
-    %7D);%0A
   %7D;

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