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

Unified 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: Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/async/async_sources.gypi ('k') | tests/lib/async/run_async2_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/event_loop.dart
diff --git a/sdk/lib/async/event_loop.dart b/sdk/lib/async/event_loop.dart
new file mode 100644
index 0000000000000000000000000000000000000000..9bd552631c7bbfe62458ab188cd06b1468cc47eb
--- /dev/null
+++ b/sdk/lib/async/event_loop.dart
@@ -0,0 +1,54 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of dart.async;
+
+typedef void _AsyncCallback();
+
+bool _callbacksAreEnqueued = false;
+List<_AsyncCallback> _asyncCallbacks = <_AsyncCallback>[];
+
+void _asyncRunCallback() {
+ // As long as we are iterating over the registered callbacks we don't
+ // unset the [_callbacksAreEnqueued] boolean.
+ while (!_asyncCallbacks.isEmpty) {
+ List callbacks = _asyncCallbacks;
+ // Create a new list, to avoid that the we grow too much if the callbacks
+ // add new callbacks themselves.
+ _asyncCallbacks = <_AsyncCallback>[];
+ for (int i = 0; i < callbacks.length; i++) {
+ Function callback = callbacks[i];
+ callbacks[i] = null;
+ try {
+ callback();
+ } catch (e) {
+ List remainingCallbacks =
+ _asyncCallbacks.getRange(i + 1, _asyncCallbacks.length - i + 1);
+ List newCallbacks = _asyncCallbacks;
+ _asyncCallbacks = [];
+ _asyncCallbacks.addAll(remainingCallbacks);
+ _asyncCallbacks.addAll(newCallbacks);
+ _AsyncRun._enqueueImmediate(_asyncRunCallback);
+ throw;
+ }
+ }
+ }
+ // Any new callback must register a callback function now.
+ _callbacksAreEnqueued = false;
+}
+
+void runAsync(void callback()) {
+ // Optimizing a group of Timer.run callbacks to be executed in the
+ // same Timer callback.
+ _asyncCallbacks.add(callback);
+ if (!_callbacksAreEnqueued) {
+ _AsyncRun._enqueueImmediate(_asyncRunCallback);
+ _callbacksAreEnqueued = true;
+ }
+}
+
+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
+ /** Enqueues the given callback before any other event in the event-loop. */
+ external static void _enqueueImmediate(void callback());
+}
« no previous file with comments | « sdk/lib/async/async_sources.gypi ('k') | tests/lib/async/run_async2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698