| 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:crypto'; | 7 import 'dart:crypto'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 /// The return values of all [Future]s are discarded. Any errors will cause the | 178 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 179 /// iteration to stop and will be piped through the return value. | 179 /// iteration to stop and will be piped through the return value. |
| 180 Future forEachFuture(Iterable input, Future fn(element)) { | 180 Future forEachFuture(Iterable input, Future fn(element)) { |
| 181 var iterator = input.iterator(); | 181 var iterator = input.iterator(); |
| 182 Future nextElement(_) { | 182 Future nextElement(_) { |
| 183 if (!iterator.hasNext) return new Future.immediate(null); | 183 if (!iterator.hasNext) return new Future.immediate(null); |
| 184 return fn(iterator.next()).chain(nextElement); | 184 return fn(iterator.next()).chain(nextElement); |
| 185 } | 185 } |
| 186 return nextElement(null); | 186 return nextElement(null); |
| 187 } | 187 } |
| 188 | |
| 189 /// Creates a temporary directory and passes its path to [fn]. Once the [Future] | |
| 190 /// returned by [fn] completes, the temporary directory and all its contents | |
| 191 /// will be deleted. | |
| 192 Future withTempDir(Future fn(String path)) { | |
| 193 var tempDir; | |
| 194 var future = new Directory('').createTemp().chain((dir) { | |
| 195 tempDir = dir; | |
| 196 return fn(tempDir.path); | |
| 197 }); | |
| 198 future.onComplete((_) => tempDir.delete(recursive: true)); | |
| 199 return future; | |
| 200 } | |
| 201 | |
| 202 /// Configures [future] so that its result (success or exception) is passed on | |
| 203 /// to [completer]. | |
| 204 void chainToCompleter(Future future, Completer completer) { | |
| 205 future.handleException((e) { | |
| 206 completer.completeException(e); | |
| 207 return true; | |
| 208 }); | |
| 209 future.then(completer.complete); | |
| 210 } | |
| OLD | NEW |