Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 json_rpc_2.two_way_stream; | 5 library json_rpc_2.two_way_stream; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'utils.dart'; | 10 import 'utils.dart'; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 /// | 31 /// |
| 32 /// This takes decoded JSON objects. | 32 /// This takes decoded JSON objects. |
| 33 final StreamSink _output; | 33 final StreamSink _output; |
| 34 | 34 |
| 35 /// Returns a [Future] that completes when the connection is closed. | 35 /// Returns a [Future] that completes when the connection is closed. |
| 36 /// | 36 /// |
| 37 /// This is the same future that's returned by [listen]. | 37 /// This is the same future that's returned by [listen]. |
| 38 Future get done => _doneCompleter.future; | 38 Future get done => _doneCompleter.future; |
| 39 final _doneCompleter = new Completer(); | 39 final _doneCompleter = new Completer(); |
| 40 | 40 |
| 41 /// Whether the stream has been closed. | |
| 42 bool get isClosed => _isClosed; | |
|
Bob Nystrom
2015/09/03 18:13:03
Can this just be => _doneCompleter.isCompleted?
nweiz
2015/09/08 21:17:32
Done.
| |
| 43 bool _isClosed = false; | |
| 44 | |
| 41 /// Creates a two-way stream. | 45 /// Creates a two-way stream. |
| 42 /// | 46 /// |
| 43 /// [input] and [output] should emit and take (respectively) JSON-encoded | 47 /// [input] and [output] should emit and take (respectively) JSON-encoded |
| 44 /// strings. | 48 /// strings. |
| 45 /// | 49 /// |
| 46 /// [inputName] is used in error messages as the name of the input parameter. | 50 /// [inputName] is used in error messages as the name of the input parameter. |
| 47 /// [outputName] is likewise used as the name of the output parameter. | 51 /// [outputName] is likewise used as the name of the output parameter. |
| 48 /// | 52 /// |
| 49 /// If [onInvalidInput] is passed, any errors parsing messages from [input] | 53 /// If [onInvalidInput] is passed, any errors parsing messages from [input] |
| 50 /// are passed to it. Otherwise, they're ignored and the input is discarded. | 54 /// are passed to it. Otherwise, they're ignored and the input is discarded. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 } | 105 } |
| 102 | 106 |
| 103 _inputSubscription = _input.listen(handleInput, | 107 _inputSubscription = _input.listen(handleInput, |
| 104 onError: (error, stackTrace) { | 108 onError: (error, stackTrace) { |
| 105 if (_doneCompleter.isCompleted) return; | 109 if (_doneCompleter.isCompleted) return; |
| 106 _output.close(); | 110 _output.close(); |
| 107 _doneCompleter.completeError(error, stackTrace); | 111 _doneCompleter.completeError(error, stackTrace); |
| 108 }, onDone: () { | 112 }, onDone: () { |
| 109 if (_doneCompleter.isCompleted) return; | 113 if (_doneCompleter.isCompleted) return; |
| 110 _output.close(); | 114 _output.close(); |
| 111 _doneCompleter.complete(); | 115 _doneCompleter.complete(); |
|
Bob Nystrom
2015/09/03 18:13:02
Should _isClosed be set here and above?
nweiz
2015/09/08 21:17:32
Done.
| |
| 112 }, cancelOnError: true); | 116 }, cancelOnError: true); |
| 113 | 117 |
| 114 return _doneCompleter.future; | 118 return _doneCompleter.future; |
| 115 } | 119 } |
| 116 | 120 |
| 117 /// Emit [event] on the output stream. | 121 /// Emit [event] on the output stream. |
| 118 void add(event) => _output.add(event); | 122 void add(event) => _output.add(event); |
| 119 | 123 |
| 120 /// Stops listening to the input stream and closes the output stream. | 124 /// Stops listening to the input stream and closes the output stream. |
| 121 Future close() { | 125 Future close() { |
| 122 if (_inputSubscription == null) { | 126 if (_inputSubscription == null) { |
| 123 throw new StateError("Can't call $_name.close before $_name.listen."); | 127 throw new StateError("Can't call $_name.close before $_name.listen."); |
| 124 } | 128 } |
| 125 | 129 |
| 130 _isClosed = true; | |
| 126 if (!_doneCompleter.isCompleted) _doneCompleter.complete(); | 131 if (!_doneCompleter.isCompleted) _doneCompleter.complete(); |
| 127 | 132 |
| 128 var inputFuture = _inputSubscription.cancel(); | 133 var inputFuture = _inputSubscription.cancel(); |
| 129 // TODO(nweiz): include the output future in the return value when issue | 134 // TODO(nweiz): include the output future in the return value when issue |
| 130 // 19095 is fixed. | 135 // 19095 is fixed. |
| 131 _output.close(); | 136 _output.close(); |
| 132 return inputFuture == null ? new Future.value() : inputFuture; | 137 return inputFuture == null ? new Future.value() : inputFuture; |
| 133 } | 138 } |
| 134 } | 139 } |
| OLD | NEW |