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`. |
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; |
(...skipping 14 matching lines...) Expand all Loading... | |
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 })); | |
Lasse Reichstein Nielsen
2016/04/08 09:20:29
I still don't like having an async function produc
nweiz
2016/04/11 20:26:16
I still think it's fine :).
| |
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 |