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>[]; |
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 List callbacks = _asyncCallbacks; | 16 List callbacks = _asyncCallbacks; |
17 // The callbacks we execute can register new callbacks. This means that | 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. | 18 // the for-loop below could grow the list if we don't replace it here. |
19 _asyncCallbacks = <_AsyncCallback>[]; | 19 _asyncCallbacks = <_AsyncCallback>[]; |
20 for (int i = 0; i < callbacks.length; i++) { | 20 for (int i = 0; i < callbacks.length; i++) { |
21 Function callback = callbacks[i]; | 21 Function callback = callbacks[i]; |
22 callbacks[i] = null; | 22 callbacks[i] = null; |
23 try { | 23 try { |
24 callback(); | 24 callback(); |
25 } catch (e) { | 25 } catch (e) { |
26 i++; // Skip current callback. | 26 i++; // Skip current callback. |
27 List remainingCallbacks = callbacks.getRange(i, callbacks.length - i); | 27 List remainingCallbacks = callbacks.sublist(i); |
28 List newCallbacks = _asyncCallbacks; | 28 List newCallbacks = _asyncCallbacks; |
29 _asyncCallbacks = <_AsyncCallback>[]; | 29 _asyncCallbacks = <_AsyncCallback>[]; |
30 _asyncCallbacks.addAll(remainingCallbacks); | 30 _asyncCallbacks.addAll(remainingCallbacks); |
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. |
(...skipping 26 matching lines...) Expand all Loading... |
64 if (!_callbacksAreEnqueued) { | 64 if (!_callbacksAreEnqueued) { |
65 _AsyncRun._enqueueImmediate(_asyncRunCallback); | 65 _AsyncRun._enqueueImmediate(_asyncRunCallback); |
66 _callbacksAreEnqueued = true; | 66 _callbacksAreEnqueued = true; |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 class _AsyncRun { | 70 class _AsyncRun { |
71 /** Enqueues the given callback before any other event in the event-loop. */ | 71 /** Enqueues the given callback before any other event in the event-loop. */ |
72 external static void _enqueueImmediate(void callback()); | 72 external static void _enqueueImmediate(void callback()); |
73 } | 73 } |
OLD | NEW |