OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.async; |
| 6 |
| 7 typedef void _AsyncCallback(); |
| 8 |
| 9 bool _callbacksAreEnqueued = false; |
| 10 List<_AsyncCallback> _asyncCallbacks = <_AsyncCallback>[]; |
| 11 |
| 12 void _asyncRunCallback() { |
| 13 // As long as we are iterating over the registered callbacks we don't |
| 14 // unset the [_callbacksAreEnqueued] boolean. |
| 15 while (!_asyncCallbacks.isEmpty) { |
| 16 List callbacks = _asyncCallbacks; |
| 17 // The callbacks we execute can register new callbacks. This means that |
| 18 // the for-loop below could grow the list if we don't replace it here. |
| 19 _asyncCallbacks = <_AsyncCallback>[]; |
| 20 for (int i = 0; i < callbacks.length; i++) { |
| 21 Function callback = callbacks[i]; |
| 22 callbacks[i] = null; |
| 23 try { |
| 24 callback(); |
| 25 } catch (e) { |
| 26 i++; // Skip current callback. |
| 27 List remainingCallbacks = callbacks.getRange(i, callbacks.length - i); |
| 28 List newCallbacks = _asyncCallbacks; |
| 29 _asyncCallbacks = <_AsyncCallback>[]; |
| 30 _asyncCallbacks.addAll(remainingCallbacks); |
| 31 _asyncCallbacks.addAll(newCallbacks); |
| 32 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 33 throw; |
| 34 } |
| 35 } |
| 36 } |
| 37 // Any new callback must register a callback function now. |
| 38 _callbacksAreEnqueued = false; |
| 39 } |
| 40 |
| 41 /** |
| 42 * Runs the given [callback] asynchronously. |
| 43 * |
| 44 * Callbacks registered through this function are always executed in order and |
| 45 * are guaranteed to run before other asynchronous events (like [Timer] events, |
| 46 * or DOM events). |
| 47 * |
| 48 * Warning: it is possible to starve the DOM by registering asynchronous |
| 49 * callbacks through this method. For example the following program will |
| 50 * run the callbacks without ever giving the Timer callback a chance to execute: |
| 51 * |
| 52 * Timer.run(() { print("executed"); }); // Will never be executed; |
| 53 * foo() { |
| 54 * asyncRun(foo); // Schedules [foo] in front of other events. |
| 55 * } |
| 56 * main() { |
| 57 * foo(); |
| 58 * } |
| 59 */ |
| 60 void runAsync(void callback()) { |
| 61 // Optimizing a group of Timer.run callbacks to be executed in the |
| 62 // same Timer callback. |
| 63 _asyncCallbacks.add(callback); |
| 64 if (!_callbacksAreEnqueued) { |
| 65 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
| 66 _callbacksAreEnqueued = true; |
| 67 } |
| 68 } |
| 69 |
| 70 class _AsyncRun { |
| 71 /** Enqueues the given callback before any other event in the event-loop. */ |
| 72 external static void _enqueueImmediate(void callback()); |
| 73 } |
OLD | NEW |