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

Side by Side Diff: lib/src/stream_sink_transformer/stream_transformer_wrapper.dart

Issue 1566603002: Add a StreamSinkTransformer class. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes 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 unified diff | Download patch
« no previous file with comments | « lib/src/stream_sink_transformer/handler_transformer.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library async.stream_sink_transformer.stream_transformer_wrapper;
6
7 import 'dart:async';
8
9 import '../stream_sink_transformer.dart';
10
11 /// A [StreamSinkTransformer] that wraps a pre-existing [StreamTransformer].
12 class StreamTransformerWrapper<S, T> implements StreamSinkTransformer<S, T> {
13 /// The wrapped transformer.
14 final StreamTransformer<S, T> _transformer;
15
16 const StreamTransformerWrapper(this._transformer);
17
18 StreamSink<S> bind(StreamSink<T> sink) =>
19 new _StreamTransformerWrapperSink<S, T>(_transformer, sink);
20 }
21
22 /// A sink created by [StreamTransformerWrapper].
23 class _StreamTransformerWrapperSink<S, T> implements StreamSink<S> {
24 /// The controller through which events are passed.
25 ///
26 /// This is used to create a stream that can be transformed by the wrapped
27 /// transformer.
28 final _controller = new StreamController<S>(sync: true);
29
30 /// The original sink that's being transformed.
31 final StreamSink<T> _inner;
32
33 Future get done => _inner.done;
34
35 _StreamTransformerWrapperSink(StreamTransformer<S, T> transformer,
36 this._inner) {
37 _controller.stream.transform(transformer).listen(
38 _inner.add,
39 onError: _inner.addError,
40 onDone: () {
41 // Ignore any errors that come from this call to [_inner.close]. The
42 // user can access them through [done] or the value returned from
43 // [this.close], and we don't want them to get top-leveled.
44 _inner.close().catchError((_) {});
45 });
46 }
47
48 void add(S event) {
49 _controller.add(event);
50 }
51
52 void addError(error, [StackTrace stackTrace]) {
53 _controller.addError(error, stackTrace);
54 }
55
56 Future addStream(Stream<S> stream) => _controller.addStream(stream);
57
58 Future close() {
59 _controller.close();
60 return _inner.done;
61 }
62 }
OLDNEW
« no previous file with comments | « lib/src/stream_sink_transformer/handler_transformer.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698