Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 library async.delegating_stream_subscription; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 /// Base class for delegating stream subscriptions. | |
| 10 abstract class _DelegatingSubscriptionBase<T> | |
| 11 implements StreamSubscription<T> { | |
| 12 StreamSubscription get _source; | |
| 13 | |
| 14 void onData(void handleData(T data)) { | |
| 15 _source.onData(handleData); | |
| 16 } | |
| 17 | |
| 18 void onError(Function handleError) { | |
| 19 _source.onError(handleError); | |
| 20 } | |
| 21 | |
| 22 void onDone(void handleDone()) { | |
| 23 _source.onDone(handleDone); | |
| 24 } | |
| 25 | |
| 26 void pause([Future resumeFuture]) { | |
| 27 _source.pause(resumeFuture); | |
| 28 } | |
| 29 | |
| 30 void resume() { | |
| 31 _source.resume(); | |
| 32 } | |
| 33 | |
| 34 Future cancel() => _source.cancel(); | |
| 35 | |
| 36 Future asFuture([futureValue]) => _source.asFuture(futureValue); | |
| 37 | |
| 38 bool get isPaused => _source.isPaused; | |
| 39 } | |
| 40 | |
| 41 /// Simple delegating wrapper around a [StreamSubscription]. | |
| 42 /// | |
| 43 /// Subclasses can override individual methods. | |
| 44 class DelegatingStreamSubscription<T> extends _DelegatingSubscriptionBase<T> { | |
| 45 final StreamSubscription _source; | |
| 46 | |
| 47 DelegatingStreamSubscription(StreamSubscription subscription) | |
| 48 : _source = subscription; | |
| 49 } | |
| 50 | |
| 51 /// A delegating wrapper around a [StreamSubscription], which can be changed. | |
| 52 /// | |
| 53 /// Like a [DelegatingStreamSubscription] except that the subscription | |
| 54 /// being delegated to can be changed after the wrapper has been created | |
| 55 /// using the static [setDelegate] methods. | |
| 56 /// | |
| 57 /// This class can be used if a stream subscription needs to act as different | |
| 58 /// other subscriptions at different times, but act as a single subscription | |
| 59 /// outwards. | |
| 60 class _MutableDelegatingSubscription<T> extends _DelegatingSubscriptionBase<T> { | |
| 61 /// The subscription being delegated to. | |
| 62 /// | |
| 63 /// Can be changed by a [MutableDelegatingStreamSubscriptionController]. | |
| 64 StreamSubscription _source; | |
| 65 | |
| 66 _MutableDelegatingSubscription(this._source); | |
| 67 } | |
| 68 | |
| 69 // A delegating stream subscription and a way to update its source subscription. | |
| 70 class MutableDelegatingStreamSubscriptionController<T> { | |
|
nweiz
2015/06/16 01:05:23
I think it's quite unlikely that people will want
Lasse Reichstein Nielsen
2015/06/18 12:10:12
I'm not using it any more, so I'll just remove it.
| |
| 71 /// A delegating stream subscription that delegates to a stream subscription. | |
| 72 /// | |
| 73 /// When this controller is created, the subscription delegates to the | |
| 74 /// subscription provided in the constructor. | |
| 75 /// After that it can be changed using the [setSourceSubscription] function. | |
| 76 final StreamSubscription<T> subscription; | |
| 77 | |
| 78 /// Create a controller with a [subscription] delegating to [source]. | |
| 79 MutableDelegatingStreamSubscriptionController(StreamSubscription source) | |
| 80 : subscription = new _MutableDelegatingSubscription<T>(source); | |
| 81 | |
| 82 /// The current subscription being delegated to. | |
| 83 StreamSubscription get sourceSubscription => subscription._source; | |
| 84 | |
| 85 void set sourceSubscription(StreamSubscription source) { | |
| 86 subscription._source = source; | |
| 87 } | |
| 88 } | |
| OLD | NEW |