| 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 119 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(sync: true); | 140 var controller = |
| 141 var controllerStream = stream.isBroadcast ? | 141 stream.isBroadcast ? new StreamController.broadcast(sync: true) |
| 142 controller.stream.asBroadcastStream() : | 142 : new StreamController(sync: true); |
| 143 controller.stream; | 143 var controllerStream = 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 |
| (...skipping 48 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 |