| OLD | NEW |
| 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 } |
| OLD | NEW |