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:collection"; | 9 import "dart:collection"; |
10 import "dart:convert"; | 10 import "dart:convert"; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 futures.add(task.then((value) { | 57 futures.add(task.then((value) { |
58 if (completed) return; | 58 if (completed) return; |
59 | 59 |
60 _pending--; | 60 _pending--; |
61 _values.add(value); | 61 _values.add(value); |
62 | 62 |
63 if (_pending <= 0) { | 63 if (_pending <= 0) { |
64 completed = true; | 64 completed = true; |
65 _completer.complete(_values); | 65 _completer.complete(_values); |
66 } | 66 } |
67 }).catchError((e) { | 67 }).catchError((e, stackTrace) { |
68 if (completed) return; | 68 if (completed) return; |
69 | 69 |
70 completed = true; | 70 completed = true; |
71 _completer.completeError(e); | 71 _completer.completeError(e, stackTrace); |
72 })); | 72 })); |
73 | 73 |
74 return task; | 74 return task; |
75 } | 75 } |
76 | 76 |
77 Future<List> get future => _completer.future; | 77 Future<List> get future => _completer.future; |
78 } | 78 } |
79 | 79 |
80 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] | 80 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] |
81 /// under the covers. | 81 /// under the covers. |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 /// Returns the hex-encoded sha1 hash of [source]. | 285 /// Returns the hex-encoded sha1 hash of [source]. |
286 String sha1(String source) { | 286 String sha1(String source) { |
287 var sha = new SHA1(); | 287 var sha = new SHA1(); |
288 sha.add(source.codeUnits); | 288 sha.add(source.codeUnits); |
289 return CryptoUtils.bytesToHex(sha.close()); | 289 return CryptoUtils.bytesToHex(sha.close()); |
290 } | 290 } |
291 | 291 |
292 /// Configures [future] so that its result (success or exception) is passed on | 292 /// Configures [future] so that its result (success or exception) is passed on |
293 /// to [completer]. | 293 /// to [completer]. |
294 void chainToCompleter(Future future, Completer completer) { | 294 void chainToCompleter(Future future, Completer completer) { |
295 future.then((value) => completer.complete(value), | 295 future.then(completer.complete, onError: completer.completeError); |
296 onError: (e) => completer.completeError(e)); | |
297 } | 296 } |
298 | 297 |
299 /// Ensures that [stream] can emit at least one value successfully (or close | 298 /// Ensures that [stream] can emit at least one value successfully (or close |
300 /// without any values). | 299 /// without any values). |
301 /// | 300 /// |
302 /// For example, reading asynchronously from a non-existent file will return a | 301 /// For example, reading asynchronously from a non-existent file will return a |
303 /// stream that fails on the first chunk. In order to handle that more | 302 /// stream that fails on the first chunk. In order to handle that more |
304 /// gracefully, you may want to check that the stream looks like it's working | 303 /// gracefully, you may want to check that the stream looks like it's working |
305 /// before you pipe the stream to something else. | 304 /// before you pipe the stream to something else. |
306 /// | 305 /// |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 /// overflow. | 633 /// overflow. |
635 Future resetStack(fn()) { | 634 Future resetStack(fn()) { |
636 // Using a [Completer] breaks the [Future] chain for the return value and | 635 // Using a [Completer] breaks the [Future] chain for the return value and |
637 // avoids the third case described above. | 636 // avoids the third case described above. |
638 var completer = new Completer(); | 637 var completer = new Completer(); |
639 | 638 |
640 // Using [new Future] adds an asynchronous operation that works around the | 639 // Using [new Future] adds an asynchronous operation that works around the |
641 // first and second cases described above. | 640 // first and second cases described above. |
642 newFuture(fn).then((val) { | 641 newFuture(fn).then((val) { |
643 scheduleMicrotask(() => completer.complete(val)); | 642 scheduleMicrotask(() => completer.complete(val)); |
644 }).catchError((err) { | 643 }).catchError((err, stackTrace) { |
645 scheduleMicrotask(() => completer.completeError(err)); | 644 scheduleMicrotask(() => completer.completeError(err, stackTrace)); |
646 }); | 645 }); |
647 return completer.future; | 646 return completer.future; |
648 } | 647 } |
649 | 648 |
650 /// The subset of strings that don't need quoting in YAML. This pattern does | 649 /// The subset of strings that don't need quoting in YAML. This pattern does |
651 /// not strictly follow the plain scalar grammar of YAML, which means some | 650 /// not strictly follow the plain scalar grammar of YAML, which means some |
652 /// strings may be unnecessarily quoted, but it's much simpler. | 651 /// strings may be unnecessarily quoted, but it's much simpler. |
653 final _unquotableYamlString = new RegExp(r"^[a-zA-Z_-][a-zA-Z_0-9-]*$"); | 652 final _unquotableYamlString = new RegExp(r"^[a-zA-Z_-][a-zA-Z_0-9-]*$"); |
654 | 653 |
655 /// Converts [data], which is a parsed YAML object, to a pretty-printed string, | 654 /// Converts [data], which is a parsed YAML object, to a pretty-printed string, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 error is AnalyzerErrorGroup || | 776 error is AnalyzerErrorGroup || |
778 error is IsolateSpawnException || | 777 error is IsolateSpawnException || |
779 error is FileSystemException || | 778 error is FileSystemException || |
780 error is HttpException || | 779 error is HttpException || |
781 error is HttpException || | 780 error is HttpException || |
782 error is OSError || | 781 error is OSError || |
783 error is ProcessException || | 782 error is ProcessException || |
784 error is SocketException || | 783 error is SocketException || |
785 error is WebSocketException; | 784 error is WebSocketException; |
786 } | 785 } |
OLD | NEW |