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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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; |
95 } | 95 } |
96 | 96 |
97 /// Returns the first element of a [StreamIterator]. | 97 // TODO(nweiz): remove this when issue 7964 is fixed. |
98 /// | 98 /// Returns a [Future] that will complete to the first element of [stream]. |
99 /// If the [StreamIterator] has no elements, the result is a state error. | 99 /// Unlike [Stream.first], this is safe to use with single-subscription streams. |
100 Future<String> streamIteratorFirst(StreamIterator<String> streamIterator) { | 100 Future streamFirst(Stream stream) { |
101 StackTrace stackTrace = new Trace.current(); | 101 var stackTrace; |
102 return streamIterator.moveNext().then((hasNext) { | 102 try { |
103 if (hasNext) { | 103 throw ''; |
104 return streamIterator.current; | 104 } catch (_, thrownStackTrace) { |
105 } else { | 105 stackTrace = thrownStackTrace; |
106 return new Future.error(new StateError("No elements"), stackTrace); | 106 } |
107 } | |
108 }); | |
109 } | |
110 | 107 |
111 /// Collects all remaining lines from a [StreamIterator] of lines. | 108 var completer = new Completer(); |
112 /// | 109 var subscription; |
113 /// Returns the concatenation of the collected lines joined by newlines. | 110 subscription = stream.listen((value) { |
114 Future<String> concatRest(StreamIterator<String> streamIterator) { | 111 subscription.cancel(); |
115 var completer = new Completer<String>(); | 112 completer.complete(value); |
116 var buffer = new StringBuffer(); | 113 }, onError: (e) { |
117 void collectAll() { | 114 completer.completeError(e); |
118 streamIterator.moveNext().then((hasNext) { | 115 }, onDone: () { |
119 if (hasNext) { | 116 completer.completeError(new StateError("No elements"), stackTrace); |
120 if (!buffer.isEmpty) buffer.write('\n'); | 117 }, cancelOnError: true); |
121 buffer.write(streamIterator.current); | |
122 collectAll(); | |
123 } else { | |
124 completer.complete(buffer.toString()); | |
125 } | |
126 }, onError: completer.completeError); | |
127 } | |
128 collectAll(); | |
129 return completer.future; | 118 return completer.future; |
130 } | 119 } |
131 | 120 |
132 /// A function that can be called to cancel a [Stream] and send a done message. | 121 /// A function that can be called to cancel a [Stream] and send a done message. |
133 typedef void StreamCanceller(); | 122 typedef void StreamCanceller(); |
134 | 123 |
135 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. | 124 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. |
136 /// Returns a wrapped version of [stream] along with a function that will cancel | 125 /// Returns a wrapped version of [stream] along with a function that will cancel |
137 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a | 126 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a |
138 /// "done" message to the wrapped stream. | 127 /// "done" message to the wrapped stream. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 191 } |
203 | 192 |
204 /// Returns a string representation of [trace] that has the core and test frames | 193 /// Returns a string representation of [trace] that has the core and test frames |
205 /// folded together. | 194 /// folded together. |
206 String terseTraceString(StackTrace trace) { | 195 String terseTraceString(StackTrace trace) { |
207 return new Trace.from(trace).terse.foldFrames((frame) { | 196 return new Trace.from(trace).terse.foldFrames((frame) { |
208 return frame.package == 'scheduled_test' || frame.package == 'unittest' || | 197 return frame.package == 'scheduled_test' || frame.package == 'unittest' || |
209 frame.isCore; | 198 frame.isCore; |
210 }).toString().trim(); | 199 }).toString().trim(); |
211 } | 200 } |
OLD | NEW |