Chromium Code Reviews| 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 /// |
| 11 /// There are different functions that offer different ways to | 11 /// There are different functions that offer different ways to |
| 12 /// handle the incoming message. | 12 /// handle the incoming message. |
| 13 /// | 13 /// |
| 14 /// The simplest function, [singleCallbackPort], takes a callback | 14 /// The simplest function, [singleCallbackPort], takes a callback |
| 15 /// and returns a port, and then calls the callback for the first | 15 /// and returns a port, and then calls the callback for the first |
| 16 /// message sent on the port. | 16 /// message sent on the port. |
| 17 /// | 17 /// |
| 18 /// Other functions intercept the returned value and either | 18 /// Other functions intercept the returned value and either |
| 19 /// does something with it, or puts it into a [Future] or [Completer]. | 19 /// does something with it, or puts it into a [Future] or [Completer]. |
| 20 library dart.pkg.isolate.ports; | 20 library isolate.ports; |
| 21 | 21 |
| 22 import "dart:async"; | |
| 22 import "dart:isolate"; | 23 import "dart:isolate"; |
| 23 import "dart:async"; | 24 |
| 24 import "src/lists.dart"; | 25 import "src/lists.dart"; |
| 25 | 26 |
| 26 /// Create a [SendPort] that accepts only one message. | 27 /// Create a [SendPort] that accepts only one message. |
| 27 /// | 28 /// |
| 28 /// The [callback] function is called once, with the first message | 29 /// The [callback] function is called once, with the first message |
| 29 /// received by the receive port. | 30 /// received by the receive port. |
| 30 /// All futher messages are ignored. | 31 /// All futher messages are ignored. |
| 31 /// | 32 /// |
| 32 /// 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 |
| 33 /// long it can take before the message is received. If a | 34 /// long it can take before the message is received. If a |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 _completer = new Completer.sync(), | 293 _completer = new Completer.sync(), |
| 293 _callback = callback, | 294 _callback = callback, |
| 294 _zone = Zone.current { | 295 _zone = Zone.current { |
| 295 _receivePort.handler = _handleResponse; | 296 _receivePort.handler = _handleResponse; |
| 296 if (timeout != null) { | 297 if (timeout != null) { |
| 297 _timer = new Timer(timeout, () { | 298 _timer = new Timer(timeout, () { |
| 298 // Executed as a timer event. | 299 // Executed as a timer event. |
| 299 _receivePort.close(); | 300 _receivePort.close(); |
| 300 if (!_completer.isCompleted) { | 301 if (!_completer.isCompleted) { |
| 301 if (throwOnTimeout) { | 302 if (throwOnTimeout) { |
| 302 _completer.completeError(new TimeoutException(timeout)); | 303 _completer.completeError( |
| 304 new TimeoutException('Timeout waiting for response, timeout')); | |
|
Lasse Reichstein Nielsen
2015/03/24 10:35:38
I think "timeout" should be the second argument, n
kevmoo
2015/03/24 13:21:19
Done.
| |
| 303 } else if (onTimeout != null) { | 305 } else if (onTimeout != null) { |
| 304 _completer.complete(new Future.sync(onTimeout)); | 306 _completer.complete(new Future.sync(onTimeout)); |
| 305 } else { | 307 } else { |
| 306 _completer.complete(timeoutValue); | 308 _completer.complete(timeoutValue); |
| 307 } | 309 } |
| 308 } | 310 } |
| 309 }); | 311 }); |
| 310 } | 312 } |
| 311 } | 313 } |
| 312 | 314 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 // 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 |
| 351 // to be propagated to the original completer. If that completer was | 353 // to be propagated to the original completer. If that completer was |
| 352 // 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 |
| 353 // would become uncaught. | 355 // would become uncaught. |
| 354 _zone.run(() { | 356 _zone.run(() { |
| 355 _completer.complete(new Future.sync(() => _callback(v))); | 357 _completer.complete(new Future.sync(() => _callback(v))); |
| 356 }); | 358 }); |
| 357 } | 359 } |
| 358 } | 360 } |
| 359 } | 361 } |
| OLD | NEW |