Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1204)

Unified Diff: lib/src/single_subscription_transformer.dart

Issue 1584023002: Add SingleSubscriptionTransformer. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Update pubspec Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
+ }
+}
« no previous file with comments | « lib/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698