| 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 List<_AsyncCallback> _asyncCallbacks = <_AsyncCallback>[]; | 10 List<_AsyncCallback> _asyncCallbacks = <_AsyncCallback>[]; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 _asyncCallbacks.addAll(newCallbacks); | 31 _asyncCallbacks.addAll(newCallbacks); |
| 32 _AsyncRun._enqueueImmediate(_asyncRunCallback); | 32 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 33 throw; | 33 throw; |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 // Any new callback must register a callback function now. | 37 // Any new callback must register a callback function now. |
| 38 _callbacksAreEnqueued = false; | 38 _callbacksAreEnqueued = false; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void _scheduleAsyncCallback(callback) { |
| 42 // Optimizing a group of Timer.run callbacks to be executed in the |
| 43 // same Timer callback. |
| 44 _asyncCallbacks.add(callback); |
| 45 if (!_callbacksAreEnqueued) { |
| 46 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 47 _callbacksAreEnqueued = true; |
| 48 } |
| 49 } |
| 50 |
| 41 /** | 51 /** |
| 42 * Runs the given [callback] asynchronously. | 52 * Runs the given [callback] asynchronously. |
| 43 * | 53 * |
| 44 * Callbacks registered through this function are always executed in order and | 54 * Callbacks registered through this function are always executed in order and |
| 45 * are guaranteed to run before other asynchronous events (like [Timer] events, | 55 * are guaranteed to run before other asynchronous events (like [Timer] events, |
| 46 * or DOM events). | 56 * or DOM events). |
| 47 * | 57 * |
| 48 * Warning: it is possible to starve the DOM by registering asynchronous | 58 * Warning: it is possible to starve the DOM by registering asynchronous |
| 49 * callbacks through this method. For example the following program will | 59 * callbacks through this method. For example the following program will |
| 50 * run the callbacks without ever giving the Timer callback a chance to execute: | 60 * run the callbacks without ever giving the Timer callback a chance to execute: |
| 51 * | 61 * |
| 52 * Timer.run(() { print("executed"); }); // Will never be executed; | 62 * Timer.run(() { print("executed"); }); // Will never be executed; |
| 53 * foo() { | 63 * foo() { |
| 54 * asyncRun(foo); // Schedules [foo] in front of other events. | 64 * asyncRun(foo); // Schedules [foo] in front of other events. |
| 55 * } | 65 * } |
| 56 * main() { | 66 * main() { |
| 57 * foo(); | 67 * foo(); |
| 58 * } | 68 * } |
| 59 */ | 69 */ |
| 60 void runAsync(void callback()) { | 70 void runAsync(void callback()) { |
| 61 // Optimizing a group of Timer.run callbacks to be executed in the | 71 _Zone._current.runAsync(callback); |
| 62 // same Timer callback. | |
| 63 _asyncCallbacks.add(callback); | |
| 64 if (!_callbacksAreEnqueued) { | |
| 65 _AsyncRun._enqueueImmediate(_asyncRunCallback); | |
| 66 _callbacksAreEnqueued = true; | |
| 67 } | |
| 68 } | 72 } |
| 69 | 73 |
| 70 class _AsyncRun { | 74 class _AsyncRun { |
| 71 /** Enqueues the given callback before any other event in the event-loop. */ | 75 /** Enqueues the given callback before any other event in the event-loop. */ |
| 72 external static void _enqueueImmediate(void callback()); | 76 external static void _enqueueImmediate(void callback()); |
| 73 } | 77 } |
| OLD | NEW |