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<T> _source; 14 : 15 : /// Create delegating subscription forwarding calls to [sourceSubscription]. 16 0 : DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription) 17 : : _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 [TypeError]. 26 0 : @Deprecated('Use Stream.cast instead') 27 : // TODO - Remove `TypeSafeStreamSubscription` and tests when removing this. 28 : static StreamSubscription<T> typed<T>(StreamSubscription subscription) => 29 0 : subscription is StreamSubscription<T> 30 : ? subscription 31 0 : : TypeSafeStreamSubscription<T>(subscription); 32 : 33 0 : @override 34 : void onData(void Function(T)? handleData) { 35 0 : _source.onData(handleData); 36 : } 37 : 38 0 : @override 39 : void onError(Function? handleError) { 40 0 : _source.onError(handleError); 41 : } 42 : 43 0 : @override 44 : void onDone(void Function()? handleDone) { 45 0 : _source.onDone(handleDone); 46 : } 47 : 48 0 : @override 49 : void pause([Future? resumeFuture]) { 50 0 : _source.pause(resumeFuture); 51 : } 52 : 53 0 : @override 54 : void resume() { 55 0 : _source.resume(); 56 : } 57 : 58 0 : @override 59 0 : Future cancel() => _source.cancel(); 60 : 61 0 : @override 62 0 : Future<E> asFuture<E>([E? futureValue]) => _source.asFuture(futureValue); 63 : 64 0 : @override 65 0 : bool get isPaused => _source.isPaused; 66 : }