Line data Source code
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 'delegate/stream.dart';
8 :
9 : /// Creates a wrapper that coerces the type of [transformer].
10 : ///
11 : /// This soundly converts a [StreamTransformer] to a `StreamTransformer<S, T>`,
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
14 : /// provided. If they're not, the stream throws a [CastError].
15 : StreamTransformer<S, T> typedStreamTransformer<S, T>(
16 : StreamTransformer transformer) =>
17 0 : transformer is StreamTransformer<S, T>
18 : ? transformer
19 0 : : new _TypeSafeStreamTransformer(transformer);
20 :
21 : /// A wrapper that coerces the type of the stream returned by an inner
22 : /// transformer.
23 : class _TypeSafeStreamTransformer<S, T> implements StreamTransformer<S, T> {
24 : final StreamTransformer _inner;
25 :
26 0 : _TypeSafeStreamTransformer(this._inner);
27 :
28 : Stream<T> bind(Stream<S> stream) =>
29 0 : DelegatingStream.typed(_inner.bind(stream));
30 : }
|