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