Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: sdk/lib/async/event_loop.dart

Issue 12380060: Add first version of runAsync. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update documentation. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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>[];
Lasse Reichstein Nielsen 2013/03/04 09:23:24 Or use a ListQueue for the callbacks queue. Then y
floitsch 2013/03/04 17:26:05 The async library doesn't include the collection l
20 for (int i = 0; i < callbacks.length; i++) {
Lasse Reichstein Nielsen 2013/03/04 09:23:24 Should we avoid running too much in the same event
floitsch 2013/03/04 17:26:05 No. runAsync guarantees to run before all events.
21 Function callback = callbacks[i];
22 callbacks[i] = null;
23 try {
24 callback();
25 } catch (e) {
Lasse Reichstein Nielsen 2013/03/04 09:23:24 If you keep using the same ListQueue, you don't ha
floitsch 2013/03/04 17:26:05 I considered ListQueues, but decided against them
26 List remainingCallbacks =
27 _asyncCallbacks.getRange(i + 1, _asyncCallbacks.length - i + 1);
28 List newCallbacks = _asyncCallbacks;
29 _asyncCallbacks = [];
Lasse Reichstein Nielsen 2013/03/04 09:23:24 Missing type parameter.
floitsch 2013/03/04 17:26:05 Done.
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 register 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());
Lasse Reichstein Nielsen 2013/03/04 09:23:24 Why this class? You should be able to patch top-le
floitsch 2013/03/04 17:26:05 I tried, but I couldn't get it to work.
73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698