| 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'; |
| 11 import 'dart:uri'; | 11 import 'dart:uri'; |
| 12 | 12 |
| 13 /// A pair of values. | 13 /// A pair of values. |
| 14 class Pair<E, F> { | 14 class Pair<E, F> { |
| 15 E first; | 15 E first; |
| 16 F last; | 16 F last; |
| 17 | 17 |
| 18 Pair(this.first, this.last); | 18 Pair(this.first, this.last); |
| 19 | 19 |
| 20 String toString() => '($first, $last)'; | 20 String toString() => '($first, $last)'; |
| 21 | 21 |
| 22 bool operator==(other) { | 22 bool operator==(other) { |
| 23 if (other is! Pair) return false; | 23 if (other is! Pair) return false; |
| 24 return other.first == first && other.last == last; | 24 return other.first == first && other.last == last; |
| 25 } | 25 } |
| 26 | 26 |
| 27 int get hashCode => first.hashCode ^ last.hashCode; | 27 int get hashCode => first.hashCode ^ last.hashCode; |
| 28 } | 28 } |
| 29 | 29 |
| 30 /// A completer that waits until all added [Future]s complete. |
| 31 // TODO(rnystrom): Copied from web_components. Remove from here when it gets |
| 32 // added to dart:core. (See #6626.) |
| 33 class FutureGroup<T> { |
| 34 int _pending = 0; |
| 35 Completer<List<T>> _completer = new Completer<List<T>>(); |
| 36 final List<Future<T>> futures = <Future<T>>[]; |
| 37 bool completed = false; |
| 38 |
| 39 final List<T> _values = <T>[]; |
| 40 |
| 41 /// Wait for [task] to complete. |
| 42 Future<T> add(Future<T> task) { |
| 43 if (completed) { |
| 44 throw new StateError("The FutureGroup has already completed."); |
| 45 } |
| 46 |
| 47 _pending++; |
| 48 futures.add(task.then((value) { |
| 49 if (completed) return; |
| 50 |
| 51 _pending--; |
| 52 _values.add(value); |
| 53 |
| 54 if (_pending <= 0) { |
| 55 completed = true; |
| 56 _completer.complete(_values); |
| 57 } |
| 58 }).catchError((e) { |
| 59 if (completed) return; |
| 60 |
| 61 completed = true; |
| 62 _completer.completeError(e.error, e.stackTrace); |
| 63 })); |
| 64 |
| 65 return task; |
| 66 } |
| 67 |
| 68 Future<List> get future => _completer.future; |
| 69 } |
| 70 |
| 30 // TODO(rnystrom): Move into String? | 71 // TODO(rnystrom): Move into String? |
| 31 /// Pads [source] to [length] by adding spaces at the end. | 72 /// Pads [source] to [length] by adding spaces at the end. |
| 32 String padRight(String source, int length) { | 73 String padRight(String source, int length) { |
| 33 final result = new StringBuffer(); | 74 final result = new StringBuffer(); |
| 34 result.add(source); | 75 result.add(source); |
| 35 | 76 |
| 36 while (result.length < length) { | 77 while (result.length < length) { |
| 37 result.add(' '); | 78 result.add(' '); |
| 38 } | 79 } |
| 39 | 80 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 211 |
| 171 /// Add all key/value pairs from [source] to [destination], overwriting any | 212 /// Add all key/value pairs from [source] to [destination], overwriting any |
| 172 /// pre-existing values. | 213 /// pre-existing values. |
| 173 void mapAddAll(Map destination, Map source) => | 214 void mapAddAll(Map destination, Map source) => |
| 174 source.forEach((key, value) => destination[key] = value); | 215 source.forEach((key, value) => destination[key] = value); |
| 175 | 216 |
| 176 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 217 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
| 177 /// replacing `+` with ` `. | 218 /// replacing `+` with ` `. |
| 178 String urlDecode(String encoded) => | 219 String urlDecode(String encoded) => |
| 179 decodeUriComponent(encoded.replaceAll("+", " ")); | 220 decodeUriComponent(encoded.replaceAll("+", " ")); |
| OLD | NEW |