OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 test.utils; | 5 library test.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
10 | 10 |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 CancelableFuture cancelableNext(StreamQueue queue) { | 332 CancelableFuture cancelableNext(StreamQueue queue) { |
333 var fork = queue.fork(); | 333 var fork = queue.fork(); |
334 var completer = new CancelableCompleter(() => fork.cancel(immediate: true)); | 334 var completer = new CancelableCompleter(() => fork.cancel(immediate: true)); |
335 completer.complete(fork.next.then((_) { | 335 completer.complete(fork.next.then((_) { |
336 fork.cancel(); | 336 fork.cancel(); |
337 return queue.next; | 337 return queue.next; |
338 })); | 338 })); |
339 return completer.future; | 339 return completer.future; |
340 } | 340 } |
341 | 341 |
342 /// Returns the result of whichever of [futures] completes first, and cancels | 342 /// Returns a single-subscription stream that emits the results of [futures] in |
343 /// the others. | 343 /// the order they complete. |
344 Future race(Iterable<CancelableFuture> futures) { | 344 /// |
345 var completer = new Completer.sync(); | 345 /// If any futures in [futures] are [CancelableFuture]s, this will cancel them |
346 for (var future in futures) { | 346 /// if the subscription is canceled. |
347 future.then((value) { | 347 Stream inCompletionOrder(Iterable<Future> futures) { |
348 if (!completer.isCompleted) completer.complete(value); | 348 var futureSet = futures.toSet(); |
349 }).catchError((error, stackTrace) { | 349 var controller = new StreamController(sync: true, onCancel: () { |
350 if (!completer.isCompleted) completer.completeError(error, stackTrace); | 350 return Future.wait(futureSet.map((future) { |
| 351 return future is CancelableFuture ? future.cancel() : null; |
| 352 }).where((future) => future != null)); |
| 353 }); |
| 354 |
| 355 for (var future in futureSet) { |
| 356 future.then(controller.add).catchError(controller.addError) |
| 357 .whenComplete(() { |
| 358 futureSet.remove(future); |
| 359 if (futureSet.isEmpty) controller.close(); |
351 }); | 360 }); |
352 } | 361 } |
353 | 362 |
354 return completer.future.whenComplete(() { | 363 return controller.stream; |
355 for (var future in futures) { | |
356 future.cancel(); | |
357 } | |
358 }); | |
359 } | 364 } |
360 | 365 |
361 /// Returns a stream that emits [error] and [stackTrace], then closes. | 366 /// Returns a stream that emits [error] and [stackTrace], then closes. |
362 /// | 367 /// |
363 /// This is useful for adding errors to streams defined via `async*`. | 368 /// This is useful for adding errors to streams defined via `async*`. |
364 Stream errorStream(error, StackTrace stackTrace) { | 369 Stream errorStream(error, StackTrace stackTrace) { |
365 var controller = new StreamController(); | 370 var controller = new StreamController(); |
366 controller.addError(error, stackTrace); | 371 controller.addError(error, stackTrace); |
367 controller.close(); | 372 controller.close(); |
368 return controller.stream; | 373 return controller.stream; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 } | 432 } |
428 } | 433 } |
429 | 434 |
430 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 435 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
431 shelf.Middleware nestingMiddleware(String beneath) { | 436 shelf.Middleware nestingMiddleware(String beneath) { |
432 return (handler) { | 437 return (handler) { |
433 var pathHandler = new PathHandler()..add(beneath, handler); | 438 var pathHandler = new PathHandler()..add(beneath, handler); |
434 return pathHandler.handler; | 439 return pathHandler.handler; |
435 }; | 440 }; |
436 } | 441 } |
OLD | NEW |