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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return completer.future; | 195 return completer.future; |
196 } | 196 } |
197 | 197 |
198 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 198 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
199 /// can be used to control the wrapped stream. | 199 /// can be used to control the wrapped stream. |
200 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 200 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
201 var controller = stream.isBroadcast ? | 201 var controller = stream.isBroadcast ? |
202 new StreamController.broadcast() : | 202 new StreamController.broadcast() : |
203 new StreamController(); | 203 new StreamController(); |
204 var subscription = stream.listen(controller.add, | 204 var subscription = stream.listen(controller.add, |
205 onError: controller.signalError, | 205 onError: controller.addError, |
206 onDone: controller.close); | 206 onDone: controller.close); |
207 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); | 207 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); |
208 } | 208 } |
209 | 209 |
210 // TODO(nweiz): remove this when issue 7787 is fixed. | 210 // TODO(nweiz): remove this when issue 7787 is fixed. |
211 /// Creates two single-subscription [Stream]s that each emit all values and | 211 /// Creates two single-subscription [Stream]s that each emit all values and |
212 /// errors from [stream]. This is useful if [stream] is single-subscription but | 212 /// errors from [stream]. This is useful if [stream] is single-subscription but |
213 /// multiple subscribers are necessary. | 213 /// multiple subscribers are necessary. |
214 Pair<Stream, Stream> tee(Stream stream) { | 214 Pair<Stream, Stream> tee(Stream stream) { |
215 var controller1 = new StreamController(); | 215 var controller1 = new StreamController(); |
216 var controller2 = new StreamController(); | 216 var controller2 = new StreamController(); |
217 stream.listen((value) { | 217 stream.listen((value) { |
218 controller1.add(value); | 218 controller1.add(value); |
219 controller2.add(value); | 219 controller2.add(value); |
220 }, onError: (error) { | 220 }, onError: (error) { |
221 controller1.signalError(error); | 221 controller1.addError(error); |
222 controller2.signalError(error); | 222 controller2.addError(error); |
223 }, onDone: () { | 223 }, onDone: () { |
224 controller1.close(); | 224 controller1.close(); |
225 controller2.close(); | 225 controller2.close(); |
226 }); | 226 }); |
227 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | 227 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
228 } | 228 } |
229 | 229 |
230 /// A regular expression matching a line termination character or character | 230 /// A regular expression matching a line termination character or character |
231 /// sequence. | 231 /// sequence. |
232 final RegExp _lineRegexp = new RegExp(r"\r\n|\r|\n"); | 232 final RegExp _lineRegexp = new RegExp(r"\r\n|\r|\n"); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 321 |
322 /// Add all key/value pairs from [source] to [destination], overwriting any | 322 /// Add all key/value pairs from [source] to [destination], overwriting any |
323 /// pre-existing values. | 323 /// pre-existing values. |
324 void mapAddAll(Map destination, Map source) => | 324 void mapAddAll(Map destination, Map source) => |
325 source.forEach((key, value) => destination[key] = value); | 325 source.forEach((key, value) => destination[key] = value); |
326 | 326 |
327 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 327 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
328 /// replacing `+` with ` `. | 328 /// replacing `+` with ` `. |
329 String urlDecode(String encoded) => | 329 String urlDecode(String encoded) => |
330 decodeUriComponent(encoded.replaceAll("+", " ")); | 330 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |