OLD | NEW |
(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 import 'dart:async'; |
| 6 |
| 7 import '../stream_sink_transformer.dart'; |
| 8 |
| 9 /// A [StreamSinkTransformer] that wraps a pre-existing [StreamTransformer]. |
| 10 class StreamTransformerWrapper<S, T> implements StreamSinkTransformer<S, T> { |
| 11 /// The wrapped transformer. |
| 12 final StreamTransformer<S, T> _transformer; |
| 13 |
| 14 const StreamTransformerWrapper(this._transformer); |
| 15 |
| 16 StreamSink<S> bind(StreamSink<T> sink) => |
| 17 new _StreamTransformerWrapperSink<S, T>(_transformer, sink); |
| 18 } |
| 19 |
| 20 /// A sink created by [StreamTransformerWrapper]. |
| 21 class _StreamTransformerWrapperSink<S, T> implements StreamSink<S> { |
| 22 /// The controller through which events are passed. |
| 23 /// |
| 24 /// This is used to create a stream that can be transformed by the wrapped |
| 25 /// transformer. |
| 26 final _controller = new StreamController<S>(sync: true); |
| 27 |
| 28 /// The original sink that's being transformed. |
| 29 final StreamSink<T> _inner; |
| 30 |
| 31 Future get done => _inner.done; |
| 32 |
| 33 _StreamTransformerWrapperSink( |
| 34 StreamTransformer<S, T> transformer, this._inner) { |
| 35 _controller.stream |
| 36 .transform(transformer) |
| 37 .listen(_inner.add, onError: _inner.addError, onDone: () { |
| 38 // Ignore any errors that come from this call to [_inner.close]. The |
| 39 // user can access them through [done] or the value returned from |
| 40 // [this.close], and we don't want them to get top-leveled. |
| 41 _inner.close().catchError((_) {}); |
| 42 }); |
| 43 } |
| 44 |
| 45 void add(S event) { |
| 46 _controller.add(event); |
| 47 } |
| 48 |
| 49 void addError(error, [StackTrace stackTrace]) { |
| 50 _controller.addError(error, stackTrace); |
| 51 } |
| 52 |
| 53 Future addStream(Stream<S> stream) => _controller.addStream(stream); |
| 54 |
| 55 Future close() { |
| 56 _controller.close(); |
| 57 return _inner.done; |
| 58 } |
| 59 } |
OLD | NEW |