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