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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library io; | 6 library io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 | 584 |
585 /// Wraps [input] to provide a timeout. If [input] completes before | 585 /// Wraps [input] to provide a timeout. If [input] completes before |
586 /// [milliseconds] have passed, then the return value completes in the same way. | 586 /// [milliseconds] have passed, then the return value completes in the same way. |
587 /// However, if [milliseconds] pass before [input] has completed, it completes | 587 /// However, if [milliseconds] pass before [input] has completed, it completes |
588 /// with a [TimeoutException] with [description] (which should be a fragment | 588 /// with a [TimeoutException] with [description] (which should be a fragment |
589 /// describing the action that timed out). | 589 /// describing the action that timed out). |
590 /// | 590 /// |
591 /// Note that timing out will not cancel the asynchronous operation behind | 591 /// Note that timing out will not cancel the asynchronous operation behind |
592 /// [input]. | 592 /// [input]. |
593 Future timeout(Future input, int milliseconds, String description) { | 593 Future timeout(Future input, int milliseconds, String description) { |
594 bool completed = false; | |
595 var completer = new Completer(); | 594 var completer = new Completer(); |
596 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | 595 var timer = new Timer(new Duration(milliseconds: milliseconds), () { |
597 completed = true; | |
598 completer.completeError(new TimeoutException( | 596 completer.completeError(new TimeoutException( |
599 'Timed out while $description.')); | 597 'Timed out while $description.')); |
600 }); | 598 }); |
601 input.then((value) { | 599 input.then((value) { |
602 if (completed) return; | 600 if (completer.isCompleted) return; |
603 timer.cancel(); | 601 timer.cancel(); |
604 completer.complete(value); | 602 completer.complete(value); |
605 }).catchError((e) { | 603 }).catchError((e) { |
606 if (completed) return; | 604 if (completer.isCompleted) return; |
607 timer.cancel(); | 605 timer.cancel(); |
608 completer.completeError(e.error, e.stackTrace); | 606 completer.completeError(e.error, e.stackTrace); |
609 }); | 607 }); |
610 return completer.future; | 608 return completer.future; |
611 } | 609 } |
612 | 610 |
613 /// Creates a temporary directory and passes its path to [fn]. Once the [Future] | 611 /// Creates a temporary directory and passes its path to [fn]. Once the [Future] |
614 /// returned by [fn] completes, the temporary directory and all its contents | 612 /// returned by [fn] completes, the temporary directory and all its contents |
615 /// will be deleted. | 613 /// will be deleted. |
616 /// | 614 /// |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 802 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
805 | 803 |
806 bool get success => exitCode == 0; | 804 bool get success => exitCode == 0; |
807 } | 805 } |
808 | 806 |
809 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 807 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
810 Uri _getUri(uri) { | 808 Uri _getUri(uri) { |
811 if (uri is Uri) return uri; | 809 if (uri is Uri) return uri; |
812 return Uri.parse(uri); | 810 return Uri.parse(uri); |
813 } | 811 } |
OLD | NEW |