Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Unified Diff: sdk/lib/_internal/pub/lib/src/utils.dart

Issue 100393010: Use a pool to restrict access to file descriptors in pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub/lib/src/utils.dart
diff --git a/sdk/lib/_internal/pub/lib/src/utils.dart b/sdk/lib/_internal/pub/lib/src/utils.dart
index 711b224cca4ee90d18d6d8bc4c14800a143a1c3e..df9f9d121b18fd38ae7336c4d9a8ac34c415a5c8 100644
--- a/sdk/lib/_internal/pub/lib/src/utils.dart
+++ b/sdk/lib/_internal/pub/lib/src/utils.dart
@@ -77,10 +77,68 @@ class FutureGroup<T> {
Future<List> get future => _completer.future;
}
+/// Returns a buffered stream that will emit the same values as the stream
+/// returned by [future] once [future] completes.
+///
+/// If [future] completes to an error, the return value will emit that error and
+/// then close.
+///
+/// If [broadcast] is true, a broadcast stream is returned. This assumes that
+/// the stream returned by [future] will be a broadcast stream as well.
+/// [broadcast] defaults to false.
+Stream futureStream(Future<Stream> future, {bool broadcast: false}) {
+ var subscription;
+ var controller;
+
+ future = future.catchError((e, stackTrace) {
+ // Since [controller] is synchronous, it's likely that emitting an error
+ // will cause it to be cancelled before we call close.
+ if (controller != null) controller.addError(e, stackTrace);
+ if (controller != null) controller.close();
+ controller = null;
+ });
+
+ onListen() {
+ future.then((stream) {
+ if (controller == null) return;
+ subscription = stream.listen(
+ controller.add,
+ onError: controller.addError,
+ onDone: controller.close);
+ });
+ }
+
+ onCancel() {
+ if (subscription != null) subscription.cancel();
+ subscription = null;
+ controller = null;
+ }
+
+ if (broadcast) {
+ controller = new StreamController.broadcast(
+ sync: true, onListen: onListen, onCancel: onCancel);
+ } else {
+ controller = new StreamController(
+ sync: true, onListen: onListen, onCancel: onCancel);
+ }
+ return controller.stream;
+}
+
/// Like [new Future], but avoids around issue 11911 by using [new Future.value]
/// under the covers.
Future newFuture(callback()) => new Future.value().then((_) => callback());
+/// Returns a [StreamTransformer] that will call [onDone] when the stream
+/// completes.
+///
+/// The stream will be passed through unchanged.
+StreamTransformer onDoneTransformer(void onDone()) {
+ return new StreamTransformer.fromHandlers(handleDone: (sink) {
+ onDone();
+ sink.close();
+ });
+}
+
// TODO(rnystrom): Move into String?
/// Pads [source] to [length] by adding spaces at the end.
String padRight(String source, int length) {
« sdk/lib/_internal/pub/lib/src/io.dart ('K') | « sdk/lib/_internal/pub/lib/src/pool.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698