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:convert"; | 9 import "dart:convert"; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 onError: controller.addError, | 591 onError: controller.addError, |
592 onDone: () { | 592 onDone: () { |
593 doneCount++; | 593 doneCount++; |
594 if (doneCount == 2) controller.close(); | 594 if (doneCount == 2) controller.close(); |
595 }); | 595 }); |
596 } | 596 } |
597 | 597 |
598 return controller.stream; | 598 return controller.stream; |
599 } | 599 } |
600 | 600 |
| 601 /// Returns a [Stream] that will emit the same values as the stream returned by |
| 602 /// [callback]. |
| 603 /// |
| 604 /// [callback] will only be called when the returned [Stream] gets a subscriber. |
| 605 Stream callbackStream(Stream callback()) { |
| 606 var subscription; |
| 607 var controller; |
| 608 controller = new StreamController(onListen: () { |
| 609 subscription = callback().listen(controller.add, |
| 610 onError: controller.addError, |
| 611 onDone: controller.close); |
| 612 }, |
| 613 onCancel: () => subscription.cancel(), |
| 614 onPause: () => subscription.pause(), |
| 615 onResume: () => subscription.resume(), |
| 616 sync: true); |
| 617 return controller.stream; |
| 618 } |
| 619 |
601 /// A regular expression matching a trailing CR character. | 620 /// A regular expression matching a trailing CR character. |
602 final _trailingCR = new RegExp(r"\r$"); | 621 final _trailingCR = new RegExp(r"\r$"); |
603 | 622 |
604 // TODO(nweiz): Use `text.split(new RegExp("\r\n?|\n\r?"))` when issue 9360 is | 623 // TODO(nweiz): Use `text.split(new RegExp("\r\n?|\n\r?"))` when issue 9360 is |
605 // fixed. | 624 // fixed. |
606 /// Splits [text] on its line breaks in a Windows-line-break-friendly way. | 625 /// Splits [text] on its line breaks in a Windows-line-break-friendly way. |
607 List<String> splitLines(String text) => | 626 List<String> splitLines(String text) => |
608 text.split("\n").map((line) => line.replaceFirst(_trailingCR, "")).toList(); | 627 text.split("\n").map((line) => line.replaceFirst(_trailingCR, "")).toList(); |
609 | 628 |
610 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. | 629 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
915 } else { | 934 } else { |
916 throw new ApplicationException(message); | 935 throw new ApplicationException(message); |
917 } | 936 } |
918 } | 937 } |
919 | 938 |
920 /// Throw a [DataException] with [message] to indicate that the command has | 939 /// Throw a [DataException] with [message] to indicate that the command has |
921 /// failed because of invalid input data. | 940 /// failed because of invalid input data. |
922 /// | 941 /// |
923 /// This will report the error and cause pub to exit with [exit_codes.DATA]. | 942 /// This will report the error and cause pub to exit with [exit_codes.DATA]. |
924 void dataError(String message) => throw new DataException(message); | 943 void dataError(String message) => throw new DataException(message); |
OLD | NEW |