OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:crypto'; | 8 import 'dart:crypto'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 /// place of [stream] after calling this method. | 145 /// place of [stream] after calling this method. |
146 Stream onDone(Stream stream, void onDone()) { | 146 Stream onDone(Stream stream, void onDone()) { |
147 var pair = tee(stream); | 147 var pair = tee(stream); |
148 pair.first.listen((_) {}, onError: (_) {}, onDone: onDone); | 148 pair.first.listen((_) {}, onError: (_) {}, onDone: onDone); |
149 return pair.last; | 149 return pair.last; |
150 } | 150 } |
151 | 151 |
152 // TODO(nweiz): remove this when issue 7786 is fixed. | 152 // TODO(nweiz): remove this when issue 7786 is fixed. |
153 /// Pipes all data and errors from [stream] into [sink]. When [stream] is done, | 153 /// Pipes all data and errors from [stream] into [sink]. When [stream] is done, |
154 /// [sink] is closed and the returned [Future] is completed. | 154 /// [sink] is closed and the returned [Future] is completed. |
155 Future store(Stream stream, StreamSink sink) { | 155 Future store(Stream stream, EventSink sink) { |
156 var completer = new Completer(); | 156 var completer = new Completer(); |
157 stream.listen(sink.add, | 157 stream.listen(sink.add, |
158 onError: sink.signalError, | 158 onError: sink.addError, |
159 onDone: () { | 159 onDone: () { |
160 sink.close(); | 160 sink.close(); |
161 completer.complete(); | 161 completer.complete(); |
162 }); | 162 }); |
163 return completer.future; | 163 return completer.future; |
164 } | 164 } |
165 | 165 |
166 /// Pipes all data and errors from [stream] into [sink]. Completes [Future] once | 166 /// Pipes all data and errors from [stream] into [sink]. Completes [Future] once |
167 /// [stream] is done. Unlike [store], [sink] remains open after [stream] is | 167 /// [stream] is done. Unlike [store], [sink] remains open after [stream] is |
168 /// done. | 168 /// done. |
169 Future writeStreamToSink(Stream stream, StreamSink sink) { | 169 Future writeStreamToSink(Stream stream, EventSink sink) { |
170 var completer = new Completer(); | 170 var completer = new Completer(); |
171 stream.listen(sink.add, | 171 stream.listen(sink.add, |
172 onError: sink.signalError, | 172 onError: sink.addError, |
173 onDone: () => completer.complete()); | 173 onDone: () => completer.complete()); |
174 return completer.future; | 174 return completer.future; |
175 } | 175 } |
176 | 176 |
177 /// Returns a [Future] that asynchronously completes to `null`. | 177 /// Returns a [Future] that asynchronously completes to `null`. |
178 Future get async => new Future.immediate(null); | 178 Future get async => new Future.immediate(null); |
179 | 179 |
180 /// Returns a closed [Stream] with no elements. | 180 /// Returns a closed [Stream] with no elements. |
181 Stream get emptyStream => streamFromIterable([]); | 181 Stream get emptyStream => streamFromIterable([]); |
182 | 182 |
(...skipping 10 matching lines...) Expand all Loading... |
193 /// Creates two single-subscription [Stream]s that each emit all values and | 193 /// Creates two single-subscription [Stream]s that each emit all values and |
194 /// errors from [stream]. This is useful if [stream] is single-subscription but | 194 /// errors from [stream]. This is useful if [stream] is single-subscription but |
195 /// multiple subscribers are necessary. | 195 /// multiple subscribers are necessary. |
196 Pair<Stream, Stream> tee(Stream stream) { | 196 Pair<Stream, Stream> tee(Stream stream) { |
197 var controller1 = new StreamController(); | 197 var controller1 = new StreamController(); |
198 var controller2 = new StreamController(); | 198 var controller2 = new StreamController(); |
199 stream.listen((value) { | 199 stream.listen((value) { |
200 controller1.add(value); | 200 controller1.add(value); |
201 controller2.add(value); | 201 controller2.add(value); |
202 }, onError: (error) { | 202 }, onError: (error) { |
203 controller1.signalError(error); | 203 controller1.addError(error); |
204 controller2.signalError(error); | 204 controller2.addError(error); |
205 }, onDone: () { | 205 }, onDone: () { |
206 controller1.close(); | 206 controller1.close(); |
207 controller2.close(); | 207 controller2.close(); |
208 }); | 208 }); |
209 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | 209 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
210 } | 210 } |
211 | 211 |
212 /// A pair of values. | 212 /// A pair of values. |
213 class Pair<E, F> { | 213 class Pair<E, F> { |
214 E first; | 214 E first; |
(...skipping 28 matching lines...) Expand all Loading... |
243 /// The return values of all [Future]s are discarded. Any errors will cause the | 243 /// The return values of all [Future]s are discarded. Any errors will cause the |
244 /// iteration to stop and will be piped through the return value. | 244 /// iteration to stop and will be piped through the return value. |
245 Future forEachFuture(Iterable input, Future fn(element)) { | 245 Future forEachFuture(Iterable input, Future fn(element)) { |
246 var iterator = input.iterator; | 246 var iterator = input.iterator; |
247 Future nextElement(_) { | 247 Future nextElement(_) { |
248 if (!iterator.moveNext()) return new Future.immediate(null); | 248 if (!iterator.moveNext()) return new Future.immediate(null); |
249 return fn(iterator.current).then(nextElement); | 249 return fn(iterator.current).then(nextElement); |
250 } | 250 } |
251 return nextElement(null); | 251 return nextElement(null); |
252 } | 252 } |
OLD | NEW |