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

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

Issue 26993002: Add factory-method for scheduleMicrotask, as well as a (failing) test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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
« no previous file with comments | « sdk/lib/async/event_loop.dart ('k') | tests/lib/async/schedule_microtask_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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._scheduleImmediate(_asyncRunCallback);
21 rethrow; 21 rethrow;
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) { 28 void _scheduleAsyncCallback(callback) {
29 // Optimizing a group of Timer.run callbacks to be executed in the 29 // Optimizing a group of Timer.run callbacks to be executed in the
30 // same Timer callback. 30 // same Timer callback.
31 _asyncCallbacks.add(callback); 31 _asyncCallbacks.add(callback);
32 if (!_callbacksAreEnqueued) { 32 if (!_callbacksAreEnqueued) {
33 _AsyncRun._enqueueImmediate(_asyncRunCallback); 33 _AsyncRun._scheduleImmediate(_asyncRunCallback);
34 _callbacksAreEnqueued = true; 34 _callbacksAreEnqueued = true;
35 } 35 }
36 } 36 }
37 37
38 /** 38 /**
39 * Runs the given [callback] asynchronously. 39 * Runs the given [callback] asynchronously.
40 * 40 *
41 * Callbacks registered through this function are always executed in order and 41 * Callbacks registered through this function are always executed in order and
42 * are guaranteed to run before other asynchronous events (like [Timer] events, 42 * are guaranteed to run before other asynchronous events (like [Timer] events,
43 * or DOM events). 43 * or DOM events).
(...skipping 23 matching lines...) Expand all
67 67
68 /** 68 /**
69 * *DEPRECATED*. Use [scheduleMicrotask] instead. 69 * *DEPRECATED*. Use [scheduleMicrotask] instead.
70 */ 70 */
71 @deprecated 71 @deprecated
72 void runAsync(void callback()) { 72 void runAsync(void callback()) {
73 scheduleMicrotask(callback); 73 scheduleMicrotask(callback);
74 } 74 }
75 75
76 class _AsyncRun { 76 class _AsyncRun {
77 /** Enqueues the given callback before any other event in the event-loop. */ 77 /** Schedule the given callback before any other event in the event-loop. */
78 external static void _enqueueImmediate(void callback()); 78 external static void _scheduleImmediate(void callback());
79 } 79 }
OLDNEW
« no previous file with comments | « sdk/lib/async/event_loop.dart ('k') | tests/lib/async/schedule_microtask_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698