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

Unified Diff: lib/runner.dart

Issue 928663003: Add IsolateRunner as a helper around Isolate. (Closed) Base URL: https://github.com/dart-lang/isolate.git@master
Patch Set: Add .status. Created 5 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 | « lib/registry.dart ('k') | lib/src/errors.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/runner.dart
diff --git a/lib/runner.dart b/lib/runner.dart
new file mode 100644
index 0000000000000000000000000000000000000000..cd26d5af981f3761562db0ac9ef63f118ab02911
--- /dev/null
+++ b/lib/runner.dart
@@ -0,0 +1,61 @@
+// Copyright (c) 2015, 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.
+
+/**
+ * A [Runner] runs a function, potentially in a different scope
+ * or even isolate.
+ */
+library dart.pkg.isolate.runner;
+
+import "dart:async" show Future;
+
+/**
+ * Calls a function with an argument.
+ *
+ * The function can be run in a different place from where the `Runner`
+ * resides, e.g., in a different isolate.
+ */
+class Runner {
+ /**
+ * Request that [function] be called with the provided arguments.
+ *
+ * The arguments will be applied to the function in the same way as by
+ * [Function.apply], but it may happen in a diffent isolate or setting.
+ *
+ * It's necessary that the function can be sent through a [SendPort]
+ * if the call is performed in another isolate.
+ * That means the other isolate should be created using [Isolate.spawn]
+ * so that it is running the same code as the sending isoalte,
+ * and the function must be a static or top-level function.
+ *
+ * Waits for the result of the call, and completes the returned future
+ * with the result, whether it's a value or an error.
+ *
+ * If the returned future does not complete before `timeLimit` has passed,
+ * the [onTimeout] action is executed instead, and its result (whether it
+ * returns or throws) is used as the result of the returned future.
+ *
+ * If `onTimeout` is omitted, a timeout will cause the returned future to
+ * complete with a [TimeoutException].
+ *
+ * The default implementation runs the function in the current isolate.
+ */
+ Future run(function(argument), Object argument,
+ {Duration timeout, onTimeout()}) {
+ Future result = new Future.sync(() => function(argument));
+ if (timeout != null) {
+ result = result.timeout(timeout, onTimeout: onTimeout);
+ }
+ return result;
+ }
+
+ /**
+ * Stop the runner.
+ *
+ * If the runner has allocated resources, e.g., an isolate, it should
+ * be released. No further calls to [run] should be made after calling
+ * stop.
+ */
+ Future close() => new Future.value();
+}
« no previous file with comments | « lib/registry.dart ('k') | lib/src/errors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698