Add `chainTransformers` and `map` utilities (dart-lang/stream_transform#47)
These are a bit different than others in this package since we would
generally not expect to see them directly inlined to a call to
`transform`, but are instead useful when dealing with variable or
arguments of type `StreamTransformer`, for instance `StreamSinkTransformer.fromStreamTransformer`.
diff --git a/pkgs/stream_transform/CHANGELOG.md b/pkgs/stream_transform/CHANGELOG.md
index 0e0cfaa..ca056d4 100644
--- a/pkgs/stream_transform/CHANGELOG.md
+++ b/pkgs/stream_transform/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.0.12
+
+- Add `chainTransformers` and `map` for use cases where `StreamTransformer`
+ instances are stored as variables or passed to methods other than `transform`.
+
## 0.0.11
- Renamed `concat` as `followedBy` to match the naming of `Iterable.followedBy`.
diff --git a/pkgs/stream_transform/lib/src/async_map_buffer.dart b/pkgs/stream_transform/lib/src/async_map_buffer.dart
index f2fd2a4..72190d2 100644
--- a/pkgs/stream_transform/lib/src/async_map_buffer.dart
+++ b/pkgs/stream_transform/lib/src/async_map_buffer.dart
@@ -4,8 +4,8 @@
import 'dart:async';
-import 'bind.dart';
import 'buffer.dart';
+import 'chain_transformers.dart';
import 'from_handlers.dart';
/// Like [Stream.asyncMap] but events are buffered until previous events have
@@ -31,9 +31,8 @@
var workFinished = new StreamController();
// Let the first event through.
workFinished.add(null);
- return fromBind((values) => values
- .transform(buffer(workFinished.stream))
- .transform(_asyncMapThen(convert, workFinished.add)));
+ return chainTransformers(
+ buffer(workFinished.stream), _asyncMapThen(convert, workFinished.add));
}
/// Like [Stream.asyncMap] but the [convert] is only called once per event,
diff --git a/pkgs/stream_transform/lib/src/chain_transformers.dart b/pkgs/stream_transform/lib/src/chain_transformers.dart
new file mode 100644
index 0000000..50d8be4
--- /dev/null
+++ b/pkgs/stream_transform/lib/src/chain_transformers.dart
@@ -0,0 +1,24 @@
+// Copyright (c) 2018, 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 'bind.dart';
+
+/// Combines two transformers into one.
+///
+/// This is most useful to keep a reference to the combination and use it in
+/// multiple places or give it a descriptive name. For inline uses the directly
+/// chained calls to `.transform` should be preferred.
+///
+/// For example:
+///
+/// ```
+/// /// values.transform(splitDecoded) is identical to
+/// /// values.transform(utf8.decoder).transform(const LineSplitter())
+/// final splitDecoded = chainTransformers(utf8.decoder, const LineSplitter());
+/// ```
+StreamTransformer<S, T> chainTransformers<S, I, T>(
+ StreamTransformer<S, I> first, StreamTransformer<I, T> second) =>
+ fromBind((values) => values.transform(first).transform(second));
diff --git a/pkgs/stream_transform/lib/src/map.dart b/pkgs/stream_transform/lib/src/map.dart
new file mode 100644
index 0000000..8bce550
--- /dev/null
+++ b/pkgs/stream_transform/lib/src/map.dart
@@ -0,0 +1,22 @@
+// Copyright (c) 2018, 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 'bind.dart';
+
+/// Models a [Stream.map] callback as a [StreamTransformer].
+///
+/// This is most useful to pass to functions that take a [StreamTransformer]
+/// other than [Stream.transform]. For inline uses [Stream.map] should be
+/// preferred.
+///
+/// For example:
+///
+/// ```
+/// final sinkMapper = new StreamSinkTransformer.fromStreamTransformer(
+/// map((v) => '$v'));
+/// ```
+StreamTransformer<S, T> map<S, T>(T convert(S event)) =>
+ fromBind((stream) => stream.map(convert));
diff --git a/pkgs/stream_transform/lib/src/switch.dart b/pkgs/stream_transform/lib/src/switch.dart
index 1774dd9..bbd40d4 100644
--- a/pkgs/stream_transform/lib/src/switch.dart
+++ b/pkgs/stream_transform/lib/src/switch.dart
@@ -3,7 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
-import 'bind.dart';
+import 'chain_transformers.dart';
+import 'map.dart';
/// Maps events to a Stream and emits values from the most recently created
/// Stream.
@@ -13,8 +14,8 @@
///
/// If the source stream is a broadcast stream, the result stream will be as
/// well, regardless of the types of the streams produced by [map].
-StreamTransformer<S, T> switchMap<S, T>(Stream<T> map(S event)) =>
- fromBind((stream) => stream.map(map).transform(switchLatest()));
+StreamTransformer<S, T> switchMap<S, T>(Stream<T> convert(S event)) =>
+ chainTransformers(map(convert), switchLatest());
/// Emits values from the most recently emitted Stream.
///
diff --git a/pkgs/stream_transform/lib/stream_transform.dart b/pkgs/stream_transform/lib/stream_transform.dart
index 170a5be..942391b 100644
--- a/pkgs/stream_transform/lib/stream_transform.dart
+++ b/pkgs/stream_transform/lib/stream_transform.dart
@@ -6,9 +6,11 @@
export 'src/async_where.dart';
export 'src/audit.dart';
export 'src/buffer.dart';
+export 'src/chain_transformers.dart';
export 'src/concat.dart';
export 'src/debounce.dart';
export 'src/followed_by.dart';
+export 'src/map.dart';
export 'src/merge.dart';
export 'src/scan.dart';
export 'src/start_with.dart';
diff --git a/pkgs/stream_transform/pubspec.yaml b/pkgs/stream_transform/pubspec.yaml
index dc4525b..6cc3be8 100644
--- a/pkgs/stream_transform/pubspec.yaml
+++ b/pkgs/stream_transform/pubspec.yaml
@@ -2,7 +2,7 @@
description: A collection of utilities to transform and manipulate streams.
author: Dart Team <misc@dartlang.org>
homepage: https://www.github.com/dart-lang/stream_transform
-version: 0.0.12-dev
+version: 0.0.12
environment:
sdk: ">=2.0.0-dev.20.0 <2.0.0"