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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
| 6 library pub.io; | 6 library pub.io; |
| 7 | 7 |
| 8 import 'dart:async' hide TimeoutException; | 8 import 'dart:async'; |
| 9 export 'dart:async' show TimeoutException; | |
|
Bob Nystrom
2013/12/02 22:40:42
Let's remove this and just change any other files
Lasse Reichstein Nielsen
2013/12/03 08:38:29
Removed.
I don't think it's needed anywhere, but I
| |
| 9 import 'dart:collection'; | 10 import 'dart:collection'; |
| 10 import 'dart:convert'; | 11 import 'dart:convert'; |
| 11 import 'dart:io'; | 12 import 'dart:io'; |
| 12 | 13 |
| 13 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 14 import 'package:http/http.dart' show ByteStream; | 15 import 'package:http/http.dart' show ByteStream; |
| 15 import 'package:stack_trace/stack_trace.dart'; | 16 import 'package:stack_trace/stack_trace.dart'; |
| 16 | 17 |
| 17 import 'error_group.dart'; | 18 import 'error_group.dart'; |
| 18 import 'log.dart' as log; | 19 import 'log.dart' as log; |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 622 } | 623 } |
| 623 | 624 |
| 624 /// Wraps [input] to provide a timeout. If [input] completes before | 625 /// Wraps [input] to provide a timeout. If [input] completes before |
| 625 /// [milliseconds] have passed, then the return value completes in the same way. | 626 /// [milliseconds] have passed, then the return value completes in the same way. |
| 626 /// However, if [milliseconds] pass before [input] has completed, it completes | 627 /// However, if [milliseconds] pass before [input] has completed, it completes |
| 627 /// with a [TimeoutException] with [description] (which should be a fragment | 628 /// with a [TimeoutException] with [description] (which should be a fragment |
| 628 /// describing the action that timed out). | 629 /// describing the action that timed out). |
| 629 /// | 630 /// |
| 630 /// Note that timing out will not cancel the asynchronous operation behind | 631 /// Note that timing out will not cancel the asynchronous operation behind |
| 631 /// [input]. | 632 /// [input]. |
| 632 Future timeout(Future input, int milliseconds, String description) { | 633 Future timeout(Future input, int milliseconds, String description) { |
|
nweiz
2013/12/02 23:54:22
Add a TODO assigned to me to replace this with [Fu
Lasse Reichstein Nielsen
2013/12/03 08:38:29
Done.
| |
| 633 var completer = new Completer(); | 634 var completer = new Completer(); |
| 634 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | 635 var duration = new Duration(milliseconds: milliseconds); |
| 636 var timer = new Timer(duration, () { | |
| 635 completer.completeError(new TimeoutException( | 637 completer.completeError(new TimeoutException( |
| 636 'Timed out while $description.'), | 638 'Timed out while $description.', duration), |
| 637 new Trace.current()); | 639 new Trace.current()); |
| 638 }); | 640 }); |
| 639 input.then((value) { | 641 input.then((value) { |
| 640 if (completer.isCompleted) return; | 642 if (completer.isCompleted) return; |
| 641 timer.cancel(); | 643 timer.cancel(); |
| 642 completer.complete(value); | 644 completer.complete(value); |
| 643 }).catchError((e, stackTrace) { | 645 }).catchError((e, stackTrace) { |
| 644 if (completer.isCompleted) return; | 646 if (completer.isCompleted) return; |
| 645 timer.cancel(); | 647 timer.cancel(); |
| 646 completer.completeError(e, stackTrace); | 648 completer.completeError(e, stackTrace); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 809 }); | 811 }); |
| 810 }).catchError((e, stackTrace) { | 812 }).catchError((e, stackTrace) { |
| 811 // We don't have to worry about double-signaling here, since the store() | 813 // We don't have to worry about double-signaling here, since the store() |
| 812 // above will only be reached if everything succeeds. | 814 // above will only be reached if everything succeeds. |
| 813 controller.addError(e, stackTrace); | 815 controller.addError(e, stackTrace); |
| 814 controller.close(); | 816 controller.close(); |
| 815 }); | 817 }); |
| 816 return new ByteStream(controller.stream); | 818 return new ByteStream(controller.stream); |
| 817 } | 819 } |
| 818 | 820 |
| 819 /// Exception thrown when an operation times out. | |
| 820 class TimeoutException implements Exception { | |
| 821 final String message; | |
| 822 | |
| 823 const TimeoutException(this.message); | |
| 824 | |
| 825 String toString() => message; | |
| 826 } | |
| 827 | |
| 828 /// Contains the results of invoking a [Process] and waiting for it to complete. | 821 /// Contains the results of invoking a [Process] and waiting for it to complete. |
| 829 class PubProcessResult { | 822 class PubProcessResult { |
| 830 final List<String> stdout; | 823 final List<String> stdout; |
| 831 final List<String> stderr; | 824 final List<String> stderr; |
| 832 final int exitCode; | 825 final int exitCode; |
| 833 | 826 |
| 834 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 827 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
| 835 | 828 |
| 836 bool get success => exitCode == 0; | 829 bool get success => exitCode == 0; |
| 837 } | 830 } |
| 838 | 831 |
| 839 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 832 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 840 Uri _getUri(uri) { | 833 Uri _getUri(uri) { |
| 841 if (uri is Uri) return uri; | 834 if (uri is Uri) return uri; |
| 842 return Uri.parse(uri); | 835 return Uri.parse(uri); |
| 843 } | 836 } |
| OLD | NEW |