Chromium Code Reviews| 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 utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// Configures [future] so that its result (success or exception) is passed on | 9 /// Configures [future] so that its result (success or exception) is passed on |
| 10 /// to [completer]. | 10 /// to [completer]. |
| 11 void chainToCompleter(Future future, Completer completer) { | 11 void chainToCompleter(Future future, Completer completer) { |
| 12 future.then((value) => completer.complete(value), | 12 future.then((value) => completer.complete(value), |
| 13 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 13 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
| 14 } | 14 } |
| 15 | 15 |
| 16 /// Prepends each line in [text] with [prefix]. | 16 /// Prepends each line in [text] with [prefix]. |
| 17 String prefixLines(String text, {String prefix: '| '}) => | 17 String prefixLines(String text, {String prefix: '| '}) => |
| 18 text.split('\n').map((line) => '$prefix$line').join('\n'); | 18 text.split('\n').map((line) => '$prefix$line').join('\n'); |
| 19 | 19 |
| 20 /// Returns a [Future] that completes in [milliseconds]. | 20 /// Returns a [Future] that completes in at least [milliseconds]. |
| 21 /// | |
| 22 /// This pumps the event loop after every second of sleeping. This should ensure | |
|
Bob Nystrom
2013/02/12 00:35:19
second -> millisecond.
nweiz
2013/02/12 00:52:19
Done.
| |
| 23 /// that even if the CPU is pegged and code is running much slower than | |
| 24 /// expected, the sleep will take longer than any non-sleep code that's running. | |
| 21 Future sleep(int milliseconds) { | 25 Future sleep(int milliseconds) { |
| 26 if (milliseconds == 0) return new Future.immediate(null); | |
| 27 return _sleep1().then((_) => sleep(milliseconds - 1)); | |
| 28 } | |
| 29 | |
| 30 /// Returns a [Future] that completes in one millisecond. | |
| 31 Future _sleep1() { | |
| 22 var completer = new Completer(); | 32 var completer = new Completer(); |
| 23 new Timer(milliseconds, (_) => completer.complete()); | 33 new Timer(1, (_) => completer.complete()); |
| 24 return completer.future; | 34 return completer.future; |
| 25 } | 35 } |
| 26 | 36 |
| 27 /// Wraps [input] to provide a timeout. If [input] completes before | 37 /// Wraps [input] to provide a timeout. If [input] completes before |
| 28 /// [milliseconds] have passed, then the return value completes in the same way. | 38 /// [milliseconds] have passed, then the return value completes in the same way. |
| 29 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | 39 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is |
| 30 /// run and its result is passed to [input] (with chaining, if it returns a | 40 /// run and its result is passed to [input] (with chaining, if it returns a |
| 31 /// [Future]). | 41 /// [Future]). |
| 32 /// | 42 /// |
| 33 /// Note that timing out will not cancel the asynchronous operation behind | 43 /// Note that timing out will not cancel the asynchronous operation behind |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 44 if (completed) return; | 54 if (completed) return; |
| 45 timer.cancel(); | 55 timer.cancel(); |
| 46 completer.complete(value); | 56 completer.complete(value); |
| 47 }).catchError((e) { | 57 }).catchError((e) { |
| 48 if (completed) return; | 58 if (completed) return; |
| 49 timer.cancel(); | 59 timer.cancel(); |
| 50 completer.completeError(e.error, e.stackTrace); | 60 completer.completeError(e.error, e.stackTrace); |
| 51 }); | 61 }); |
| 52 return completer.future; | 62 return completer.future; |
| 53 } | 63 } |
| OLD | NEW |