| 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 class _AsyncCallbackEntry { | 9 class _AsyncCallbackEntry { |
| 10 final _AsyncCallback callback; | 10 final _AsyncCallback callback; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 * Runs a function asynchronously. | 52 * Runs a function asynchronously. |
| 53 * | 53 * |
| 54 * Callbacks registered through this function are always executed in order and | 54 * Callbacks registered through this function are always executed in order and |
| 55 * are guaranteed to run before other asynchronous events (like [Timer] events, | 55 * are guaranteed to run before other asynchronous events (like [Timer] events, |
| 56 * or DOM events). | 56 * or DOM events). |
| 57 * | 57 * |
| 58 * **Warning:** it is possible to starve the DOM by registering asynchronous | 58 * **Warning:** it is possible to starve the DOM by registering asynchronous |
| 59 * callbacks through this method. For example the following program runs | 59 * callbacks through this method. For example the following program runs |
| 60 * the callbacks without ever giving the Timer callback a chance to execute: | 60 * the callbacks without ever giving the Timer callback a chance to execute: |
| 61 * | 61 * |
| 62 * Timer.run(() { print("executed"); }); // Will never be executed. | |
| 63 * foo() { | |
| 64 * scheduleMicrotask(foo); // Schedules [foo] in front of other events. | |
| 65 * } | |
| 66 * main() { | 62 * main() { |
| 63 * Timer.run(() { print("executed"); }); // Will never be executed. |
| 64 * foo() { |
| 65 * scheduleMicrotask(foo); // Schedules [foo] in front of other events. |
| 66 * } |
| 67 * foo(); | 67 * foo(); |
| 68 * } | 68 * } |
| 69 * | 69 * |
| 70 * ## Other resources | 70 * ## Other resources |
| 71 * | 71 * |
| 72 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | 72 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): |
| 73 * Learn how Dart handles the event queue and microtask queue, so you can write | 73 * Learn how Dart handles the event queue and microtask queue, so you can write |
| 74 * better asynchronous code with fewer surprises. | 74 * better asynchronous code with fewer surprises. |
| 75 */ | 75 */ |
| 76 void scheduleMicrotask(void callback()) { | 76 void scheduleMicrotask(void callback()) { |
| 77 if (Zone.current == Zone.ROOT) { | 77 if (identical(_ROOT_ZONE, Zone.current)) { |
| 78 // No need to bind the callback. We know that the root's scheduleMicrotask | 78 // No need to bind the callback. We know that the root's scheduleMicrotask |
| 79 // will be invoked in the root zone. | 79 // will be invoked in the root zone. |
| 80 Zone.current.scheduleMicrotask(callback); | 80 _rootScheduleMicrotask(null, null, _ROOT_ZONE, callback); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 Zone.current.scheduleMicrotask( | 83 Zone.current.scheduleMicrotask( |
| 84 Zone.current.bindCallback(callback, runGuarded: true)); | 84 Zone.current.bindCallback(callback, runGuarded: true)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 class _AsyncRun { | 87 class _AsyncRun { |
| 88 /** Schedule the given callback before any other event in the event-loop. */ | 88 /** Schedule the given callback before any other event in the event-loop. */ |
| 89 external static void _scheduleImmediate(void callback()); | 89 external static void _scheduleImmediate(void callback()); |
| 90 } | 90 } |
| OLD | NEW |