Chromium Code Reviews| 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 "stream_completer.dart"; | 7 import "stream_completer.dart"; |
| 8 import "delegate/stream.dart"; | |
| 8 | 9 |
| 9 /// A [Stream] wrapper that forwards to another [Stream] that's initialized | 10 /// A [Stream] wrapper that forwards to another [Stream] that's initialized |
| 10 /// lazily. | 11 /// lazily. |
| 11 /// | 12 /// |
| 12 /// This class allows a concrete `Stream` to be created only once it has a | 13 /// This class allows a concrete `Stream` to be created only once it has a |
| 13 /// listener. It's useful to wrapping APIs that do expensive computation to | 14 /// listener. It's useful to wrapping APIs that do expensive computation to |
| 14 /// produce a `Stream`. | 15 /// produce a `Stream`. |
|
floitsch
2016/04/12 20:03:08
Why would it be expensive to create a Stream?
As l
Lasse Reichstein Nielsen
2016/04/12 20:18:01
I think an example would be an API that provide th
| |
| 15 class LazyStream<T> extends Stream<T> { | 16 class LazyStream<T> extends Stream<T> { |
| 16 /// The callback that's called to create the inner stream. | 17 /// The callback that's called to create the inner stream. |
| 17 ZoneCallback _callback; | 18 ZoneCallback _callback; |
| 18 | 19 |
| 19 /// Creates a single-subscription `Stream` that calls [callback] when it gets | 20 /// Creates a single-subscription `Stream` that calls [callback] when it gets |
| 20 /// a listener and forwards to the returned stream. | 21 /// a listener and forwards to the returned stream. |
| 21 /// | 22 /// |
| 22 /// The [callback] may return a `Stream` or a `Future<Stream>`. | 23 /// The [callback] may return a `Stream` or a `Future<Stream>`. |
| 23 LazyStream(callback()) : _callback = callback { | 24 LazyStream(callback()) : _callback = callback { |
| 24 // Explicitly check for null because we null out [_callback] internally. | 25 // Explicitly check for null because we null out [_callback] internally. |
| 25 if (_callback == null) throw new ArgumentError.notNull('callback'); | 26 if (_callback == null) throw new ArgumentError.notNull('callback'); |
| 26 } | 27 } |
| 27 | 28 |
| 28 StreamSubscription<T> listen(void onData(T event), | 29 StreamSubscription<T> listen(void onData(T event), |
| 29 {Function onError, | 30 {Function onError, |
| 30 void onDone(), | 31 void onDone(), |
| 31 bool cancelOnError}) { | 32 bool cancelOnError}) { |
| 32 if (_callback == null) { | 33 if (_callback == null) { |
| 33 throw new StateError("Stream has already been listened to."); | 34 throw new StateError("Stream has already been listened to."); |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Null out the callback before we invoke it to ensure that even while | 37 // Null out the callback before we invoke it to ensure that even while |
| 37 // running it, this can't be called twice. | 38 // running it, this can't be called twice. |
| 38 var callback = _callback; | 39 var callback = _callback; |
| 39 _callback = null; | 40 _callback = null; |
| 40 var result = callback(); | 41 var result = callback(); |
| 41 | 42 |
| 42 Stream stream = result is Future | 43 Stream<T> stream; |
| 43 ? StreamCompleter.fromFuture(result) | 44 if (result is Future) { |
| 44 : result; | 45 stream = StreamCompleter.fromFuture(result.then((stream) { |
| 46 return DelegatingStream.typed/*<T>*/(stream as Stream); | |
| 47 })); | |
| 48 } else { | |
| 49 stream = DelegatingStream.typed/*<T>*/(result as Stream); | |
| 50 } | |
| 45 | 51 |
| 46 return stream.listen(onData, | 52 return stream.listen(onData, |
| 47 onError: onError, onDone: onDone, cancelOnError: cancelOnError); | 53 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
| 48 } | 54 } |
| 49 } | 55 } |
| OLD | NEW |