| 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 /// to [completer]. | 166 /// to [completer]. |
| 167 void chainToCompleter(Future future, Completer completer) { | 167 void chainToCompleter(Future future, Completer completer) { |
| 168 future.then((value) => completer.complete(value), | 168 future.then((value) => completer.complete(value), |
| 169 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 169 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
| 170 } | 170 } |
| 171 | 171 |
| 172 // TODO(nweiz): remove this when issue 7964 is fixed. | 172 // TODO(nweiz): remove this when issue 7964 is fixed. |
| 173 /// Returns a [Future] that will complete to the first element of [stream]. | 173 /// Returns a [Future] that will complete to the first element of [stream]. |
| 174 /// Unlike [Stream.first], this is safe to use with single-subscription streams. | 174 /// Unlike [Stream.first], this is safe to use with single-subscription streams. |
| 175 Future streamFirst(Stream stream) { | 175 Future streamFirst(Stream stream) { |
| 176 // TODO(nweiz): remove this when issue 8512 is fixed. |
| 177 var cancelled = false; |
| 176 var completer = new Completer(); | 178 var completer = new Completer(); |
| 177 var subscription; | 179 var subscription; |
| 178 subscription = stream.listen((value) { | 180 subscription = stream.listen((value) { |
| 179 subscription.cancel(); | 181 if (!cancelled) { |
| 180 completer.complete(value); | 182 cancelled = true; |
| 181 }, | 183 subscription.cancel(); |
| 182 onError: (e) => completer.completeError(e.error, e.stackTrace), | 184 completer.complete(value); |
| 183 onDone: () => completer.completeError(new StateError("No elements")), | 185 } |
| 184 unsubscribeOnError: true); | 186 }, onError: (e) { |
| 187 if (!cancelled) { |
| 188 completer.completeError(e.error, e.stackTrace); |
| 189 } |
| 190 }, onDone: () { |
| 191 if (!cancelled) { |
| 192 completer.completeError(new StateError("No elements")); |
| 193 } |
| 194 }, unsubscribeOnError: true); |
| 185 return completer.future; | 195 return completer.future; |
| 186 } | 196 } |
| 187 | 197 |
| 188 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 198 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
| 189 /// can be used to control the wrapped stream. | 199 /// can be used to control the wrapped stream. |
| 190 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 200 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
| 191 var controller = stream.isBroadcast ? | 201 var controller = stream.isBroadcast ? |
| 192 new StreamController.broadcast() : | 202 new StreamController.broadcast() : |
| 193 new StreamController(); | 203 new StreamController(); |
| 194 var subscription = stream.listen(controller.add, | 204 var subscription = stream.listen(controller.add, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 | 334 |
| 325 /// Add all key/value pairs from [source] to [destination], overwriting any | 335 /// Add all key/value pairs from [source] to [destination], overwriting any |
| 326 /// pre-existing values. | 336 /// pre-existing values. |
| 327 void mapAddAll(Map destination, Map source) => | 337 void mapAddAll(Map destination, Map source) => |
| 328 source.forEach((key, value) => destination[key] = value); | 338 source.forEach((key, value) => destination[key] = value); |
| 329 | 339 |
| 330 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 340 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
| 331 /// replacing `+` with ` `. | 341 /// replacing `+` with ` `. |
| 332 String urlDecode(String encoded) => | 342 String urlDecode(String encoded) => |
| 333 decodeUriComponent(encoded.replaceAll("+", " ")); | 343 decodeUriComponent(encoded.replaceAll("+", " ")); |
| OLD | NEW |