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

Unified Diff: lib/src/two_way_stream.dart

Issue 810333007: Add a done getter to Client, Server, and Peer. (Closed) Base URL: git@github.com:dart-lang/json_rpc_2@master
Patch Set: Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/server.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/two_way_stream.dart
diff --git a/lib/src/two_way_stream.dart b/lib/src/two_way_stream.dart
index f470c2fcec6f024d34c15a1f015b2848050ae089..5914173876f6d21799bfdcc1c34cd45471c71852 100644
--- a/lib/src/two_way_stream.dart
+++ b/lib/src/two_way_stream.dart
@@ -32,10 +32,11 @@ class TwoWayStream {
/// This takes decoded JSON objects.
final StreamSink _output;
- /// The completer for [listen].
+ /// Returns a [Future] that completes when the connection is closed.
///
- /// This is non-`null` after [listen] has been called.
- Completer _listenCompleter;
+ /// This is the same future that's returned by [listen].
+ Future get done => _doneCompleter.future;
+ final _doneCompleter = new Completer();
/// Creates a two-way stream.
///
@@ -95,23 +96,22 @@ class TwoWayStream {
/// The returned Future will complete when the input stream is closed. If the
/// input stream emits an error, that will be piped to the returned Future.
Future listen(void handleInput(input)) {
- if (_listenCompleter != null) {
+ if (_inputSubscription != null) {
throw new StateError("Can only call $_name.listen once.");
}
- _listenCompleter = new Completer();
_inputSubscription = _input.listen(handleInput,
onError: (error, stackTrace) {
- if (_listenCompleter.isCompleted) return;
+ if (_doneCompleter.isCompleted) return;
_output.close();
- _listenCompleter.completeError(error, stackTrace);
+ _doneCompleter.completeError(error, stackTrace);
}, onDone: () {
- if (_listenCompleter.isCompleted) return;
+ if (_doneCompleter.isCompleted) return;
_output.close();
- _listenCompleter.complete();
+ _doneCompleter.complete();
}, cancelOnError: true);
- return _listenCompleter.future;
+ return _doneCompleter.future;
}
/// Emit [event] on the output stream.
@@ -119,11 +119,11 @@ class TwoWayStream {
/// Stops listening to the input stream and closes the output stream.
Future close() {
- if (_listenCompleter == null) {
+ if (_inputSubscription == null) {
throw new StateError("Can't call $_name.close before $_name.listen.");
}
- if (!_listenCompleter.isCompleted) _listenCompleter.complete();
+ if (!_doneCompleter.isCompleted) _doneCompleter.complete();
var inputFuture = _inputSubscription.cancel();
// TODO(nweiz): include the output future in the return value when issue
« no previous file with comments | « lib/src/server.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698