Add SingleSubscriptionTransformer.
This transforms a broadcast stream to a single-subscription stream,
adding internal buffering.
R=lrn@google.com
Review URL: https://codereview.chromium.org//1584023002 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8cb7880..d7142da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 1.7.0
+
+- Added `SingleSubscriptionTransformer`, a `StreamTransformer` that converts a
+ broadcast stream into a single-subscription stream.
+
## 1.6.0
- Added `CancelableOperation.valueOrCancellation()`, which allows users to be
diff --git a/lib/async.dart b/lib/async.dart
index 8f14bac..ab630dd 100644
--- a/lib/async.dart
+++ b/lib/async.dart
@@ -17,6 +17,7 @@
export "src/lazy_stream.dart";
export "src/restartable_timer.dart";
export "src/result_future.dart";
+export "src/single_subscription_transformer.dart";
export "src/stream_completer.dart";
export "src/stream_group.dart";
export "src/stream_queue.dart";
diff --git a/lib/src/single_subscription_transformer.dart b/lib/src/single_subscription_transformer.dart
new file mode 100644
index 0000000..28fd512
--- /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;
+ }
+}
diff --git a/pubspec.yaml b/pubspec.yaml
index e13f6ab..423f3ce 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: async
-version: 1.6.0
+version: 1.7.0
author: Dart Team <misc@dartlang.org>
description: Utility functions and classes related to the 'dart:async' library.
homepage: https://www.github.com/dart-lang/async
diff --git a/test/single_subscription_transformer_test.dart b/test/single_subscription_transformer_test.dart
new file mode 100644
index 0000000..f21d4e9
--- /dev/null
+++ b/test/single_subscription_transformer_test.dart
@@ -0,0 +1,50 @@
+// 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.
+
+import 'dart:async';
+
+import 'package:async/async.dart';
+import 'package:test/test.dart';
+
+import 'utils.dart';
+
+void main() {
+ test("buffers events as soon as it's bound", () async {
+ var controller = new StreamController.broadcast();
+ var stream = controller.stream.transform(
+ const SingleSubscriptionTransformer());
+
+ // Add events before [stream] has a listener to be sure it buffers them.
+ controller.add(1);
+ controller.add(2);
+ await flushMicrotasks();
+
+ expect(stream.toList(), completion(equals([1, 2, 3, 4])));
+ await flushMicrotasks();
+
+ controller.add(3);
+ controller.add(4);
+ controller.close();
+ });
+
+ test("cancels the subscription to the broadcast stream when it's canceled",
+ () async {
+ var canceled = false;
+ var controller = new StreamController.broadcast(onCancel: () {
+ canceled = true;
+ });
+ var stream = controller.stream.transform(
+ const SingleSubscriptionTransformer());
+ await flushMicrotasks();
+ expect(canceled, isFalse);
+
+ var subscription = stream.listen(null);
+ await flushMicrotasks();
+ expect(canceled, isFalse);
+
+ subscription.cancel();
+ await flushMicrotasks();
+ expect(canceled, isTrue);
+ });
+}