| 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'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 /// The controllers that are listening for future events from [_stream]. | 28 /// The controllers that are listening for future events from [_stream]. |
| 29 final _controllers = new Set<StreamController<T>>(); | 29 final _controllers = new Set<StreamController<T>>(); |
| 30 | 30 |
| 31 StreamReplayer(this._stream) { | 31 StreamReplayer(this._stream) { |
| 32 _stream.listen((data) { | 32 _stream.listen((data) { |
| 33 _buffer.add(new Either<T, dynamic>.withFirst(data)); | 33 _buffer.add(new Either<T, dynamic>.withFirst(data)); |
| 34 for (var controller in _controllers) { | 34 for (var controller in _controllers) { |
| 35 controller.add(data); | 35 controller.add(data); |
| 36 } | 36 } |
| 37 }, onError: (error) { | 37 }, onError: (error, [StackTrace stackTrace]) { |
| 38 _buffer.add(new Either<T, dynamic>.withSecond(error)); | 38 _buffer.add(new Either<T, dynamic>.withSecond(error)); |
| 39 for (var controller in _controllers) { | 39 for (var controller in _controllers) { |
| 40 controller.addError(error); | 40 controller.addError(error, stackTrace); |
| 41 } | 41 } |
| 42 }, onDone: () { | 42 }, onDone: () { |
| 43 _isClosed = true; | 43 _isClosed = true; |
| 44 for (var controller in _controllers) { | 44 for (var controller in _controllers) { |
| 45 controller.close(); | 45 controller.close(); |
| 46 } | 46 } |
| 47 _controllers.clear(); | 47 _controllers.clear(); |
| 48 }); | 48 }); |
| 49 } | 49 } |
| 50 | 50 |
| 51 /// Returns a stream that replays the values and errors of the input stream. | 51 /// Returns a stream that replays the values and errors of the input stream. |
| 52 /// | 52 /// |
| 53 /// This stream is a buffered stream regardless of whether the input stream | 53 /// This stream is a buffered stream regardless of whether the input stream |
| 54 /// was broadcast or buffered. | 54 /// was broadcast or buffered. |
| 55 Stream<T> getReplay() { | 55 Stream<T> getReplay() { |
| 56 var controller = new StreamController<T>(); | 56 var controller = new StreamController<T>(); |
| 57 for (var eventOrError in _buffer) { | 57 for (var eventOrError in _buffer) { |
| 58 eventOrError.match(controller.add, controller.addError); | 58 eventOrError.match(controller.add, controller.addError); |
| 59 } | 59 } |
| 60 if (_isClosed) { | 60 if (_isClosed) { |
| 61 controller.close(); | 61 controller.close(); |
| 62 } else { | 62 } else { |
| 63 _controllers.add(controller); | 63 _controllers.add(controller); |
| 64 } | 64 } |
| 65 return controller.stream; | 65 return controller.stream; |
| 66 } | 66 } |
| 67 } | 67 } |
| OLD | NEW |