Line data Source code
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
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'dart:async';
6 :
7 : import '../typed/stream_subscription.dart';
8 :
9 : /// Simple delegating wrapper around a [StreamSubscription].
10 : ///
11 : /// Subclasses can override individual methods.
12 : class DelegatingStreamSubscription<T> implements StreamSubscription<T> {
13 : final StreamSubscription _source;
14 :
15 : /// Create delegating subscription forwarding calls to [sourceSubscription].
16 : DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription)
17 0 : : _source = sourceSubscription;
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>(StreamSubscription subscription) =>
27 0 : subscription is StreamSubscription<T>
28 : ? subscription
29 0 : : new TypeSafeStreamSubscription<T>(subscription);
30 :
31 : void onData(void handleData(T data)) {
32 0 : _source.onData(handleData);
33 : }
34 :
35 : void onError(Function handleError) {
36 0 : _source.onError(handleError);
37 : }
38 :
39 : void onDone(void handleDone()) {
40 0 : _source.onDone(handleDone);
41 : }
42 :
43 : void pause([Future resumeFuture]) {
44 0 : _source.pause(resumeFuture);
45 : }
46 :
47 : void resume() {
48 0 : _source.resume();
49 : }
50 :
51 0 : Future cancel() => _source.cancel();
52 :
53 0 : Future<E> asFuture<E>([E futureValue]) => _source.asFuture(futureValue);
54 :
55 0 : bool get isPaused => _source.isPaused;
56 : }
|