Index: lib/src/single_subscription_transformer.dart |
diff --git a/lib/src/single_subscription_transformer.dart b/lib/src/single_subscription_transformer.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..28fd512d38c5addfd13de136d462a101207a3552 |
--- /dev/null |
+++ b/lib/src/single_subscription_transformer.dart |
@@ -0,0 +1,25 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library async.single_subscription_transformer; |
+ |
+import 'dart:async'; |
+ |
+/// A transformer that converts a broadcast stream into a single-subscription |
+/// stream. |
+/// |
+/// This buffers the broadcast stream's events, which means that it starts |
+/// listening to a stream as soon as it's bound. |
+class SingleSubscriptionTransformer<S, T> implements StreamTransformer<S, T> { |
+ const SingleSubscriptionTransformer(); |
+ |
+ Stream<T> bind(Stream<S> stream) { |
+ var subscription; |
+ var controller = new StreamController(sync: true, |
+ onCancel: () => subscription.cancel()); |
+ subscription = stream.listen(controller.add, |
+ onError: controller.addError, onDone: controller.close); |
+ return controller.stream; |
+ } |
+} |