OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'delegate/stream.dart'; | 7 import 'delegate/stream.dart'; |
8 | 8 |
9 /// Creates a wrapper that coerces the type of [transformer]. | 9 /// Creates a wrapper that coerces the type of [transformer]. |
10 /// | 10 /// |
11 /// This soundly converts a [StreamTransformer] to a `StreamTransformer<S, T>`, | 11 /// This soundly converts a [StreamTransformer] to a `StreamTransformer<S, T>`, |
12 /// regardless of its original generic type, by asserting that the events | 12 /// regardless of its original generic type, by asserting that the events |
13 /// emitted by the transformed stream are instances of `T` whenever they're | 13 /// emitted by the transformed stream are instances of `T` whenever they're |
14 /// provided. If they're not, the stream throws a [CastError]. | 14 /// provided. If they're not, the stream throws a [CastError]. |
15 StreamTransformer/*<S, T>*/ typedStreamTransformer/*<S, T>*/( | 15 StreamTransformer<S, T> typedStreamTransformer<S, T>( |
16 StreamTransformer transformer) => | 16 StreamTransformer transformer) => |
17 transformer is StreamTransformer/*<S, T>*/ | 17 transformer is StreamTransformer<S, T> |
18 ? transformer | 18 ? transformer |
19 : new _TypeSafeStreamTransformer(transformer); | 19 : new _TypeSafeStreamTransformer(transformer); |
20 | 20 |
21 /// A wrapper that coerces the type of the stream returned by an inner | 21 /// A wrapper that coerces the type of the stream returned by an inner |
22 /// transformer. | 22 /// transformer. |
23 class _TypeSafeStreamTransformer<S, T> implements StreamTransformer<S, T> { | 23 class _TypeSafeStreamTransformer<S, T> implements StreamTransformer<S, T> { |
24 final StreamTransformer _inner; | 24 final StreamTransformer _inner; |
25 | 25 |
26 _TypeSafeStreamTransformer(this._inner); | 26 _TypeSafeStreamTransformer(this._inner); |
27 | 27 |
28 Stream<T> bind(Stream<S> stream) => | 28 Stream<T> bind(Stream<S> stream) => |
29 DelegatingStream.typed(_inner.bind(stream)); | 29 DelegatingStream.typed(_inner.bind(stream)); |
30 } | 30 } |
OLD | NEW |