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:typeddata'; | 10 import 'dart:typeddata'; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 /// done. | 157 /// done. |
158 Future writeStreamToSink(Stream stream, EventSink sink) { | 158 Future writeStreamToSink(Stream stream, EventSink sink) { |
159 var completer = new Completer(); | 159 var completer = new Completer(); |
160 stream.listen(sink.add, | 160 stream.listen(sink.add, |
161 onError: sink.addError, | 161 onError: sink.addError, |
162 onDone: () => completer.complete()); | 162 onDone: () => completer.complete()); |
163 return completer.future; | 163 return completer.future; |
164 } | 164 } |
165 | 165 |
166 /// Returns a [Future] that asynchronously completes to `null`. | 166 /// Returns a [Future] that asynchronously completes to `null`. |
167 Future get async => new Future.immediate(null); | 167 Future get async => new Future.value(); |
168 | 168 |
169 /// Returns a closed [Stream] with no elements. | 169 /// Returns a closed [Stream] with no elements. |
170 Stream get emptyStream => streamFromIterable([]); | 170 Stream get emptyStream => streamFromIterable([]); |
171 | 171 |
172 /// Creates a single-subscription stream that emits the items in [iter] and then | 172 /// Creates a single-subscription stream that emits the items in [iter] and then |
173 /// ends. | 173 /// ends. |
174 Stream streamFromIterable(Iterable iter) { | 174 Stream streamFromIterable(Iterable iter) { |
175 var controller = new StreamController(); | 175 var controller = new StreamController(); |
176 iter.forEach(controller.add); | 176 iter.forEach(controller.add); |
177 controller.close(); | 177 controller.close(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // is in. | 227 // is in. |
228 /// Runs [fn] for each element in [input] in order, moving to the next element | 228 /// Runs [fn] for each element in [input] in order, moving to the next element |
229 /// only when the [Future] returned by [fn] completes. Returns a [Future] that | 229 /// only when the [Future] returned by [fn] completes. Returns a [Future] that |
230 /// completes when all elements have been processed. | 230 /// completes when all elements have been processed. |
231 /// | 231 /// |
232 /// The return values of all [Future]s are discarded. Any errors will cause the | 232 /// The return values of all [Future]s are discarded. Any errors will cause the |
233 /// iteration to stop and will be piped through the return value. | 233 /// iteration to stop and will be piped through the return value. |
234 Future forEachFuture(Iterable input, Future fn(element)) { | 234 Future forEachFuture(Iterable input, Future fn(element)) { |
235 var iterator = input.iterator; | 235 var iterator = input.iterator; |
236 Future nextElement(_) { | 236 Future nextElement(_) { |
237 if (!iterator.moveNext()) return new Future.immediate(null); | 237 if (!iterator.moveNext()) return new Future.value(); |
238 return fn(iterator.current).then(nextElement); | 238 return fn(iterator.current).then(nextElement); |
239 } | 239 } |
240 return nextElement(null); | 240 return nextElement(null); |
241 } | 241 } |
OLD | NEW |