Chromium Code Reviews| 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Helper class to wrap a [StreamConsumer<List<int>>] and provide | 8 * Helper class to wrap a [StreamConsumer<List<int>>] and provide |
| 9 * utility functions for writing to the StreamConsumer directly. The | 9 * utility functions for writing to the StreamConsumer directly. The |
| 10 * [IOSink] buffers the input given by all [StringSink] methods and will delay | 10 * [IOSink] buffers the input given by all [StringSink] methods and will delay |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 } | 55 } |
| 56 | 56 |
| 57 class _StreamSinkImpl<T> implements StreamSink<T> { | 57 class _StreamSinkImpl<T> implements StreamSink<T> { |
| 58 final StreamConsumer<T> _target; | 58 final StreamConsumer<T> _target; |
| 59 Completer _doneCompleter = new Completer(); | 59 Completer _doneCompleter = new Completer(); |
| 60 Future _doneFuture; | 60 Future _doneFuture; |
| 61 StreamController<T> _controllerInstance; | 61 StreamController<T> _controllerInstance; |
| 62 Completer _controllerCompleter; | 62 Completer _controllerCompleter; |
| 63 bool _isClosed = false; | 63 bool _isClosed = false; |
| 64 bool _isBound = false; | 64 bool _isBound = false; |
| 65 bool _hasError = false; | |
| 65 | 66 |
| 66 _StreamSinkImpl(StreamConsumer<T> this._target) { | 67 _StreamSinkImpl(StreamConsumer<T> this._target) { |
| 67 _doneFuture = _doneCompleter.future; | 68 _doneFuture = _doneCompleter.future; |
| 68 } | 69 } |
| 69 | 70 |
| 70 void add(T data) { | 71 void add(T data) { |
| 71 _controller.add(data); | 72 _controller.add(data); |
| 72 } | 73 } |
| 73 | 74 |
| 74 void addError(error) { | 75 void addError(error) { |
| 75 _controller.addError(error); | 76 _controller.addError(error); |
| 76 } | 77 } |
| 77 | 78 |
| 78 Future addStream(Stream<T> stream) { | 79 Future addStream(Stream<T> stream) { |
| 79 if (_isBound) { | 80 if (_isBound) { |
| 80 throw new StateError("StreamSink is already bound to a stream"); | 81 throw new StateError("StreamSink is already bound to a stream"); |
| 81 } | 82 } |
| 82 _isBound = true; | 83 _isBound = true; |
| 84 if (_hasError) return done; | |
| 83 // Wait for any sync operations to complete. | 85 // Wait for any sync operations to complete. |
| 84 Future targetAddStream() { | 86 Future targetAddStream() { |
| 85 return _target.addStream(stream) | 87 return _target.addStream(stream) |
| 86 .whenComplete(() { | 88 .whenComplete(() { |
| 87 _isBound = false; | 89 _isBound = false; |
| 88 }); | 90 }); |
| 89 } | 91 } |
| 90 if (_controllerInstance == null) return targetAddStream(); | 92 if (_controllerInstance == null) return targetAddStream(); |
| 91 var future = _controllerCompleter.future; | 93 var future = _controllerCompleter.future; |
| 92 _controllerInstance.close(); | 94 _controllerInstance.close(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 114 onError: (error) => _completeDone(error: error)); | 116 onError: (error) => _completeDone(error: error)); |
| 115 } | 117 } |
| 116 | 118 |
| 117 Future get done => _doneFuture; | 119 Future get done => _doneFuture; |
| 118 | 120 |
| 119 void _completeDone({value, error}) { | 121 void _completeDone({value, error}) { |
| 120 if (_doneCompleter == null) return; | 122 if (_doneCompleter == null) return; |
| 121 if (error == null) { | 123 if (error == null) { |
| 122 _doneCompleter.complete(value); | 124 _doneCompleter.complete(value); |
| 123 } else { | 125 } else { |
| 126 _hasError = true; | |
|
Bill Hesse
2013/07/01 11:18:05
Do you also want to set hasError in the case that
Anders Johnsen
2013/07/01 11:18:57
No, it's just a guard to avoid us setting it twice
| |
| 124 _doneCompleter.completeError(error); | 127 _doneCompleter.completeError(error); |
| 125 } | 128 } |
| 126 _doneCompleter = null; | 129 _doneCompleter = null; |
| 127 } | 130 } |
| 128 | 131 |
| 129 StreamController<T> get _controller { | 132 StreamController<T> get _controller { |
| 130 if (_isBound) { | 133 if (_isBound) { |
| 131 throw new StateError("StreamSink is bound to a stream"); | 134 throw new StateError("StreamSink is bound to a stream"); |
| 132 } | 135 } |
| 133 if (_isClosed) { | 136 if (_isClosed) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 221 |
| 219 void writeln([Object obj = ""]) { | 222 void writeln([Object obj = ""]) { |
| 220 write(obj); | 223 write(obj); |
| 221 write("\n"); | 224 write("\n"); |
| 222 } | 225 } |
| 223 | 226 |
| 224 void writeCharCode(int charCode) { | 227 void writeCharCode(int charCode) { |
| 225 write(new String.fromCharCode(charCode)); | 228 write(new String.fromCharCode(charCode)); |
| 226 } | 229 } |
| 227 } | 230 } |
| OLD | NEW |