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; | |
178 var completer = new Completer(); | 176 var completer = new Completer(); |
179 var subscription; | 177 var subscription; |
180 subscription = stream.listen((value) { | 178 subscription = stream.listen((value) { |
181 if (!cancelled) { | 179 subscription.cancel(); |
182 cancelled = true; | 180 completer.complete(value); |
183 subscription.cancel(); | |
184 completer.complete(value); | |
185 } | |
186 }, onError: (e) { | 181 }, onError: (e) { |
187 if (!cancelled) { | 182 completer.completeError(e.error, e.stackTrace); |
188 completer.completeError(e.error, e.stackTrace); | |
189 } | |
190 }, onDone: () { | 183 }, onDone: () { |
191 if (!cancelled) { | 184 completer.completeError(new StateError("No elements")); |
192 completer.completeError(new StateError("No elements")); | |
193 } | |
194 }, unsubscribeOnError: true); | 185 }, unsubscribeOnError: true); |
195 return completer.future; | 186 return completer.future; |
196 } | 187 } |
197 | 188 |
198 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 189 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
199 /// can be used to control the wrapped stream. | 190 /// can be used to control the wrapped stream. |
200 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 191 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
201 var controller = stream.isBroadcast ? | 192 var controller = stream.isBroadcast ? |
202 new StreamController.broadcast() : | 193 new StreamController.broadcast() : |
203 new StreamController(); | 194 new StreamController(); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 312 |
322 /// Add all key/value pairs from [source] to [destination], overwriting any | 313 /// Add all key/value pairs from [source] to [destination], overwriting any |
323 /// pre-existing values. | 314 /// pre-existing values. |
324 void mapAddAll(Map destination, Map source) => | 315 void mapAddAll(Map destination, Map source) => |
325 source.forEach((key, value) => destination[key] = value); | 316 source.forEach((key, value) => destination[key] = value); |
326 | 317 |
327 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 318 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
328 /// replacing `+` with ` `. | 319 /// replacing `+` with ` `. |
329 String urlDecode(String encoded) => | 320 String urlDecode(String encoded) => |
330 decodeUriComponent(encoded.replaceAll("+", " ")); | 321 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |