| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 typedef void _AsyncCallback(); | 7 typedef void _AsyncCallback(); |
| 8 | 8 |
| 9 bool _callbacksAreEnqueued = false; | 9 bool _callbacksAreEnqueued = false; |
| 10 Queue<_AsyncCallback> _asyncCallbacks = new Queue<_AsyncCallback>(); | 10 Queue<_AsyncCallback> _asyncCallbacks = new Queue<_AsyncCallback>(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 * Runs a function asynchronously. | 39 * Runs a function asynchronously. |
| 40 * | 40 * |
| 41 * Callbacks registered through this function are always executed in order and | 41 * Callbacks registered through this function are always executed in order and |
| 42 * are guaranteed to run before other asynchronous events (like [Timer] events, | 42 * are guaranteed to run before other asynchronous events (like [Timer] events, |
| 43 * or DOM events). | 43 * or DOM events). |
| 44 * | 44 * |
| 45 * **Warning:** it is possible to starve the DOM by registering asynchronous | 45 * **Warning:** it is possible to starve the DOM by registering asynchronous |
| 46 * callbacks through this method. For example the following program runs | 46 * callbacks through this method. For example the following program runs |
| 47 * the callbacks without ever giving the Timer callback a chance to execute: | 47 * the callbacks without ever giving the Timer callback a chance to execute: |
| 48 * | 48 * |
| 49 * Timer.run(() { print("executed"); }); // Will never be executed, | 49 * Timer.run(() { print("executed"); }); // Will never be executed. |
| 50 * foo() { | 50 * foo() { |
| 51 * scheduleMicrotask(foo); // Schedules [foo] in front of other events. | 51 * scheduleMicrotask(foo); // Schedules [foo] in front of other events. |
| 52 * } | 52 * } |
| 53 * main() { | 53 * main() { |
| 54 * foo(); | 54 * foo(); |
| 55 * } | 55 * } |
| 56 * | 56 * |
| 57 * ## Other Resources | 57 * ## Other resources |
| 58 * | 58 * |
| 59 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | 59 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): |
| 60 * Learn how Dart handles the event queue and microtask queue, so you can write | 60 * Learn how Dart handles the event queue and microtask queue, so you can write |
| 61 * better asynchronous code with fewer surprises. | 61 * better asynchronous code with fewer surprises. |
| 62 */ | 62 */ |
| 63 void scheduleMicrotask(void callback()) { | 63 void scheduleMicrotask(void callback()) { |
| 64 if (Zone.current == Zone.ROOT) { | 64 if (Zone.current == Zone.ROOT) { |
| 65 // No need to bind the callback. We know that the root's scheduleMicrotask | 65 // No need to bind the callback. We know that the root's scheduleMicrotask |
| 66 // will be invoked in the root zone. | 66 // will be invoked in the root zone. |
| 67 Zone.current.scheduleMicrotask(callback); | 67 Zone.current.scheduleMicrotask(callback); |
| 68 return; | 68 return; |
| 69 } | 69 } |
| 70 Zone.current.scheduleMicrotask( | 70 Zone.current.scheduleMicrotask( |
| 71 Zone.current.bindCallback(callback, runGuarded: true)); | 71 Zone.current.bindCallback(callback, runGuarded: true)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 /** | 74 /** |
| 75 * *DEPRECATED*. Use [scheduleMicrotask] instead. | 75 * *DEPRECATED*. Use [scheduleMicrotask] instead. |
| 76 */ | 76 */ |
| 77 @deprecated | 77 @deprecated |
| 78 void runAsync(void callback()) { | 78 void runAsync(void callback()) { |
| 79 scheduleMicrotask(callback); | 79 scheduleMicrotask(callback); |
| 80 } | 80 } |
| 81 | 81 |
| 82 class _AsyncRun { | 82 class _AsyncRun { |
| 83 /** Schedule the given callback before any other event in the event-loop. */ | 83 /** Schedule the given callback before any other event in the event-loop. */ |
| 84 external static void _scheduleImmediate(void callback()); | 84 external static void _scheduleImmediate(void callback()); |
| 85 } | 85 } |
| OLD | NEW |