| 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 169 |
| 170 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ | 170 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ |
| 171 // is in. | 171 // is in. |
| 172 /// Runs [fn] for each element in [input] in order, moving to the next element | 172 /// Runs [fn] for each element in [input] in order, moving to the next element |
| 173 /// only when the [Future] returned by [fn] completes. Returns a [Future] that | 173 /// only when the [Future] returned by [fn] completes. Returns a [Future] that |
| 174 /// completes when all elements have been processed. | 174 /// completes when all elements have been processed. |
| 175 /// | 175 /// |
| 176 /// The return values of all [Future]s are discarded. Any errors will cause the | 176 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 177 /// iteration to stop and will be piped through the return value. | 177 /// iteration to stop and will be piped through the return value. |
| 178 Future forEachFuture(Iterable input, Future fn(element)) { | 178 Future forEachFuture(Iterable input, Future fn(element)) { |
| 179 var iterator = input.iterator(); | 179 var iterator = input.iterator; |
| 180 Future nextElement(_) { | 180 Future nextElement(_) { |
| 181 if (!iterator.hasNext) return new Future.immediate(null); | 181 if (!iterator.moveNext()) return new Future.immediate(null); |
| 182 return fn(iterator.next()).chain(nextElement); | 182 return fn(iterator.current).chain(nextElement); |
| 183 } | 183 } |
| 184 return nextElement(null); | 184 return nextElement(null); |
| 185 } | 185 } |
| 186 | 186 |
| 187 /// Creates a temporary directory and passes its path to [fn]. Once the [Future] | 187 /// Creates a temporary directory and passes its path to [fn]. Once the [Future] |
| 188 /// returned by [fn] completes, the temporary directory and all its contents | 188 /// returned by [fn] completes, the temporary directory and all its contents |
| 189 /// will be deleted. | 189 /// will be deleted. |
| 190 Future withTempDir(Future fn(String path)) { | 190 Future withTempDir(Future fn(String path)) { |
| 191 var tempDir; | 191 var tempDir; |
| 192 var future = new Directory('').createTemp().chain((dir) { | 192 var future = new Directory('').createTemp().chain((dir) { |
| 193 tempDir = dir; | 193 tempDir = dir; |
| 194 return fn(tempDir.path); | 194 return fn(tempDir.path); |
| 195 }); | 195 }); |
| 196 future.onComplete((_) => tempDir.delete(recursive: true)); | 196 future.onComplete((_) => tempDir.delete(recursive: true)); |
| 197 return future; | 197 return future; |
| 198 } | 198 } |
| 199 | 199 |
| 200 /// Configures [future] so that its result (success or exception) is passed on | 200 /// Configures [future] so that its result (success or exception) is passed on |
| 201 /// to [completer]. | 201 /// to [completer]. |
| 202 void chainToCompleter(Future future, Completer completer) { | 202 void chainToCompleter(Future future, Completer completer) { |
| 203 future.handleException((e) { | 203 future.handleException((e) { |
| 204 completer.completeException(e); | 204 completer.completeException(e); |
| 205 return true; | 205 return true; |
| 206 }); | 206 }); |
| 207 future.then(completer.complete); | 207 future.then(completer.complete); |
| 208 } | 208 } |
| OLD | NEW |