OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Simple delegating wrapper around a [StreamConsumer]. | 7 /// Simple delegating wrapper around a [StreamConsumer]. |
8 /// | 8 /// |
9 /// Subclasses can override individual methods, or use this to expose only the | 9 /// Subclasses can override individual methods, or use this to expose only the |
10 /// [StreamConsumer] methods of a subclass. | 10 /// [StreamConsumer] methods of a subclass. |
11 class DelegatingStreamConsumer<T> implements StreamConsumer<T> { | 11 class DelegatingStreamConsumer<T> implements StreamConsumer<T> { |
12 final StreamConsumer _consumer; | 12 final StreamConsumer _consumer; |
13 | 13 |
14 /// Create a delegating consumer forwarding calls to [consumer]. | 14 /// Create a delegating consumer forwarding calls to [consumer]. |
15 DelegatingStreamConsumer(StreamConsumer consumer) | 15 DelegatingStreamConsumer(StreamConsumer<T> consumer) : _consumer = consumer; |
16 : _consumer = consumer; | 16 |
| 17 DelegatingStreamConsumer._(this._consumer); |
| 18 |
| 19 /// Creates a wrapper that coerces the type of [consumer]. |
| 20 /// |
| 21 /// Unlike [new StreamConsumer], this only requires its argument to be an |
| 22 /// instance of `StreamConsumer`, not `StreamConsumer<T>`. This means that |
| 23 /// calls to [addStream] may throw a [CastError] if the argument type doesn't |
| 24 /// match the reified type of [consumer]. |
| 25 static StreamConsumer/*<T>*/ typed/*<T>*/(StreamConsumer consumer) => |
| 26 consumer is StreamConsumer/*<T>*/ |
| 27 ? consumer |
| 28 : new DelegatingStreamConsumer._(consumer); |
17 | 29 |
18 Future addStream(Stream<T> stream) => _consumer.addStream(stream); | 30 Future addStream(Stream<T> stream) => _consumer.addStream(stream); |
19 | 31 |
20 Future close() => _consumer.close(); | 32 Future close() => _consumer.close(); |
21 } | 33 } |
OLD | NEW |