@@ -169,13 +169,15 @@ which will make you a better Ember developer.
169169
170170You should begin a run loop when the callback fires.
171171
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
172+ The ` run` method from ` @ember / runloop ` can be used to create a run loop.
173+ In this example, ` run` is used to handle an online
174174event (browser gains internet access) and run some Ember code.
175175
176176` ` ` javascript
177+ import { run } from ' @ember/runloop' ;
178+
177179window .addEventListener (' online' , () => {
178- Ember . run (() => { // begin loop
180+ run (() => { // begin loop
179181 // Code that results in jobs being scheduled goes here
180182 }); // end loop, jobs are flushed and executed
181183});
@@ -185,15 +187,17 @@ window.addEventListener('online', () => {
185187
186188## What happens if I forget to start a run loop in an async handler?
187189
188- As mentioned above, you should wrap any non-Ember async callbacks in ` Ember . run ` .
190+ As mentioned above, you should wrap any non-Ember async callbacks in ` run` .
189191If you don't, Ember will try to approximate a beginning and end for you.
190192Consider the following callback:
191193
192194` ` ` javascript
195+ import { schedule } from ' @ember/runloop' ;
196+
193197window .addEventListener (' online' , () => {
194198 console .log (' Doing things...' );
195199
196- Ember . run . schedule (' actions' , () => {
200+ schedule (' actions' , () => {
197201 // Do more things
198202 });
199203});
@@ -207,40 +211,42 @@ These automatically created run loops we call _autoruns_.
207211Here is some pseudocode to describe what happens using the example above:
208212
209213` ` ` javascript
214+ import { begin , end , schedule } from ' @ember/runloop' ;
215+
210216window .addEventListener (' online' , () => {
211217 // 1. autoruns do not change the execution of arbitrary code in a callback.
212218 // This code is still run when this callback is executed and will not be
213219 // scheduled on an autorun.
214220 console .log (' Doing things...' );
215221
216- Ember . run . schedule (' actions' , () => {
222+ schedule (' actions' , () => {
217223 // 2. schedule notices that there is no currently available run loop so it
218224 // creates one. It schedules it to close and flush queues on the next
219225 // turn of the JS event loop.
220226 if (! Ember .run .hasOpenRunLoop ()) {
221- Ember . run . begin ();
227+ begin ();
222228 nextTick (() => {
223- Ember . run . end ()
229+ end ()
224230 }, 0 );
225231 }
226232
227233 // 3. There is now a run loop available so schedule adds its item to the
228234 // given queue
229- Ember . run . schedule (' actions' , () => {
235+ schedule (' actions' , () => {
230236 // Do more things
231237 });
232238
233239 });
234240
235241 // 4. This schedule sees the autorun created by schedule above as an available
236242 // run loop and adds its item to the given queue.
237- Ember . run . schedule (' afterRender' , () => {
243+ schedule (' afterRender' , () => {
238244 // Do yet more things
239245 });
240246});
241247` ` `
242248
243249## Where can I find more information?
244250
245- Check out the [Ember.run ](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
251+ Check out the [@ember/runloop ](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
246252as well as the [Backburner library](https://github.com/ebryn/backburner.js/) that powers the run loop.
0 commit comments