Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Side by Side Diff: lib/src/delegate/stream_subscription.dart

Issue 1870543004: Add typed wrapper functions to delegate classes. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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';
8
7 /// Simple delegating wrapper around a [StreamSubscription]. 9 /// Simple delegating wrapper around a [StreamSubscription].
8 /// 10 ///
9 /// Subclasses can override individual methods. 11 /// Subclasses can override individual methods.
10 class DelegatingStreamSubscription<T> implements StreamSubscription<T> { 12 class DelegatingStreamSubscription<T> implements StreamSubscription<T> {
11 final StreamSubscription _source; 13 final StreamSubscription _source;
12 14
13 /// Create delegating subscription forwarding calls to [sourceSubscription]. 15 /// Create delegating subscription forwarding calls to [sourceSubscription].
14 DelegatingStreamSubscription(StreamSubscription sourceSubscription) 16 DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription)
15 : _source = sourceSubscription; 17 : _source = sourceSubscription;
16 18
19 /// Creates a wrapper which throws if [subscription]'s events aren't instances
20 /// of `T`.
21 ///
22 /// This soundly converts a [StreamSubscription] to a `StreamSubscription<T>`,
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
25 /// subscription throws a [CastError].
26 static StreamSubscription/*<T>*/ typed/*<T>*/(
27 StreamSubscription subscription) =>
28 subscription is StreamSubscription/*<T>*/
29 ? subscription
30 : new TypeSafeStreamSubscription/*<T>*/(subscription);
31
17 void onData(void handleData(T data)) { 32 void onData(void handleData(T data)) {
18 _source.onData(handleData); 33 _source.onData(handleData);
19 } 34 }
20 35
21 void onError(Function handleError) { 36 void onError(Function handleError) {
22 _source.onError(handleError); 37 _source.onError(handleError);
23 } 38 }
24 39
25 void onDone(void handleDone()) { 40 void onDone(void handleDone()) {
26 _source.onDone(handleDone); 41 _source.onDone(handleDone);
27 } 42 }
28 43
29 void pause([Future resumeFuture]) { 44 void pause([Future resumeFuture]) {
30 _source.pause(resumeFuture); 45 _source.pause(resumeFuture);
31 } 46 }
32 47
33 void resume() { 48 void resume() {
34 _source.resume(); 49 _source.resume();
35 } 50 }
36 51
37 Future cancel() => _source.cancel(); 52 Future cancel() => _source.cancel();
38 53
39 Future asFuture([futureValue]) => _source.asFuture(futureValue); 54 Future asFuture([futureValue]) => _source.asFuture(futureValue);
40 55
41 bool get isPaused => _source.isPaused; 56 bool get isPaused => _source.isPaused;
42 } 57 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698