| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:stream_channel/stream_channel.dart'; | 7 import 'package:stream_channel/stream_channel.dart'; |
| 8 | 8 |
| 9 /// Wraps a [StreamChannel] and handles logic that's shared between [Server], | 9 /// Wraps a [StreamChannel] and handles logic that's shared between [Server], |
| 10 /// [Client], and [Peer]. | 10 /// [Client], and [Peer]. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 if (_listenCalled) { | 49 if (_listenCalled) { |
| 50 throw new StateError("Can only call $_name.listen() once."); | 50 throw new StateError("Can only call $_name.listen() once."); |
| 51 } | 51 } |
| 52 _listenCalled = true; | 52 _listenCalled = true; |
| 53 | 53 |
| 54 _channel.stream.listen(handleInput, | 54 _channel.stream.listen(handleInput, |
| 55 onError: (error, stackTrace) { | 55 onError: (error, stackTrace) { |
| 56 _doneCompleter.completeError(error, stackTrace); | 56 _doneCompleter.completeError(error, stackTrace); |
| 57 _channel.sink.close(); | 57 _channel.sink.close(); |
| 58 }, | 58 }, |
| 59 onDone: _doneCompleter.complete, | 59 onDone: () { |
| 60 if (!_doneCompleter.isCompleted) _doneCompleter.complete(); |
| 61 }, |
| 60 cancelOnError: true); | 62 cancelOnError: true); |
| 61 | 63 |
| 62 return done; | 64 return done; |
| 63 } | 65 } |
| 64 | 66 |
| 65 /// Emit [event]. | 67 /// Emit [event]. |
| 66 void add(event) { | 68 void add(event) { |
| 67 if (isClosed && !_closeCalled) return; | 69 if (isClosed && !_closeCalled) return; |
| 68 _channel.sink.add(event); | 70 _channel.sink.add(event); |
| 69 } | 71 } |
| 70 | 72 |
| 71 /// Closes the channel. | 73 /// Closes the channel. |
| 72 Future close() { | 74 Future close() { |
| 73 _closeCalled = true; | 75 _closeCalled = true; |
| 74 if (!_doneCompleter.isCompleted) { | 76 if (!_doneCompleter.isCompleted) { |
| 75 _doneCompleter.complete(_channel.sink.close()); | 77 _doneCompleter.complete(_channel.sink.close()); |
| 76 } | 78 } |
| 77 return done; | 79 return done; |
| 78 } | 80 } |
| 79 } | 81 } |
| OLD | NEW |