| 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 11 matching lines...) Expand all Loading... |
| 22 return other.first == first && other.last == last; | 22 return other.first == first && other.last == last; |
| 23 } | 23 } |
| 24 | 24 |
| 25 int get hashCode => first.hashCode ^ last.hashCode; | 25 int get hashCode => first.hashCode ^ last.hashCode; |
| 26 } | 26 } |
| 27 | 27 |
| 28 /// Configures [future] so that its result (success or exception) is passed on | 28 /// Configures [future] so that its result (success or exception) is passed on |
| 29 /// to [completer]. | 29 /// to [completer]. |
| 30 void chainToCompleter(Future future, Completer completer) { | 30 void chainToCompleter(Future future, Completer completer) { |
| 31 future.then((value) => completer.complete(value), | 31 future.then((value) => completer.complete(value), |
| 32 onError: (e) => completer.completeError(e)); | 32 onError: completer.completeError); |
| 33 } | 33 } |
| 34 | 34 |
| 35 /// Prepends each line in [text] with [prefix]. If [firstPrefix] is passed, the | 35 /// Prepends each line in [text] with [prefix]. If [firstPrefix] is passed, the |
| 36 /// first line is prefixed with that instead. | 36 /// first line is prefixed with that instead. |
| 37 String prefixLines(String text, {String prefix: '| ', String firstPrefix}) { | 37 String prefixLines(String text, {String prefix: '| ', String firstPrefix}) { |
| 38 var lines = text.split('\n'); | 38 var lines = text.split('\n'); |
| 39 if (firstPrefix == null) { | 39 if (firstPrefix == null) { |
| 40 return lines.map((line) => '$prefix$line').join('\n'); | 40 return lines.map((line) => '$prefix$line').join('\n'); |
| 41 } | 41 } |
| 42 | 42 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(sync: true); | 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: controller.addError, |
| 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; |
| 95 } | 95 } |
| 96 | 96 |
| 97 /// Returns the first element of a [StreamIterator]. | 97 /// Returns the first element of a [StreamIterator]. |
| 98 /// | 98 /// |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 = | 140 var controller = |
| 141 stream.isBroadcast ? new StreamController.broadcast(sync: true) | 141 stream.isBroadcast ? new StreamController.broadcast(sync: true) |
| 142 : new StreamController(sync: true); | 142 : new StreamController(sync: true); |
| 143 var controllerStream = 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, [StackTrace stackTrace]) { |
| 147 if (!controller.isClosed) controller.addError(error); | 147 if (!controller.isClosed) controller.addError(error, stackTrace); |
| 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(sync: true); | 157 var controller1 = new StreamController(sync: true); |
| 158 var controller2 = new StreamController(sync: true); | 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, [StackTrace stackTrace]) { |
| 163 controller1.addError(error); | 163 controller1.addError(error, stackTrace); |
| 164 controller2.addError(error); | 164 controller2.addError(error, stackTrace); |
| 165 }, onDone: () { | 165 }, onDone: () { |
| 166 controller1.close(); | 166 controller1.close(); |
| 167 controller2.close(); | 167 controller2.close(); |
| 168 }); | 168 }); |
| 169 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | 169 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
| 170 } | 170 } |
| 171 | 171 |
| 172 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar | 172 /// Takes a simple data structure (composed of [Map]s, [Iterable]s, scalar |
| 173 /// objects, and [Future]s) and recursively resolves all the [Future]s contained | 173 /// objects, and [Future]s) and recursively resolves all the [Future]s contained |
| 174 /// within. Completes with the fully resolved structure. | 174 /// within. Completes with the fully resolved structure. |
| (...skipping 27 matching lines...) Expand all 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 |