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 // Create a new list, to avoid that the we grow too much if the callbacks | |
18 // add new callbacks themselves. | |
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 List remainingCallbacks = | |
27 _asyncCallbacks.getRange(i + 1, _asyncCallbacks.length - i + 1); | |
28 List newCallbacks = _asyncCallbacks; | |
29 _asyncCallbacks = []; | |
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 void runAsync(void 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 | |
51 class _AsyncRun { | |
floitsch
2013/03/01 14:08:21
Without the class the VM won't find the external f
floitsch
2013/03/08 20:04:40
Debugged with Ivan. For now the class is necessary
| |
52 /** Enqueues the given callback before any other event in the event-loop. */ | |
53 external static void _enqueueImmediate(void callback()); | |
54 } | |
OLD | NEW |