Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: pkg/scheduled_test/lib/src/utils.dart

Issue 14753009: Make StreamSubscription be the active part of a stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/scheduled_test/lib/src/mock_clock.dart ('k') | runtime/bin/socket_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // TODO(nweiz): remove this when issue 7964 is fixed. 97 /// Returns the first element of a [StreamIterator].
98 /// Returns a [Future] that will complete to the first element of [stream]. 98 ///
99 /// Unlike [Stream.first], this is safe to use with single-subscription streams. 99 /// If the [StreamIterator] has no elements, the result is a state error.
100 Future streamFirst(Stream stream) { 100 Future<String> streamIteratorFirst(StreamIterator<String> streamIterator) {
101 var stackTrace; 101 StackTrace stackTrace = new Trace.current();
102 try { 102 return streamIterator.moveNext().then((hasNext) {
103 throw ''; 103 if (hasNext) {
104 } catch (_, thrownStackTrace) { 104 return streamIterator.current;
105 stackTrace = thrownStackTrace; 105 } else {
106 return new Future.error(new StateError("No elements"), stackTrace);
107 }
108 });
109 }
110
111 /// Collects all remaining lines from a [StreamIterator] of lines.
112 ///
113 /// Returns the concatenation of the collected lines joined by newlines.
114 Future<String> concatRest(StreamIterator<String> streamIterator) {
115 var completer = new Completer<String>();
116 var buffer = new StringBuffer();
117 void collectAll() {
118 streamIterator.moveNext().then((hasNext) {
119 if (hasNext) {
120 if (!buffer.isEmpty) buffer.write('\n');
121 buffer.write(streamIterator.current);
122 collectAll();
123 } else {
124 completer.complete(buffer.toString());
125 }
126 }, onError: completer.completeError);
106 } 127 }
107 128 collectAll();
108 var completer = new Completer();
109 var subscription;
110 subscription = stream.listen((value) {
111 subscription.cancel();
112 completer.complete(value);
113 }, onError: (e) {
114 completer.completeError(e);
115 }, onDone: () {
116 completer.completeError(new StateError("No elements"), stackTrace);
117 }, cancelOnError: true);
118 return completer.future; 129 return completer.future;
119 } 130 }
120 131
121 /// 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.
122 typedef void StreamCanceller(); 133 typedef void StreamCanceller();
123 134
124 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. 135 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed.
125 /// 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
126 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a 137 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a
127 /// "done" message to the wrapped stream. 138 /// "done" message to the wrapped stream.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 202 }
192 203
193 /// 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
194 /// folded together. 205 /// folded together.
195 String terseTraceString(StackTrace trace) { 206 String terseTraceString(StackTrace trace) {
196 return new Trace.from(trace).terse.foldFrames((frame) { 207 return new Trace.from(trace).terse.foldFrames((frame) {
197 return frame.package == 'scheduled_test' || frame.package == 'unittest' || 208 return frame.package == 'scheduled_test' || frame.package == 'unittest' ||
198 frame.isCore; 209 frame.isCore;
199 }).toString().trim(); 210 }).toString().trim();
200 } 211 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/mock_clock.dart ('k') | runtime/bin/socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698