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 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 }, onError: (error, stackTrace) { | 316 }, onError: (error, stackTrace) { |
317 completer.completeError(error, stackTrace); | 317 completer.completeError(error, stackTrace); |
318 subscription.cancel(); | 318 subscription.cancel(); |
319 }, onDone: () { | 319 }, onDone: () { |
320 completer.complete(); | 320 completer.complete(); |
321 }); | 321 }); |
322 | 322 |
323 return completer.future; | 323 return completer.future; |
324 } | 324 } |
325 | 325 |
| 326 /// Returns a [CancelableFuture] that returns the next value of [queue] unless |
| 327 /// it's canceled. |
| 328 /// |
| 329 /// If the future is canceled, [queue] is not moved forward at all. Note that |
| 330 /// it's not safe to call further methods on [queue] until this future has |
| 331 /// either completed or been canceled. |
326 CancelableFuture cancelableNext(StreamQueue queue) { | 332 CancelableFuture cancelableNext(StreamQueue queue) { |
327 var fork = queue.fork(); | 333 var fork = queue.fork(); |
328 var completer = new CancelableCompleter(() => fork.cancel(immediate: true)); | 334 var completer = new CancelableCompleter(() => fork.cancel(immediate: true)); |
329 completer.complete(fork.next.then((_) { | 335 completer.complete(fork.next.then((_) { |
330 fork.cancel(); | 336 fork.cancel(); |
331 return queue.next; | 337 return queue.next; |
332 })); | 338 })); |
333 return completer.future; | 339 return completer.future; |
334 } | 340 } |
335 | 341 |
| 342 /// Returns the result of whichever of [futures] completes first, and cancels |
| 343 /// the others. |
336 Future race(Iterable<CancelableFuture> futures) { | 344 Future race(Iterable<CancelableFuture> futures) { |
337 var completer = new Completer.sync(); | 345 var completer = new Completer.sync(); |
338 for (var future in futures) { | 346 for (var future in futures) { |
339 future.then((value) { | 347 future.then((value) { |
340 if (!completer.isCompleted) completer.complete(value); | 348 if (!completer.isCompleted) completer.complete(value); |
341 }).catchError((error, stackTrace) { | 349 }).catchError((error, stackTrace) { |
342 if (!completer.isCompleted) completer.completeError(error, stackTrace); | 350 if (!completer.isCompleted) completer.completeError(error, stackTrace); |
343 }); | 351 }); |
344 } | 352 } |
345 | 353 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 427 } |
420 } | 428 } |
421 | 429 |
422 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 430 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
423 shelf.Middleware nestingMiddleware(String beneath) { | 431 shelf.Middleware nestingMiddleware(String beneath) { |
424 return (handler) { | 432 return (handler) { |
425 var pathHandler = new PathHandler()..add(beneath, handler); | 433 var pathHandler = new PathHandler()..add(beneath, handler); |
426 return pathHandler.handler; | 434 return pathHandler.handler; |
427 }; | 435 }; |
428 } | 436 } |
OLD | NEW |