OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return completer.future; | 111 return completer.future; |
112 } | 112 } |
113 | 113 |
114 /// Configures [future] so that its result (success or exception) is passed on | 114 /// Configures [future] so that its result (success or exception) is passed on |
115 /// to [completer]. | 115 /// to [completer]. |
116 void chainToCompleter(Future future, Completer completer) { | 116 void chainToCompleter(Future future, Completer completer) { |
117 future.then((value) => completer.complete(value), | 117 future.then((value) => completer.complete(value), |
118 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
119 } | 119 } |
120 | 120 |
| 121 // TODO(nweiz): remove this when issue 7964 is fixed. |
| 122 /// Returns a [Future] that will complete to the first element of [stream]. |
| 123 /// Unlike [Stream.first], this is safe to use with single-subscription streams. |
| 124 Future streamFirst(Stream stream) { |
| 125 var completer = new Completer(); |
| 126 var subscription; |
| 127 subscription = stream.listen((value) { |
| 128 subscription.cancel(); |
| 129 completer.complete(value); |
| 130 }, |
| 131 onError: (e) => completer.completeError(e.error, e.stackTrace), |
| 132 onDone: () => completer.completeError(new StateError("No elements")), |
| 133 unsubscribeOnError: true); |
| 134 return completer.future; |
| 135 } |
| 136 |
| 137 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
| 138 /// can be used to control the wrapped stream. |
| 139 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
| 140 var controller = stream.isSingleSubscription ? |
| 141 new StreamController() : |
| 142 new StreamController.multiSubscription(); |
| 143 var subscription = stream.listen(controller.add, |
| 144 onError: controller.signalError, |
| 145 onDone: controller.close); |
| 146 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); |
| 147 } |
| 148 |
| 149 // TODO(nweiz): remove this when issue 7787 is fixed. |
| 150 /// Creates two single-subscription [Stream]s that each emit all values and |
| 151 /// errors from [stream]. This is useful if [stream] is single-subscription but |
| 152 /// multiple subscribers are necessary. |
| 153 Pair<Stream, Stream> tee(Stream stream) { |
| 154 var controller1 = new StreamController(); |
| 155 var controller2 = new StreamController(); |
| 156 stream.listen((value) { |
| 157 controller1.add(value); |
| 158 controller2.add(value); |
| 159 }, onError: (error) { |
| 160 controller1.signalError(error); |
| 161 controller2.signalError(error); |
| 162 }, onDone: () { |
| 163 controller1.close(); |
| 164 controller2.close(); |
| 165 }); |
| 166 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
| 167 } |
| 168 |
| 169 /// A regular expression matching a line termination character or character |
| 170 /// sequence. |
| 171 final RegExp _lineRegexp = new RegExp(r"\r\n|\r|\n"); |
| 172 |
| 173 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. |
| 174 /// The lines don't include line termination characters. A single trailing |
| 175 /// newline is ignored. |
| 176 Stream<String> streamToLines(Stream<String> stream) { |
| 177 var buffer = new StringBuffer(); |
| 178 return stream.transform(new StreamTransformer.from( |
| 179 onData: (chunk, sink) { |
| 180 var lines = chunk.split(_lineRegexp); |
| 181 var leftover = lines.removeLast(); |
| 182 for (var line in lines) { |
| 183 if (!buffer.isEmpty) { |
| 184 buffer.add(line); |
| 185 line = buffer.toString(); |
| 186 buffer.clear(); |
| 187 } |
| 188 |
| 189 sink.add(line); |
| 190 } |
| 191 buffer.add(leftover); |
| 192 }, onDone: (sink) { |
| 193 if (!buffer.isEmpty) sink.add(buffer.toString()); |
| 194 sink.close(); |
| 195 })); |
| 196 } |
| 197 |
121 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the | 198 /// Like [Iterable.where], but allows [test] to return [Future]s and uses the |
122 /// results of those [Future]s as the test. | 199 /// results of those [Future]s as the test. |
123 Future<Iterable> futureWhere(Iterable iter, test(value)) { | 200 Future<Iterable> futureWhere(Iterable iter, test(value)) { |
124 return Future.wait(iter.mappedBy((e) { | 201 return Future.wait(iter.mappedBy((e) { |
125 var result = test(e); | 202 var result = test(e); |
126 if (result is! Future) result = new Future.immediate(result); | 203 if (result is! Future) result = new Future.immediate(result); |
127 return result.then((result) => new Pair(e, result)); | 204 return result.then((result) => new Pair(e, result)); |
128 })) | 205 })) |
129 .then((pairs) => pairs.where((pair) => pair.last)) | 206 .then((pairs) => pairs.where((pair) => pair.last)) |
130 .then((pairs) => pairs.mappedBy((pair) => pair.first)); | 207 .then((pairs) => pairs.mappedBy((pair) => pair.first)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 259 |
183 /// Add all key/value pairs from [source] to [destination], overwriting any | 260 /// Add all key/value pairs from [source] to [destination], overwriting any |
184 /// pre-existing values. | 261 /// pre-existing values. |
185 void mapAddAll(Map destination, Map source) => | 262 void mapAddAll(Map destination, Map source) => |
186 source.forEach((key, value) => destination[key] = value); | 263 source.forEach((key, value) => destination[key] = value); |
187 | 264 |
188 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 265 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
189 /// replacing `+` with ` `. | 266 /// replacing `+` with ` `. |
190 String urlDecode(String encoded) => | 267 String urlDecode(String encoded) => |
191 decodeUriComponent(encoded.replaceAll("+", " ")); | 268 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |