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

Side by Side Diff: pkg/barback/lib/src/stream_replayer.dart

Issue 119673002: Add a ScheduledStream class and some stream matchers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/barback/lib/src/utils.dart » ('j') | pkg/barback/lib/src/utils.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 library barback.stream_replayer; 5 library barback.stream_replayer;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'utils.dart'; 10 import 'utils.dart';
11 11
12 /// Records the values and errors that are sent through a stream and allows them 12 /// Records the values and errors that are sent through a stream and allows them
13 /// to be replayed arbitrarily many times. 13 /// to be replayed arbitrarily many times.
14 /// 14 ///
15 /// This only listens to the wrapped stream when a replayed stream gets a 15 /// This only listens to the wrapped stream when a replayed stream gets a
16 /// listener. 16 /// listener.
17 class StreamReplayer<T> { 17 class StreamReplayer<T> {
18 /// The wrapped stream. 18 /// The wrapped stream.
19 final Stream<T> _stream; 19 final Stream<T> _stream;
20 20
21 /// Whether or not [this] has started listening to [_stream]. 21 /// Whether or not [this] has started listening to [_stream].
22 bool _isSubscribed = false; 22 bool _isSubscribed = false;
23 23
24 /// Whether or not [_stream] has been closed. 24 /// Whether or not [_stream] has been closed.
25 bool _isClosed = false; 25 bool _isClosed = false;
26 26
27 /// The buffer of events or errors that have already been emitted by 27 /// The buffer of events or errors that have already been emitted by
28 /// [_stream]. 28 /// [_stream].
29 /// 29 ///
30 /// Each element is a [Either] that's either a value or an error sent through 30 /// Each element is a [FallibleValue] that's either a value or an error sent
31 /// the stream. 31 /// through the stream.
32 final _buffer = new Queue<Either<T, Pair<dynamic, StackTrace>>>(); 32 final _buffer = new Queue<FallibleValue<T>>();
33 33
34 /// The controllers that are listening for future events from [_stream]. 34 /// The controllers that are listening for future events from [_stream].
35 final _controllers = new Set<StreamController<T>>(); 35 final _controllers = new Set<StreamController<T>>();
36 36
37 StreamReplayer(this._stream); 37 StreamReplayer(this._stream);
38 38
39 /// Returns a stream that replays the values and errors of the input stream. 39 /// Returns a stream that replays the values and errors of the input stream.
40 /// 40 ///
41 /// This stream is a buffered stream. 41 /// This stream is a buffered stream.
42 Stream<T> getReplay() { 42 Stream<T> getReplay() {
43 var controller = new StreamController<T>(onListen: _subscribe); 43 var controller = new StreamController<T>(onListen: _subscribe);
44 44
45 for (var eventOrError in _buffer) { 45 for (var eventOrError in _buffer) {
46 eventOrError.match(controller.add, (pair) { 46 if (eventOrError.hasValue) {
47 controller.addError(pair.first, pair.second); 47 controller.add(eventOrError.value);
48 }); 48 } else {
49 controller.addError(eventOrError.error, eventOrError.stackTrace);
50 }
49 } 51 }
50 if (_isClosed) { 52 if (_isClosed) {
51 controller.close(); 53 controller.close();
52 } else { 54 } else {
53 _controllers.add(controller); 55 _controllers.add(controller);
54 } 56 }
55 return controller.stream; 57 return controller.stream;
56 } 58 }
57 59
58 /// Subscribe to [_stream] if we haven't yet done so. 60 /// Subscribe to [_stream] if we haven't yet done so.
59 void _subscribe() { 61 void _subscribe() {
60 if (_isSubscribed || _isClosed) return; 62 if (_isSubscribed || _isClosed) return;
61 _isSubscribed = true; 63 _isSubscribed = true;
62 64
63 _stream.listen((data) { 65 _stream.listen((data) {
64 _buffer.add(new Either<T, dynamic>.withFirst(data)); 66 _buffer.add(new FallibleValue<T>.withValue(data));
65 for (var controller in _controllers) { 67 for (var controller in _controllers) {
66 controller.add(data); 68 controller.add(data);
67 } 69 }
68 }, onError: (error, [stackTrace]) { 70 }, onError: (error, [stackTrace]) {
69 _buffer.add(new Either<T, Pair<dynamic, StackTrace>>.withSecond( 71 _buffer.add(new FallibleValue<T>.withError(error, stackTrace));
70 new Pair<dynamic, StackTrace>(error, stackTrace)));
71 for (var controller in _controllers) { 72 for (var controller in _controllers) {
72 controller.addError(error, stackTrace); 73 controller.addError(error, stackTrace);
73 } 74 }
74 }, onDone: () { 75 }, onDone: () {
75 _isClosed = true; 76 _isClosed = true;
76 for (var controller in _controllers) { 77 for (var controller in _controllers) {
77 controller.close(); 78 controller.close();
78 } 79 }
79 _controllers.clear(); 80 _controllers.clear();
80 }); 81 });
81 } 82 }
82 } 83 }
OLDNEW
« no previous file with comments | « no previous file | pkg/barback/lib/src/utils.dart » ('j') | pkg/barback/lib/src/utils.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698