Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: pkg/http/lib/src/utils.dart

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/unittest/lib/mock.dart » ('j') | runtime/lib/array.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« no previous file with comments | « no previous file | pkg/unittest/lib/mock.dart » ('j') | runtime/lib/array.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698