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 import '../typed/stream_subscription.dart'; | 7 import '../typed/stream_subscription.dart'; |
8 | 8 |
9 /// Simple delegating wrapper around a [StreamSubscription]. | 9 /// Simple delegating wrapper around a [StreamSubscription]. |
10 /// | 10 /// |
11 /// Subclasses can override individual methods. | 11 /// Subclasses can override individual methods. |
12 class DelegatingStreamSubscription<T> implements StreamSubscription<T> { | 12 class DelegatingStreamSubscription<T> implements StreamSubscription<T> { |
13 final StreamSubscription _source; | 13 final StreamSubscription _source; |
14 | 14 |
15 /// Create delegating subscription forwarding calls to [sourceSubscription]. | 15 /// Create delegating subscription forwarding calls to [sourceSubscription]. |
16 DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription) | 16 DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription) |
17 : _source = sourceSubscription; | 17 : _source = sourceSubscription; |
18 | 18 |
19 /// Creates a wrapper which throws if [subscription]'s events aren't instances | 19 /// Creates a wrapper which throws if [subscription]'s events aren't instances |
20 /// of `T`. | 20 /// of `T`. |
21 /// | 21 /// |
22 /// This soundly converts a [StreamSubscription] to a `StreamSubscription<T>`, | 22 /// This soundly converts a [StreamSubscription] to a `StreamSubscription<T>`, |
23 /// regardless of its original generic type, by asserting that its events are | 23 /// regardless of its original generic type, by asserting that its events are |
24 /// instances of `T` whenever they're provided. If they're not, the | 24 /// instances of `T` whenever they're provided. If they're not, the |
25 /// subscription throws a [CastError]. | 25 /// subscription throws a [CastError]. |
26 static StreamSubscription/*<T>*/ typed/*<T>*/( | 26 static StreamSubscription<T> typed<T>( |
27 StreamSubscription subscription) => | 27 StreamSubscription subscription) => |
28 subscription is StreamSubscription/*<T>*/ | 28 subscription is StreamSubscription<T> |
29 ? subscription | 29 ? subscription |
30 : new TypeSafeStreamSubscription/*<T>*/(subscription); | 30 : new TypeSafeStreamSubscription<T>(subscription); |
31 | 31 |
32 void onData(void handleData(T data)) { | 32 void onData(void handleData(T data)) { |
33 _source.onData(handleData); | 33 _source.onData(handleData); |
34 } | 34 } |
35 | 35 |
36 void onError(Function handleError) { | 36 void onError(Function handleError) { |
37 _source.onError(handleError); | 37 _source.onError(handleError); |
38 } | 38 } |
39 | 39 |
40 void onDone(void handleDone()) { | 40 void onDone(void handleDone()) { |
41 _source.onDone(handleDone); | 41 _source.onDone(handleDone); |
42 } | 42 } |
43 | 43 |
44 void pause([Future resumeFuture]) { | 44 void pause([Future resumeFuture]) { |
45 _source.pause(resumeFuture); | 45 _source.pause(resumeFuture); |
46 } | 46 } |
47 | 47 |
48 void resume() { | 48 void resume() { |
49 _source.resume(); | 49 _source.resume(); |
50 } | 50 } |
51 | 51 |
52 Future cancel() => _source.cancel(); | 52 Future cancel() => _source.cancel(); |
53 | 53 |
54 Future/*<E>*/ asFuture/*<E>*/([/*=E*/ futureValue]) => | 54 Future<E> asFuture<E>([E futureValue]) => |
55 _source.asFuture(futureValue); | 55 _source.asFuture(futureValue); |
56 | 56 |
57 bool get isPaused => _source.isPaused; | 57 bool get isPaused => _source.isPaused; |
58 } | 58 } |
OLD | NEW |