Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 /// A transformer that converts a broadcast stream into a single-subscription | 7 /// A transformer that converts a broadcast stream into a single-subscription |
| 8 /// stream. | 8 /// stream. |
| 9 /// | 9 /// |
| 10 /// This buffers the broadcast stream's events, which means that it starts | 10 /// This buffers the broadcast stream's events, which means that it starts |
| 11 /// listening to a stream as soon as it's bound. | 11 /// listening to a stream as soon as it's bound. |
| 12 /// | |
| 13 /// This also casts the source stream's events to type `T`. If the cast fails, | |
| 14 /// the result stream will emit a [CastError]. This behavior is deprecated, and | |
| 15 /// should not be relied upon. | |
| 12 class SingleSubscriptionTransformer<S, T> implements StreamTransformer<S, T> { | 16 class SingleSubscriptionTransformer<S, T> implements StreamTransformer<S, T> { |
| 13 const SingleSubscriptionTransformer(); | 17 const SingleSubscriptionTransformer(); |
| 14 | 18 |
| 15 Stream<T> bind(Stream<S> stream) { | 19 Stream<T> bind(Stream<S> stream) { |
| 16 var subscription; | 20 var subscription; |
| 17 var controller = new StreamController(sync: true, | 21 var controller = new StreamController<T>(sync: true, |
| 18 onCancel: () => subscription.cancel()); | 22 onCancel: () => subscription.cancel()); |
| 19 subscription = stream.listen(controller.add, | 23 subscription = stream.listen((value) { |
| 20 onError: controller.addError, onDone: controller.close); | 24 // TODO(nweiz): When we release a new major version, get rid of the second |
| 25 // type parameter and avoid this conversion. | |
|
Lasse Reichstein Nielsen
2016/03/29 21:53:59
The extra type parameter allows you to cast from o
nweiz
2016/03/30 00:57:19
I think having an explicit means of casting is bet
| |
| 26 try { | |
| 27 controller.add(value as T); | |
| 28 } on CastError catch (error, stackTrace) { | |
| 29 controller.addError(error, stackTrace); | |
| 30 } | |
| 31 }, onError: controller.addError, onDone: controller.close); | |
| 21 return controller.stream; | 32 return controller.stream; |
| 22 } | 33 } |
| 23 } | 34 } |
| OLD | NEW |