| 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>(); |
| 11 | 11 |
| 12 void _asyncRunCallback() { | 12 void _asyncRunCallback() { |
| 13 // As long as we are iterating over the registered callbacks we don't | 13 // As long as we are iterating over the registered callbacks we don't |
| 14 // unset the [_callbacksAreEnqueued] boolean. | 14 // unset the [_callbacksAreEnqueued] boolean. |
| 15 while (!_asyncCallbacks.isEmpty) { | 15 while (!_asyncCallbacks.isEmpty) { |
| 16 Function callback = _asyncCallbacks.removeFirst(); | 16 Function callback = _asyncCallbacks.removeFirst(); |
| 17 try { | 17 try { |
| 18 callback(); | 18 callback(); |
| 19 } catch (e) { | 19 } catch (e) { |
| 20 _AsyncRun._enqueueImmediate(_asyncRunCallback); | 20 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 21 rethrow; | 21 throw; |
| 22 } | 22 } |
| 23 } | 23 } |
| 24 // Any new callback must register a callback function now. | 24 // Any new callback must register a callback function now. |
| 25 _callbacksAreEnqueued = false; | 25 _callbacksAreEnqueued = false; |
| 26 } | 26 } |
| 27 | 27 |
| 28 void _scheduleAsyncCallback(callback) { | |
| 29 // Optimizing a group of Timer.run callbacks to be executed in the | |
| 30 // same Timer callback. | |
| 31 _asyncCallbacks.add(callback); | |
| 32 if (!_callbacksAreEnqueued) { | |
| 33 _AsyncRun._enqueueImmediate(_asyncRunCallback); | |
| 34 _callbacksAreEnqueued = true; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 /** | 28 /** |
| 39 * Runs the given [callback] asynchronously. | 29 * Runs the given [callback] asynchronously. |
| 40 * | 30 * |
| 41 * Callbacks registered through this function are always executed in order and | 31 * Callbacks registered through this function are always executed in order and |
| 42 * are guaranteed to run before other asynchronous events (like [Timer] events, | 32 * are guaranteed to run before other asynchronous events (like [Timer] events, |
| 43 * or DOM events). | 33 * or DOM events). |
| 44 * | 34 * |
| 45 * Warning: it is possible to starve the DOM by registering asynchronous | 35 * Warning: it is possible to starve the DOM by registering asynchronous |
| 46 * callbacks through this method. For example the following program will | 36 * callbacks through this method. For example the following program will |
| 47 * run the callbacks without ever giving the Timer callback a chance to execute: | 37 * run the callbacks without ever giving the Timer callback a chance to execute: |
| 48 * | 38 * |
| 49 * Timer.run(() { print("executed"); }); // Will never be executed; | 39 * Timer.run(() { print("executed"); }); // Will never be executed; |
| 50 * foo() { | 40 * foo() { |
| 51 * asyncRun(foo); // Schedules [foo] in front of other events. | 41 * asyncRun(foo); // Schedules [foo] in front of other events. |
| 52 * } | 42 * } |
| 53 * main() { | 43 * main() { |
| 54 * foo(); | 44 * foo(); |
| 55 * } | 45 * } |
| 56 */ | 46 */ |
| 57 void runAsync(void callback()) { | 47 void runAsync(void callback()) { |
| 58 _Zone._current.runAsync(callback); | 48 // Optimizing a group of Timer.run callbacks to be executed in the |
| 49 // same Timer callback. |
| 50 _asyncCallbacks.add(callback); |
| 51 if (!_callbacksAreEnqueued) { |
| 52 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 53 _callbacksAreEnqueued = true; |
| 54 } |
| 59 } | 55 } |
| 60 | 56 |
| 61 class _AsyncRun { | 57 class _AsyncRun { |
| 62 /** Enqueues the given callback before any other event in the event-loop. */ | 58 /** Enqueues the given callback before any other event in the event-loop. */ |
| 63 external static void _enqueueImmediate(void callback()); | 59 external static void _enqueueImmediate(void callback()); |
| 64 } | 60 } |
| OLD | NEW |