| 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 25 matching lines...) Expand all Loading... |
| 36 } | 36 } |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Runs the given [callback] asynchronously. | 39 * Runs the given [callback] 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 will | 46 * callbacks through this method. For example the following program runs |
| 47 * run 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 * asyncRun(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 void runAsync(void callback()) { | 57 void scheduleMicrotask(void callback()) { |
| 58 if (Zone.current == Zone.ROOT) { | 58 if (Zone.current == Zone.ROOT) { |
| 59 // No need to bind the callback. We know that the root's runAsync will | 59 // No need to bind the callback. We know that the root's scheduleMicrotask |
| 60 // be invoked in the root zone. | 60 // will be invoked in the root zone. |
| 61 Zone.current.scheduleMicrotask(callback); | 61 Zone.current.scheduleMicrotask(callback); |
| 62 return; | 62 return; |
| 63 } | 63 } |
| 64 Zone.current.scheduleMicrotask( | 64 Zone.current.scheduleMicrotask( |
| 65 Zone.current.bindCallback(callback, runGuarded: true)); | 65 Zone.current.bindCallback(callback, runGuarded: true)); |
| 66 } | 66 } |
| 67 | 67 |
| 68 /** |
| 69 * *DEPRECATED*. Use [scheduleMicrotask] instead. |
| 70 */ |
| 71 @deprecated |
| 72 void runAsync(void callback()) { |
| 73 scheduleMicrotask(callback); |
| 74 } |
| 75 |
| 68 class _AsyncRun { | 76 class _AsyncRun { |
| 69 /** Enqueues the given callback before any other event in the event-loop. */ | 77 /** Enqueues the given callback before any other event in the event-loop. */ |
| 70 external static void _enqueueImmediate(void callback()); | 78 external static void _enqueueImmediate(void callback()); |
| 71 } | 79 } |
| OLD | NEW |