OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 /** |
| 6 * A [Runner] runs a function, potentially in a different scope |
| 7 * or even isolate. |
| 8 */ |
| 9 library dart.pkg.isolate.runner; |
| 10 |
| 11 import "dart:async" show Future; |
| 12 |
| 13 /** |
| 14 * Calls a function with an argument. |
| 15 * |
| 16 * The function can be run in a different place from where the `Runner` |
| 17 * resides, e.g., in a different isolate. |
| 18 */ |
| 19 class Runner { |
| 20 /** |
| 21 * Request that [function] be called with the provided arguments. |
| 22 * |
| 23 * The arguments will be applied to the function in the same way as by |
| 24 * [Function.apply], but it may happen in a diffent isolate or setting. |
| 25 * |
| 26 * It's necessary that the function can be sent through a [SendPort] |
| 27 * if the call is performed in another isolate. |
| 28 * That means the other isolate should be created using [Isolate.spawn] |
| 29 * so that it is running the same code as the sending isoalte, |
| 30 * and the function must be a static or top-level function. |
| 31 * |
| 32 * Waits for the result of the call, and completes the returned future |
| 33 * with the result, whether it's a value or an error. |
| 34 * |
| 35 * If the returned future does not complete before `timeLimit` has passed, |
| 36 * the [onTimeout] action is executed instead, and its result (whether it |
| 37 * returns or throws) is used as the result of the returned future. |
| 38 * |
| 39 * If `onTimeout` is omitted, a timeout will cause the returned future to |
| 40 * complete with a [TimeoutException]. |
| 41 * |
| 42 * The default implementation runs the function in the current isolate. |
| 43 */ |
| 44 Future run(function(argument), Object argument, |
| 45 {Duration timeout, onTimeout()}) { |
| 46 Future result = new Future.sync(() => function(argument)); |
| 47 if (timeout != null) { |
| 48 result = result.timeout(timeout, onTimeout: onTimeout); |
| 49 } |
| 50 return result; |
| 51 } |
| 52 |
| 53 /** |
| 54 * Stop the runner. |
| 55 * |
| 56 * If the runner has allocated resources, e.g., an isolate, it should |
| 57 * be released. No further calls to [run] should be made after calling |
| 58 * stop. |
| 59 */ |
| 60 Future close() => new Future.value(); |
| 61 } |
OLD | NEW |