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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 void _closeTarget() { | 109 void _closeTarget() { |
110 _target.close() | 110 _target.close() |
111 .then((value) => _completeDone(value: value), | 111 .then((value) => _completeDone(value: value), |
112 onError: (error) => _completeDone(error: error)); | 112 onError: (error) => _completeDone(error: error)); |
113 } | 113 } |
114 | 114 |
115 Future get done => _doneFuture; | 115 Future get done => _doneFuture; |
116 | 116 |
117 void _completeDone({value, error}) { | 117 void _completeDone({value, error}) { |
118 if (_doneCompleter == null) return; | 118 if (_doneCompleter == null) return; |
119 var tmp = _doneCompleter; | 119 if (error == null) { |
| 120 _doneCompleter.complete(value); |
| 121 } else { |
| 122 _doneCompleter.completeError(error); |
| 123 } |
120 _doneCompleter = null; | 124 _doneCompleter = null; |
121 if (error == null) { | |
122 tmp.complete(value); | |
123 } else { | |
124 tmp.completeError(error); | |
125 } | |
126 } | 125 } |
127 | 126 |
128 StreamController<T> get _controller { | 127 StreamController<T> get _controller { |
129 if (_isBound) { | 128 if (_isBound) { |
130 throw new StateError("StreamSink is bound to a stream"); | 129 throw new StateError("StreamSink is bound to a stream"); |
131 } | 130 } |
132 if (_isClosed) { | 131 if (_isClosed) { |
133 throw new StateError("StreamSink is closed"); | 132 throw new StateError("StreamSink is closed"); |
134 } | 133 } |
135 if (_controllerInstance == null) { | 134 if (_controllerInstance == null) { |
136 _controllerInstance = new StreamController<T>(); | 135 _controllerInstance = new StreamController<T>(); |
137 _controllerCompleter = new Completer(); | 136 _controllerCompleter = new Completer(); |
138 _target.addStream(_controller.stream) | 137 _target.addStream(_controller.stream) |
139 .then( | 138 .then( |
140 (_) { | 139 (_) { |
141 if (_isBound) { | 140 if (_isBound) { |
142 // A new stream takes over - forward values to that stream. | 141 // A new stream takes over - forward values to that stream. |
143 var completer = _controllerCompleter; | 142 _controllerCompleter.complete(); |
144 _controllerCompleter = null; | 143 _controllerCompleter = null; |
145 _controllerInstance = null; | 144 _controllerInstance = null; |
146 completer.complete(); | |
147 } else { | 145 } else { |
148 // No new stream, .close was called. Close _target. | 146 // No new stream, .close was called. Close _target. |
149 _closeTarget(); | 147 _closeTarget(); |
150 } | 148 } |
151 }, | 149 }, |
152 onError: (error) { | 150 onError: (error) { |
153 if (_isBound) { | 151 if (_isBound) { |
154 // A new stream takes over - forward errors to that stream. | 152 // A new stream takes over - forward errors to that stream. |
155 var completer = _controllerCompleter; | 153 _controllerCompleter.completeError(error); |
156 _controllerCompleter = null; | 154 _controllerCompleter = null; |
157 _controllerInstance = null; | 155 _controllerInstance = null; |
158 completer.completeError(error); | |
159 } else { | 156 } else { |
160 // No new stream. No need to close target, as it have already | 157 // No new stream. No need to close target, as it have already |
161 // failed. | 158 // failed. |
162 _completeDone(error: error); | 159 _completeDone(error: error); |
163 } | 160 } |
164 }); | 161 }); |
165 } | 162 } |
166 return _controllerInstance; | 163 return _controllerInstance; |
167 } | 164 } |
168 } | 165 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 216 |
220 void writeln([Object obj = ""]) { | 217 void writeln([Object obj = ""]) { |
221 write(obj); | 218 write(obj); |
222 write("\n"); | 219 write("\n"); |
223 } | 220 } |
224 | 221 |
225 void writeCharCode(int charCode) { | 222 void writeCharCode(int charCode) { |
226 write(new String.fromCharCode(charCode)); | 223 write(new String.fromCharCode(charCode)); |
227 } | 224 } |
228 } | 225 } |
OLD | NEW |