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

Side by Side Diff: quiver/lib/src/async/future_stream.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 | « quiver/lib/src/async/future_group.dart ('k') | quiver/lib/src/async/iteration.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 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 part of quiver.async;
16
17 /**
18 * A Stream that will emit the same values as the stream returned by [future]
19 * once [future] completes.
20 *
21 * If [future] completes to an error, the return value will emit that error and
22 * then close.
23 *
24 * If [broadcast] is true, this will be a broadcast stream. This assumes that
25 * the stream returned by [future] will be a broadcast stream as well.
26 * [broadcast] defaults to false.
27 *
28 * # Example
29 *
30 * This class is useful when you need to retreive some object via a `Future`,
31 * then return a `Stream` from that object:
32 *
33 * var futureOfStream = getResource().then((resource) => resource.stream);
34 * return new FutureStream(futureOfStream);
35 */
36 class FutureStream<T> extends Stream<T> {
37 Future<Stream<T>> _future;
38 StreamController<T> _controller;
39 StreamSubscription _subscription;
40
41 FutureStream(Future<Stream<T>> future, {bool broadcast: false}) {
42 _future = future.catchError((e, stackTrace) {
43 // Since [controller] is synchronous, it's likely that emitting an error
44 // will cause it to be cancelled before we call close.
45 if (_controller != null) {
46 _controller.addError(e, stackTrace);
47 _controller.close();
48 }
49 _controller = null;
50 });
51
52 if (broadcast == true) {
53 _controller = new StreamController.broadcast(
54 sync: true, onListen: _onListen, onCancel: _onCancel);
55 } else {
56 _controller = new StreamController(
57 sync: true, onListen: _onListen, onCancel: _onCancel);
58 }
59 }
60
61 _onListen() {
62 _future.then((stream) {
63 if (_controller == null) return;
64 _subscription = stream.listen(_controller.add,
65 onError: _controller.addError, onDone: _controller.close);
66 });
67 }
68
69 _onCancel() {
70 if (_subscription != null) _subscription.cancel();
71 _subscription = null;
72 _controller = null;
73 }
74
75 StreamSubscription<T> listen(void onData(T event),
76 {Function onError, void onDone(), bool cancelOnError}) {
77 return _controller.stream.listen(onData,
78 onError: onError, onDone: onDone, cancelOnError: cancelOnError);
79 }
80
81 bool get isBroadcast => _controller.stream.isBroadcast;
82 }
OLDNEW
« no previous file with comments | « quiver/lib/src/async/future_group.dart ('k') | quiver/lib/src/async/iteration.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698