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

Side by Side Diff: utils/pub/utils.dart

Issue 14070010: Refactor Future constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added co19 issue number. Created 7 years, 8 months 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 | « utils/pub/source.dart ('k') | utils/pub/validator/compiled_dartdoc.dart » ('j') | no next file with comments »
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 /// Generic utility functions. Stuff that should possibly be in core. 5 /// Generic utility functions. Stuff that should possibly be in core.
6 library utils; 6 library utils;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 import 'dart:crypto'; 9 import 'dart:crypto';
10 import 'dart:isolate'; 10 import 'dart:isolate';
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 if (!buffer.isEmpty) sink.add(buffer.toString()); 242 if (!buffer.isEmpty) sink.add(buffer.toString());
243 sink.close(); 243 sink.close();
244 })); 244 }));
245 } 245 }
246 246
247 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the 247 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the
248 /// results of those [Future]s as the test. 248 /// results of those [Future]s as the test.
249 Future<Iterable> futureWhere(Iterable iter, test(value)) { 249 Future<Iterable> futureWhere(Iterable iter, test(value)) {
250 return Future.wait(iter.map((e) { 250 return Future.wait(iter.map((e) {
251 var result = test(e); 251 var result = test(e);
252 if (result is! Future) result = new Future.immediate(result); 252 if (result is! Future) result = new Future.value(result);
253 return result.then((result) => new Pair(e, result)); 253 return result.then((result) => new Pair(e, result));
254 })) 254 }))
255 .then((pairs) => pairs.where((pair) => pair.last)) 255 .then((pairs) => pairs.where((pair) => pair.last))
256 .then((pairs) => pairs.map((pair) => pair.first)); 256 .then((pairs) => pairs.map((pair) => pair.first));
257 } 257 }
258 258
259 // TODO(nweiz): unify the following functions with the utility functions in 259 // TODO(nweiz): unify the following functions with the utility functions in
260 // pkg/http. 260 // pkg/http.
261 261
262 /// Like [String.split], but only splits on the first occurrence of the pattern. 262 /// Like [String.split], but only splits on the first occurrence of the pattern.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 336
337 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar 337 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar
338 /// objects, and [Future]s) and recursively resolves all the [Future]s contained 338 /// objects, and [Future]s) and recursively resolves all the [Future]s contained
339 /// within. Completes with the fully resolved structure. 339 /// within. Completes with the fully resolved structure.
340 Future awaitObject(object) { 340 Future awaitObject(object) {
341 // Unroll nested futures. 341 // Unroll nested futures.
342 if (object is Future) return object.then(awaitObject); 342 if (object is Future) return object.then(awaitObject);
343 if (object is Iterable) { 343 if (object is Iterable) {
344 return Future.wait(object.map(awaitObject).toList()); 344 return Future.wait(object.map(awaitObject).toList());
345 } 345 }
346 if (object is! Map) return new Future.immediate(object); 346 if (object is! Map) return new Future.value(object);
347 347
348 var pairs = <Future<Pair>>[]; 348 var pairs = <Future<Pair>>[];
349 object.forEach((key, value) { 349 object.forEach((key, value) {
350 pairs.add(awaitObject(value) 350 pairs.add(awaitObject(value)
351 .then((resolved) => new Pair(key, resolved))); 351 .then((resolved) => new Pair(key, resolved)));
352 }); 352 });
353 return Future.wait(pairs).then((resolvedPairs) { 353 return Future.wait(pairs).then((resolvedPairs) {
354 var map = {}; 354 var map = {};
355 for (var pair in resolvedPairs) { 355 for (var pair in resolvedPairs) {
356 map[pair.first] = pair.last; 356 map[pair.first] = pair.last;
357 } 357 }
358 return map; 358 return map;
359 }); 359 });
360 } 360 }
OLDNEW
« no previous file with comments | « utils/pub/source.dart ('k') | utils/pub/validator/compiled_dartdoc.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698