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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 115 } |
116 | 116 |
117 /// A function that can be called to cancel a [Stream] and send a done message. | 117 /// A function that can be called to cancel a [Stream] and send a done message. |
118 typedef void StreamCanceller(); | 118 typedef void StreamCanceller(); |
119 | 119 |
120 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. | 120 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. |
121 /// Returns a wrapped version of [stream] along with a function that will cancel | 121 /// Returns a wrapped version of [stream] along with a function that will cancel |
122 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a | 122 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a |
123 /// "done" message to the wrapped stream. | 123 /// "done" message to the wrapped stream. |
124 Pair<Stream, StreamCanceller> streamWithCanceller(Stream stream) { | 124 Pair<Stream, StreamCanceller> streamWithCanceller(Stream stream) { |
125 var controller = stream.isBroadcast ? | 125 var controller = new StreamController(); |
126 new StreamController.broadcast() : | 126 var controllerStream = stream.isBroadcast ? |
127 new StreamController(); | 127 controller.stream.asBroadcastStream() : |
| 128 controller.stream; |
128 var subscription = stream.listen((value) { | 129 var subscription = stream.listen((value) { |
129 if (!controller.isClosed) controller.add(value); | 130 if (!controller.isClosed) controller.add(value); |
130 }, onError: (error) { | 131 }, onError: (error) { |
131 if (!controller.isClosed) controller.signalError(error); | 132 if (!controller.isClosed) controller.signalError(error); |
132 }, onDone: controller.close); | 133 }, onDone: controller.close); |
133 return new Pair<Stream, StreamCanceller>(controller.stream, controller.close); | 134 return new Pair<Stream, StreamCanceller>(controllerStream, controller.close); |
134 } | 135 } |
135 | 136 |
136 // TODO(nweiz): remove this when issue 7787 is fixed. | 137 // TODO(nweiz): remove this when issue 7787 is fixed. |
137 /// Creates two single-subscription [Stream]s that each emit all values and | 138 /// Creates two single-subscription [Stream]s that each emit all values and |
138 /// errors from [stream]. This is useful if [stream] is single-subscription but | 139 /// errors from [stream]. This is useful if [stream] is single-subscription but |
139 /// multiple subscribers are necessary. | 140 /// multiple subscribers are necessary. |
140 Pair<Stream, Stream> tee(Stream stream) { | 141 Pair<Stream, Stream> tee(Stream stream) { |
141 var controller1 = new StreamController(); | 142 var controller1 = new StreamController(); |
142 var controller2 = new StreamController(); | 143 var controller2 = new StreamController(); |
143 stream.listen((value) { | 144 stream.listen((value) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } | 187 } |
187 | 188 |
188 /// Returns a string representation of [trace] that has the core and test frames | 189 /// Returns a string representation of [trace] that has the core and test frames |
189 /// folded together. | 190 /// folded together. |
190 String terseTraceString(StackTrace trace) { | 191 String terseTraceString(StackTrace trace) { |
191 return new Trace.from(trace).terse.foldFrames((frame) { | 192 return new Trace.from(trace).terse.foldFrames((frame) { |
192 return frame.package == 'scheduled_test' || frame.package == 'unittest' || | 193 return frame.package == 'scheduled_test' || frame.package == 'unittest' || |
193 frame.isCore; | 194 frame.isCore; |
194 }).toString().trim(); | 195 }).toString().trim(); |
195 } | 196 } |
OLD | NEW |