EmberJS - Run Loop



It is an area where most of the application's internal code takes place. This is used to batch and is a way of ordering or re-ordering the work to check if it is effective and efficient. It schedules the work based on specific queues to complete the work in priority order.

Integrating the run loop with non-Ember API leads to some asynchronous callback. For instance −

  • setTimeout and setInterval callbacks
  • AJAX callbacks
  • postMessage and messageChannel event handlers
  • Websocket callbacks
  • DOM update and event callbacks

The run loop works in Ember based on queues specified priority-wise −

Ember.run.queues
=> ["sync", "actions", "routerTransitions", "render", "afterRender", "destroy"]
  • sync − It is a higher priority queue that includes binding synchronization jobs.

  • actions − It is a general work queue that includes scheduled tasks.

  • routerTransitions − It specifies the transition jobs in router.

  • render − It is used for rendering the jobs which update the DOM.

  • afterRender − It runs the jobs after completing the scheduled tasks.

  • destroy − It is a lower priority queue that terminates the jobs which are scheduled to destroy.

Execution of Jobs based on Queues

Follow these steps for the execution of Jobs based on Queues −

Step 1 − In this step, pending jobs of highest priority queue will be checked in CURRENT_QUEUE. The run loop will be completed, if there are no pending jobs.

Step 2 − Specify the new temporary queue as WORK_QUEUE.

Step 3 − Transfer the jobs from CURRENT_QUEUE to WORK_QUEUE.

Step 4 − Successively process the jobs in WORK_QUEUE.

Step 5 − Repeat from Step 1.

Behavior of Run Loop when Testing

If we try to schedule the work without run loop, then Ember will throw an error when the application is in testing mode. Consider the following reasons to understand why Autoruns are disabled −

  • If you fail to open the run loop before scheduling callbacks on it, then Autoruns will not make any mistake in the production.

  • Disabling the autoruns identifies the incorrect test failures which occur when an application runs outside a run loop and helps in the testing of your application.

For more about these run loop along with an example, see this link.

emberjs_application_concerns.htm
Advertisements