| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Functions go in this file as opposed to lib/src/utils.dart if they need to | 5 /// Functions go in this file as opposed to lib/src/utils.dart if they need to |
| 6 /// be accessible to the transformer-loading isolate. | 6 /// be accessible to the transformer-loading isolate. |
| 7 import 'dart:async'; | |
| 8 | 7 |
| 9 /// A regular expression to match the exception prefix that some exceptions' | 8 /// A regular expression to match the exception prefix that some exceptions' |
| 10 /// [Object.toString] values contain. | 9 /// [Object.toString] values contain. |
| 11 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); | 10 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
| 12 | 11 |
| 13 /// Get a string description of an exception. | 12 /// Get a string description of an exception. |
| 14 /// | 13 /// |
| 15 /// Many exceptions include the exception class name at the beginning of their | 14 /// Many exceptions include the exception class name at the beginning of their |
| 16 /// [toString], so we remove that if it exists. | 15 /// [toString], so we remove that if it exists. |
| 17 String getErrorMessage(error) => | 16 String getErrorMessage(error) => |
| 18 error.toString().replaceFirst(_exceptionPrefix, ''); | 17 error.toString().replaceFirst(_exceptionPrefix, ''); |
| 19 | |
| 20 /// Returns a buffered stream that will emit the same values as the stream | |
| 21 /// returned by [future] once [future] completes. | |
| 22 /// | |
| 23 /// If [future] completes to an error, the return value will emit that error and | |
| 24 /// then close. | |
| 25 /// | |
| 26 /// If [broadcast] is true, a broadcast stream is returned. This assumes that | |
| 27 /// the stream returned by [future] will be a broadcast stream as well. | |
| 28 /// [broadcast] defaults to false. | |
| 29 Stream futureStream(Future<Stream> future, {bool broadcast: false}) { | |
| 30 var subscription; | |
| 31 var controller; | |
| 32 | |
| 33 future = future.catchError((e, stackTrace) { | |
| 34 // Since [controller] is synchronous, it's likely that emitting an error | |
| 35 // will cause it to be cancelled before we call close. | |
| 36 if (controller != null) controller.addError(e, stackTrace); | |
| 37 if (controller != null) controller.close(); | |
| 38 controller = null; | |
| 39 }); | |
| 40 | |
| 41 onListen() { | |
| 42 future.then((stream) { | |
| 43 if (controller == null) return; | |
| 44 subscription = stream.listen( | |
| 45 controller.add, | |
| 46 onError: controller.addError, | |
| 47 onDone: controller.close); | |
| 48 }); | |
| 49 } | |
| 50 | |
| 51 onCancel() { | |
| 52 if (subscription != null) subscription.cancel(); | |
| 53 subscription = null; | |
| 54 controller = null; | |
| 55 } | |
| 56 | |
| 57 if (broadcast) { | |
| 58 controller = new StreamController.broadcast( | |
| 59 sync: true, onListen: onListen, onCancel: onCancel); | |
| 60 } else { | |
| 61 controller = new StreamController( | |
| 62 sync: true, onListen: onListen, onCancel: onCancel); | |
| 63 } | |
| 64 return controller.stream; | |
| 65 } | |
| 66 | |
| 67 /// Returns a [Stream] that will emit the same values as the stream returned by | |
| 68 /// [callback]. | |
| 69 /// | |
| 70 /// [callback] will only be called when the returned [Stream] gets a subscriber. | |
| 71 Stream callbackStream(Stream callback()) { | |
| 72 var subscription; | |
| 73 var controller; | |
| 74 controller = new StreamController(onListen: () { | |
| 75 subscription = callback().listen(controller.add, | |
| 76 onError: controller.addError, | |
| 77 onDone: controller.close); | |
| 78 }, | |
| 79 onCancel: () => subscription.cancel(), | |
| 80 onPause: () => subscription.pause(), | |
| 81 onResume: () => subscription.resume(), | |
| 82 sync: true); | |
| 83 return controller.stream; | |
| 84 } | |
| OLD | NEW |