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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library pub.utils; | 6 library pub.utils; |
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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 completed = true; | 65 completed = true; |
66 _completer.completeError(e); | 66 _completer.completeError(e); |
67 })); | 67 })); |
68 | 68 |
69 return task; | 69 return task; |
70 } | 70 } |
71 | 71 |
72 Future<List> get future => _completer.future; | 72 Future<List> get future => _completer.future; |
73 } | 73 } |
74 | 74 |
| 75 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] |
| 76 /// under the covers. |
| 77 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| 78 |
75 // TODO(rnystrom): Move into String? | 79 // TODO(rnystrom): Move into String? |
76 /// Pads [source] to [length] by adding spaces at the end. | 80 /// Pads [source] to [length] by adding spaces at the end. |
77 String padRight(String source, int length) { | 81 String padRight(String source, int length) { |
78 final result = new StringBuffer(); | 82 final result = new StringBuffer(); |
79 result.write(source); | 83 result.write(source); |
80 | 84 |
81 while (result.length < length) { | 85 while (result.length < length) { |
82 result.write(' '); | 86 result.write(' '); |
83 } | 87 } |
84 | 88 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 /// * Even if the former is guarded against by adding asynchronous operations, | 404 /// * Even if the former is guarded against by adding asynchronous operations, |
401 /// returning a value through the [Future] chain can still cause a stack | 405 /// returning a value through the [Future] chain can still cause a stack |
402 /// overflow. | 406 /// overflow. |
403 Future resetStack(fn()) { | 407 Future resetStack(fn()) { |
404 // Using a [Completer] breaks the [Future] chain for the return value and | 408 // Using a [Completer] breaks the [Future] chain for the return value and |
405 // avoids the third case described above. | 409 // avoids the third case described above. |
406 var completer = new Completer(); | 410 var completer = new Completer(); |
407 | 411 |
408 // Using [new Future] adds an asynchronous operation that works around the | 412 // Using [new Future] adds an asynchronous operation that works around the |
409 // first and second cases described above. | 413 // first and second cases described above. |
410 new Future(fn).then((val) { | 414 newFuture(fn).then((val) { |
411 runAsync(() => completer.complete(val)); | 415 runAsync(() => completer.complete(val)); |
412 }).catchError((err) { | 416 }).catchError((err) { |
413 runAsync(() => completer.completeError(err)); | 417 runAsync(() => completer.completeError(err)); |
414 }); | 418 }); |
415 return completer.future; | 419 return completer.future; |
416 } | 420 } |
417 | 421 |
418 /// The subset of strings that don't need quoting in YAML. This pattern does | 422 /// The subset of strings that don't need quoting in YAML. This pattern does |
419 /// not strictly follow the plain scalar grammar of YAML, which means some | 423 /// not strictly follow the plain scalar grammar of YAML, which means some |
420 /// strings may be unnecessarily quoted, but it's much simpler. | 424 /// strings may be unnecessarily quoted, but it's much simpler. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 error is DirectoryException || | 505 error is DirectoryException || |
502 error is FileException || | 506 error is FileException || |
503 error is HttpException || | 507 error is HttpException || |
504 error is HttpException || | 508 error is HttpException || |
505 error is LinkException || | 509 error is LinkException || |
506 error is OSError || | 510 error is OSError || |
507 error is ProcessException || | 511 error is ProcessException || |
508 error is SocketException || | 512 error is SocketException || |
509 error is WebSocketException; | 513 error is WebSocketException; |
510 } | 514 } |
OLD | NEW |