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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 stream.listen( | 189 stream.listen( |
190 controller.add, | 190 controller.add, |
191 onError: controller.addError, | 191 onError: controller.addError, |
192 onDone: controller.close); | 192 onDone: controller.close); |
193 }).catchError((e, stackTrace) { | 193 }).catchError((e, stackTrace) { |
194 controller.addError(e, stackTrace); | 194 controller.addError(e, stackTrace); |
195 controller.close(); | 195 controller.close(); |
196 }); | 196 }); |
197 return controller.stream; | 197 return controller.stream; |
198 } | 198 } |
| 199 |
| 200 /// Returns a [Stream] that will emit the same values as the stream returned by |
| 201 /// [callback]. |
| 202 /// |
| 203 /// [callback] will only be called when the returned [Stream] gets a subscriber. |
| 204 Stream callbackStream(Stream callback()) { |
| 205 var subscription; |
| 206 var controller; |
| 207 controller = new StreamController(onListen: () { |
| 208 subscription = callback().listen(controller.add, |
| 209 onError: controller.addError, |
| 210 onDone: controller.close); |
| 211 }, |
| 212 onCancel: () => subscription.cancel(), |
| 213 onPause: () => subscription.pause(), |
| 214 onResume: () => subscription.resume(), |
| 215 sync: true); |
| 216 return controller.stream; |
| 217 } |
OLD | NEW |