Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: packages/async/lib/src/lazy_stream.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/async/lib/src/future_group.dart ('k') | packages/async/lib/src/null_stream_sink.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import "dart:async";
6
7 import "delegate/stream.dart";
8 import "stream_completer.dart";
9 import "utils.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 FutureOrCallback<Stream<T>> _callback;
20
21 /// Creates a single-subscription `Stream` that calls [callback] when it gets
22 /// a listener and forwards to the returned stream.
23 LazyStream(FutureOr<Stream<T>> callback()) : _callback = callback {
24 // Explicitly check for null because we null out [_callback] internally.
25 if (_callback == null) throw new ArgumentError.notNull('callback');
26 }
27
28 StreamSubscription<T> listen(void onData(T event),
29 {Function onError, void onDone(), bool cancelOnError}) {
30 if (_callback == null) {
31 throw new StateError("Stream has already been listened to.");
32 }
33
34 // Null out the callback before we invoke it to ensure that even while
35 // running it, this can't be called twice.
36 var callback = _callback;
37 _callback = null;
38 var result = callback();
39
40 Stream<T> stream;
41 if (result is Future<Stream<T>>) {
42 stream = StreamCompleter.fromFuture(result.then((stream) {
43 return DelegatingStream.typed<T>(stream);
44 }));
45 } else {
46 stream = DelegatingStream.typed<T>(result as Stream);
47 }
48
49 return stream.listen(onData,
50 onError: onError, onDone: onDone, cancelOnError: cancelOnError);
51 }
52 }
OLDNEW
« no previous file with comments | « packages/async/lib/src/future_group.dart ('k') | packages/async/lib/src/null_stream_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698