| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.utils; | 5 library barback.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// A pair of values. | 9 /// A pair of values. |
| 10 class Pair<E, F> { | 10 class Pair<E, F> { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // invoking this method. | 174 // invoking this method. |
| 175 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 175 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 /// Like `new Future`, but avoids issue 11911 by using `new Future.value` under | 178 /// Like `new Future`, but avoids issue 11911 by using `new Future.value` under |
| 179 /// the covers. | 179 /// the covers. |
| 180 // TODO(jmesserly): doc comment changed to due 14601. | 180 // TODO(jmesserly): doc comment changed to due 14601. |
| 181 Future newFuture(callback()) => new Future.value().then((_) => callback()); | 181 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| 182 | 182 |
| 183 /// Returns a buffered stream that will emit the same values as the stream | 183 /// Returns a buffered stream that will emit the same values as the stream |
| 184 /// returned by [future] once [future] completes. If [future] completes to an | 184 /// returned by [future] once [future] completes. |
| 185 /// error, the return value will emit that error and then close. | 185 /// |
| 186 Stream futureStream(Future<Stream> future) { | 186 /// If [future] completes to an error, the return value will emit that error and |
| 187 var controller = new StreamController(sync: true); | 187 /// then close. |
| 188 future.then((stream) { | 188 /// |
| 189 stream.listen( | 189 /// If [broadcast] is true, a broadcast stream is returned. This assumes that |
| 190 controller.add, | 190 /// the stream returned by [future] will be a broadcast stream as well. |
| 191 onError: controller.addError, | 191 /// [broadcast] defaults to false. |
| 192 onDone: controller.close); | 192 Stream futureStream(Future<Stream> future, {bool broadcast: false}) { |
| 193 }).catchError((e, stackTrace) { | 193 var subscription; |
| 194 var controller; |
| 195 |
| 196 future = future.catchError((e, stackTrace) { |
| 197 if (controller == null) return; |
| 194 controller.addError(e, stackTrace); | 198 controller.addError(e, stackTrace); |
| 195 controller.close(); | 199 controller.close(); |
| 200 controller = null; |
| 196 }); | 201 }); |
| 202 |
| 203 onListen() { |
| 204 future.then((stream) { |
| 205 if (controller == null) return; |
| 206 subscription = stream.listen( |
| 207 controller.add, |
| 208 onError: controller.addError, |
| 209 onDone: controller.close); |
| 210 }); |
| 211 } |
| 212 |
| 213 onCancel() { |
| 214 if (subscription != null) subscription.cancel(); |
| 215 subscription = null; |
| 216 controller = null; |
| 217 } |
| 218 |
| 219 if (broadcast) { |
| 220 controller = new StreamController.broadcast( |
| 221 sync: true, onListen: onListen, onCancel: onCancel); |
| 222 } else { |
| 223 controller = new StreamController( |
| 224 sync: true, onListen: onListen, onCancel: onCancel); |
| 225 } |
| 197 return controller.stream; | 226 return controller.stream; |
| 198 } | 227 } |
| 199 | 228 |
| 200 /// Returns a [Stream] that will emit the same values as the stream returned by | 229 /// Returns a [Stream] that will emit the same values as the stream returned by |
| 201 /// [callback]. | 230 /// [callback]. |
| 202 /// | 231 /// |
| 203 /// [callback] will only be called when the returned [Stream] gets a subscriber. | 232 /// [callback] will only be called when the returned [Stream] gets a subscriber. |
| 204 Stream callbackStream(Stream callback()) { | 233 Stream callbackStream(Stream callback()) { |
| 205 var subscription; | 234 var subscription; |
| 206 var controller; | 235 var controller; |
| 207 controller = new StreamController(onListen: () { | 236 controller = new StreamController(onListen: () { |
| 208 subscription = callback().listen(controller.add, | 237 subscription = callback().listen(controller.add, |
| 209 onError: controller.addError, | 238 onError: controller.addError, |
| 210 onDone: controller.close); | 239 onDone: controller.close); |
| 211 }, | 240 }, |
| 212 onCancel: () => subscription.cancel(), | 241 onCancel: () => subscription.cancel(), |
| 213 onPause: () => subscription.pause(), | 242 onPause: () => subscription.pause(), |
| 214 onResume: () => subscription.resume(), | 243 onResume: () => subscription.resume(), |
| 215 sync: true); | 244 sync: true); |
| 216 return controller.stream; | 245 return controller.stream; |
| 217 } | 246 } |
| OLD | NEW |