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

Side by Side Diff: sdk/lib/_internal/pub_generated/asset/dart/utils.dart

Issue 896623005: Use native async/await support in pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 5 years, 10 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 /// Functions go in this file as opposed to lib/src/utils.dart if they need to
6 /// be accessible to the transformer-loading isolate.
7 library pub.asset.utils;
8
9 import 'dart:async';
10
11 /// A regular expression to match the exception prefix that some exceptions'
12 /// [Object.toString] values contain.
13 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): ');
14
15 /// Get a string description of an exception.
16 ///
17 /// Many exceptions include the exception class name at the beginning of their
18 /// [toString], so we remove that if it exists.
19 String getErrorMessage(error) =>
20 error.toString().replaceFirst(_exceptionPrefix, '');
21
22 /// Returns a buffered stream that will emit the same values as the stream
23 /// returned by [future] once [future] completes.
24 ///
25 /// If [future] completes to an error, the return value will emit that error and
26 /// then close.
27 ///
28 /// If [broadcast] is true, a broadcast stream is returned. This assumes that
29 /// the stream returned by [future] will be a broadcast stream as well.
30 /// [broadcast] defaults to false.
31 Stream futureStream(Future<Stream> future, {bool broadcast: false}) {
32 var subscription;
33 var controller;
34
35 future = future.catchError((e, stackTrace) {
36 // Since [controller] is synchronous, it's likely that emitting an error
37 // will cause it to be cancelled before we call close.
38 if (controller != null) controller.addError(e, stackTrace);
39 if (controller != null) controller.close();
40 controller = null;
41 });
42
43 onListen() {
44 future.then((stream) {
45 if (controller == null) return;
46 subscription = stream.listen(
47 controller.add,
48 onError: controller.addError,
49 onDone: controller.close);
50 });
51 }
52
53 onCancel() {
54 if (subscription != null) subscription.cancel();
55 subscription = null;
56 controller = null;
57 }
58
59 if (broadcast) {
60 controller = new StreamController.broadcast(
61 sync: true, onListen: onListen, onCancel: onCancel);
62 } else {
63 controller = new StreamController(
64 sync: true, onListen: onListen, onCancel: onCancel);
65 }
66 return controller.stream;
67 }
68
69 /// Returns a [Stream] that will emit the same values as the stream returned by
70 /// [callback].
71 ///
72 /// [callback] will only be called when the returned [Stream] gets a subscriber.
73 Stream callbackStream(Stream callback()) {
74 var subscription;
75 var controller;
76 controller = new StreamController(onListen: () {
77 subscription = callback().listen(controller.add,
78 onError: controller.addError,
79 onDone: controller.close);
80 },
81 onCancel: () => subscription.cancel(),
82 onPause: () => subscription.pause(),
83 onResume: () => subscription.resume(),
84 sync: true);
85 return controller.stream;
86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698