OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// Utility functions for setting up ports and sending data. | 5 /// Utility functions for setting up ports and sending data. |
6 /// | 6 /// |
7 /// This library contains a number of functions that handle the | 7 /// This library contains a number of functions that handle the |
8 /// boiler-plate of setting up a receive port and receiving a | 8 /// boiler-plate of setting up a receive port and receiving a |
9 /// single message on the port. | 9 /// single message on the port. |
10 /// | 10 /// |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 import "dart:async"; | 22 import "dart:async"; |
23 import "dart:isolate"; | 23 import "dart:isolate"; |
24 | 24 |
25 import "src/lists.dart"; | 25 import "src/lists.dart"; |
26 | 26 |
27 /// Create a [SendPort] that accepts only one message. | 27 /// Create a [SendPort] that accepts only one message. |
28 /// | 28 /// |
29 /// The [callback] function is called once, with the first message | 29 /// The [callback] function is called once, with the first message |
30 /// received by the receive port. | 30 /// received by the receive port. |
31 /// All futher messages are ignored. | 31 /// All further messages are ignored. |
32 /// | 32 /// |
33 /// If [timeout] is supplied, it is used as a limit on how | 33 /// If [timeout] is supplied, it is used as a limit on how |
34 /// long it can take before the message is received. If a | 34 /// long it can take before the message is received. If a |
35 /// message isn't received in time, the `callback` function | 35 /// message isn't received in time, the `callback` function |
36 /// is called once with the [timeoutValue] instead. | 36 /// is called once with the [timeoutValue] instead. |
37 /// | 37 /// |
38 /// Returns the `SendPort` expecting the single message. | 38 /// Returns the `SendPort` expecting the single message. |
39 /// | 39 /// |
40 /// Equivalent to: | 40 /// Equivalent to: |
41 /// | 41 /// |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 if (response.length == 2) { | 238 if (response.length == 2) { |
239 var error = new RemoteError(response[0], response[1]); | 239 var error = new RemoteError(response[0], response[1]); |
240 completer.completeError(error, error.stackTrace); | 240 completer.completeError(error, error.stackTrace); |
241 } else { | 241 } else { |
242 var result = response[0]; | 242 var result = response[0]; |
243 completer.complete(result); | 243 completer.complete(result); |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 | 247 |
248 /// Convertes a received message created by [sendFutureResult] to a future | 248 /// Converts a received message created by [sendFutureResult] to a future |
249 /// result. | 249 /// result. |
250 /// | 250 /// |
251 /// The [response] must be a message on the format sent by [sendFutureResult]. | 251 /// The [response] must be a message on the format sent by [sendFutureResult]. |
252 Future receiveFutureResult(var response) { | 252 Future receiveFutureResult(var response) { |
253 if (response.length == 2) { | 253 if (response.length == 2) { |
254 var error = new RemoteError(response[0], response[1]); | 254 var error = new RemoteError(response[0], response[1]); |
255 return new Future.error(error, error.stackTrace); | 255 return new Future.error(error, error.stackTrace); |
256 } | 256 } |
257 var result = response[0]; | 257 var result = response[0]; |
258 return new Future.value(result); | 258 return new Future.value(result); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 // what the user expects, and because it may create an error that needs | 352 // what the user expects, and because it may create an error that needs |
353 // to be propagated to the original completer. If that completer was | 353 // to be propagated to the original completer. If that completer was |
354 // created in a different error zone, an error from the root zone | 354 // created in a different error zone, an error from the root zone |
355 // would become uncaught. | 355 // would become uncaught. |
356 _zone.run(() { | 356 _zone.run(() { |
357 _completer.complete(new Future.sync(() => _callback(v))); | 357 _completer.complete(new Future.sync(() => _callback(v))); |
358 }); | 358 }); |
359 } | 359 } |
360 } | 360 } |
361 } | 361 } |
OLD | NEW |