| 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 /// 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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 if (!buffer.isEmpty) sink.add(buffer.toString()); | 241 if (!buffer.isEmpty) sink.add(buffer.toString()); |
| 242 sink.close(); | 242 sink.close(); |
| 243 })); | 243 })); |
| 244 } | 244 } |
| 245 | 245 |
| 246 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the | 246 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the |
| 247 /// results of those [Future]s as the test. | 247 /// results of those [Future]s as the test. |
| 248 Future<Iterable> futureWhere(Iterable iter, test(value)) { | 248 Future<Iterable> futureWhere(Iterable iter, test(value)) { |
| 249 return Future.wait(iter.map((e) { | 249 return Future.wait(iter.map((e) { |
| 250 var result = test(e); | 250 var result = test(e); |
| 251 if (result is! Future) result = new Future.immediate(result); | 251 if (result is! Future) result = new Future.value(result); |
| 252 return result.then((result) => new Pair(e, result)); | 252 return result.then((result) => new Pair(e, result)); |
| 253 })) | 253 })) |
| 254 .then((pairs) => pairs.where((pair) => pair.last)) | 254 .then((pairs) => pairs.where((pair) => pair.last)) |
| 255 .then((pairs) => pairs.map((pair) => pair.first)); | 255 .then((pairs) => pairs.map((pair) => pair.first)); |
| 256 } | 256 } |
| 257 | 257 |
| 258 // TODO(nweiz): unify the following functions with the utility functions in | 258 // TODO(nweiz): unify the following functions with the utility functions in |
| 259 // pkg/http. | 259 // pkg/http. |
| 260 | 260 |
| 261 /// Like [String.split], but only splits on the first occurrence of the pattern. | 261 /// 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 Loading... |
| 335 | 335 |
| 336 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar | 336 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar |
| 337 /// objects, and [Future]s) and recursively resolves all the [Future]s contained | 337 /// objects, and [Future]s) and recursively resolves all the [Future]s contained |
| 338 /// within. Completes with the fully resolved structure. | 338 /// within. Completes with the fully resolved structure. |
| 339 Future awaitObject(object) { | 339 Future awaitObject(object) { |
| 340 // Unroll nested futures. | 340 // Unroll nested futures. |
| 341 if (object is Future) return object.then(awaitObject); | 341 if (object is Future) return object.then(awaitObject); |
| 342 if (object is Iterable) { | 342 if (object is Iterable) { |
| 343 return Future.wait(object.map(awaitObject).toList()); | 343 return Future.wait(object.map(awaitObject).toList()); |
| 344 } | 344 } |
| 345 if (object is! Map) return new Future.immediate(object); | 345 if (object is! Map) return new Future.value(object); |
| 346 | 346 |
| 347 var pairs = <Future<Pair>>[]; | 347 var pairs = <Future<Pair>>[]; |
| 348 object.forEach((key, value) { | 348 object.forEach((key, value) { |
| 349 pairs.add(awaitObject(value) | 349 pairs.add(awaitObject(value) |
| 350 .then((resolved) => new Pair(key, resolved))); | 350 .then((resolved) => new Pair(key, resolved))); |
| 351 }); | 351 }); |
| 352 return Future.wait(pairs).then((resolvedPairs) { | 352 return Future.wait(pairs).then((resolvedPairs) { |
| 353 var map = {}; | 353 var map = {}; |
| 354 for (var pair in resolvedPairs) { | 354 for (var pair in resolvedPairs) { |
| 355 map[pair.first] = pair.last; | 355 map[pair.first] = pair.last; |
| 356 } | 356 } |
| 357 return map; | 357 return map; |
| 358 }); | 358 }); |
| 359 } | 359 } |
| OLD | NEW |