1- ** Note:**
2- * _ For basic Ember app development scenarios, you don't need to understand the run loop or use it directly. All common paths are paved nicely for you and don't require working with the run loop._
3- * _ However, the run loop will be helpful to understand the internals of Ember and to assist in customized performance tuning by manually batching costly work._
1+ <div class =" cta " >
2+ <div class =" cta-note " >
3+ <div class="cta-note-body">
4+ <div class="cta-note-heading" data-test-es-note-heading="">Zoey says...</div>
5+ <div class="cta-note-message">
6+
7+ <p>
8+ For basic Ember app development scenarios, you don't need to understand the run loop or use it directly. All common paths are paved nicely for you and don't require working with the run loop.
9+ </p>
10+
11+ <p>
12+ However, the run loop will be helpful to understand the internals of Ember and to assist in customized performance tuning by manually batching costly work.
13+ </p>
14+
15+ </div>
16+ </div>
17+ <img src="/images/mascots/zoey.png" role="presentation" alt="">
18+ </div >
19+ </div >
420
521Ember's internals and most of the code you will write in your applications takes place in a run loop.
622The run loop is used to batch, and order (or reorder) work in a way that is most effective and efficient.
@@ -83,9 +99,13 @@ class Image {
8399
84100and a template to display its attributes:
85101
86- ` ` ` handlebars
87- {{this .width }}
88- {{this .aspectRatio }}
102+ ` ` ` gjs
103+ let profilePhoto = new Image ({ width: 250 , height: 500 });
104+
105+ < template>
106+ {{profilePhoto .width }}
107+ {{profilePhoto .aspectRatio }}
108+ < / template>
89109` ` `
90110
91111If we execute the following code without the run loop:
@@ -169,13 +189,15 @@ which will make you a better Ember developer.
169189
170190You should begin a run loop when the callback fires.
171191
172- The ` Ember . run ` method can be used to create a run loop.
173- In this example, ` Ember . run ` is used to handle an online
192+ The ` run () ` method, imported from ` @ember / runloop ` , can be used to create a run loop.
193+ In this example, ` run () ` is used to handle an online
174194event (browser gains internet access) and run some Ember code.
175195
176196` ` ` javascript
197+ import { run } from ' @ember/runloop' ;
198+
177199window .addEventListener (' online' , () => {
178- Ember . run (() => { // begin loop
200+ run (() => { // begin loop
179201 // Code that results in jobs being scheduled goes here
180202 }); // end loop, jobs are flushed and executed
181203});
@@ -185,15 +207,17 @@ window.addEventListener('online', () => {
185207
186208## What happens if I forget to start a run loop in an async handler?
187209
188- As mentioned above, you should wrap any non-Ember async callbacks in ` Ember . run ` .
210+ As mentioned above, you should wrap any non-Ember async callbacks in ` run () ` .
189211If you don't, Ember will try to approximate a beginning and end for you.
190212Consider the following callback:
191213
192214` ` ` javascript
215+ import { run } from ' @ember/runloop' ;
216+
193217window .addEventListener (' online' , () => {
194218 console .log (' Doing things...' );
195219
196- Ember . run .schedule (' actions' , () => {
220+ run .schedule (' actions' , () => {
197221 // Do more things
198222 });
199223});
@@ -207,40 +231,42 @@ These automatically created run loops we call _autoruns_.
207231Here is some pseudocode to describe what happens using the example above:
208232
209233` ` ` javascript
234+ import { run } from ' @ember/runloop' ;
235+
210236window .addEventListener (' online' , () => {
211237 // 1. autoruns do not change the execution of arbitrary code in a callback.
212238 // This code is still run when this callback is executed and will not be
213239 // scheduled on an autorun.
214240 console .log (' Doing things...' );
215241
216- Ember . run .schedule (' actions' , () => {
242+ run .schedule (' actions' , () => {
217243 // 2. schedule notices that there is no currently available run loop so it
218244 // creates one. It schedules it to close and flush queues on the next
219245 // turn of the JS event loop.
220- if (! Ember . run .hasOpenRunLoop ()) {
221- Ember . run .begin ();
246+ if (! run .hasOpenRunLoop ()) {
247+ run .begin ();
222248 nextTick (() => {
223- Ember . run .end ()
249+ run .end ()
224250 }, 0 );
225251 }
226252
227253 // 3. There is now a run loop available so schedule adds its item to the
228254 // given queue
229- Ember . run .schedule (' actions' , () => {
255+ run .schedule (' actions' , () => {
230256 // Do more things
231257 });
232258
233259 });
234260
235261 // 4. This schedule sees the autorun created by schedule above as an available
236262 // run loop and adds its item to the given queue.
237- Ember . run .schedule (' afterRender' , () => {
263+ run .schedule (' afterRender' , () => {
238264 // Do yet more things
239265 });
240266});
241267` ` `
242268
243269## Where can I find more information?
244270
245- Check out the [Ember.run ](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
271+ Check out the [` @ember / runloop ` ](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
246272as well as the [Backburner library](https://github.com/ebryn/backburner.js/) that powers the run loop.
0 commit comments