OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 74 } |
75 | 75 |
76 // TODO(nweiz): remove this when issue 8731 is fixed. | 76 // TODO(nweiz): remove this when issue 8731 is fixed. |
77 /// Returns a [Stream] that will immediately emit [error] and then close. | 77 /// Returns a [Stream] that will immediately emit [error] and then close. |
78 Stream errorStream(error) => new Future.error(error).asStream(); | 78 Stream errorStream(error) => new Future.error(error).asStream(); |
79 | 79 |
80 /// Returns a buffered stream that will emit the same values as the stream | 80 /// Returns a buffered stream that will emit the same values as the stream |
81 /// returned by [future] once [future] completes. If [future] completes to an | 81 /// returned by [future] once [future] completes. If [future] completes to an |
82 /// error, the return value will emit that error and then close. | 82 /// error, the return value will emit that error and then close. |
83 Stream futureStream(Future<Stream> future) { | 83 Stream futureStream(Future<Stream> future) { |
84 var controller = new StreamController(); | 84 var controller = new StreamController(sync: true); |
85 future.then((stream) { | 85 future.then((stream) { |
86 stream.listen( | 86 stream.listen( |
87 controller.add, | 87 controller.add, |
88 onError: (error) => controller.addError(error), | 88 onError: (error) => controller.addError(error), |
89 onDone: controller.close); | 89 onDone: controller.close); |
90 }).catchError((e) { | 90 }).catchError((e) { |
91 controller.addError(e); | 91 controller.addError(e); |
92 controller.close(); | 92 controller.close(); |
93 }); | 93 }); |
94 return controller.stream; | 94 return controller.stream; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 130 } |
131 | 131 |
132 /// A function that can be called to cancel a [Stream] and send a done message. | 132 /// A function that can be called to cancel a [Stream] and send a done message. |
133 typedef void StreamCanceller(); | 133 typedef void StreamCanceller(); |
134 | 134 |
135 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. | 135 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. |
136 /// Returns a wrapped version of [stream] along with a function that will cancel | 136 /// Returns a wrapped version of [stream] along with a function that will cancel |
137 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a | 137 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a |
138 /// "done" message to the wrapped stream. | 138 /// "done" message to the wrapped stream. |
139 Pair<Stream, StreamCanceller> streamWithCanceller(Stream stream) { | 139 Pair<Stream, StreamCanceller> streamWithCanceller(Stream stream) { |
140 var controller = new StreamController(); | 140 var controller = new StreamController(sync: true); |
141 var controllerStream = stream.isBroadcast ? | 141 var controllerStream = stream.isBroadcast ? |
142 controller.stream.asBroadcastStream() : | 142 controller.stream.asBroadcastStream() : |
143 controller.stream; | 143 controller.stream; |
144 var subscription = stream.listen((value) { | 144 var subscription = stream.listen((value) { |
145 if (!controller.isClosed) controller.add(value); | 145 if (!controller.isClosed) controller.add(value); |
146 }, onError: (error) { | 146 }, onError: (error) { |
147 if (!controller.isClosed) controller.addError(error); | 147 if (!controller.isClosed) controller.addError(error); |
148 }, onDone: controller.close); | 148 }, onDone: controller.close); |
149 return new Pair<Stream, StreamCanceller>(controllerStream, controller.close); | 149 return new Pair<Stream, StreamCanceller>(controllerStream, controller.close); |
150 } | 150 } |
151 | 151 |
152 // TODO(nweiz): remove this when issue 7787 is fixed. | 152 // TODO(nweiz): remove this when issue 7787 is fixed. |
153 /// Creates two single-subscription [Stream]s that each emit all values and | 153 /// Creates two single-subscription [Stream]s that each emit all values and |
154 /// errors from [stream]. This is useful if [stream] is single-subscription but | 154 /// errors from [stream]. This is useful if [stream] is single-subscription but |
155 /// multiple subscribers are necessary. | 155 /// multiple subscribers are necessary. |
156 Pair<Stream, Stream> tee(Stream stream) { | 156 Pair<Stream, Stream> tee(Stream stream) { |
157 var controller1 = new StreamController(); | 157 var controller1 = new StreamController(sync: true); |
158 var controller2 = new StreamController(); | 158 var controller2 = new StreamController(sync: true); |
159 stream.listen((value) { | 159 stream.listen((value) { |
160 controller1.add(value); | 160 controller1.add(value); |
161 controller2.add(value); | 161 controller2.add(value); |
162 }, onError: (error) { | 162 }, onError: (error) { |
163 controller1.addError(error); | 163 controller1.addError(error); |
164 controller2.addError(error); | 164 controller2.addError(error); |
165 }, onDone: () { | 165 }, onDone: () { |
166 controller1.close(); | 166 controller1.close(); |
167 controller2.close(); | 167 controller2.close(); |
168 }); | 168 }); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 202 } |
203 | 203 |
204 /// Returns a string representation of [trace] that has the core and test frames | 204 /// Returns a string representation of [trace] that has the core and test frames |
205 /// folded together. | 205 /// folded together. |
206 String terseTraceString(StackTrace trace) { | 206 String terseTraceString(StackTrace trace) { |
207 return new Trace.from(trace).terse.foldFrames((frame) { | 207 return new Trace.from(trace).terse.foldFrames((frame) { |
208 return frame.package == 'scheduled_test' || frame.package == 'unittest' || | 208 return frame.package == 'scheduled_test' || frame.package == 'unittest' || |
209 frame.isCore; | 209 frame.isCore; |
210 }).toString().trim(); | 210 }).toString().trim(); |
211 } | 211 } |
OLD | NEW |