| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.utils; | 5 library barback.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// A pair of values. | 9 /// A pair of values. |
| 10 class Pair<E, F> { | 10 class Pair<E, F> { |
| 11 E first; | 11 E first; |
| 12 F last; | 12 F last; |
| 13 | 13 |
| 14 Pair(this.first, this.last); | 14 Pair(this.first, this.last); |
| 15 | 15 |
| 16 String toString() => '($first, $last)'; | 16 String toString() => '($first, $last)'; |
| 17 | 17 |
| 18 bool operator==(other) { | 18 bool operator==(other) { |
| 19 if (other is! Pair) return false; | 19 if (other is! Pair) return false; |
| 20 return other.first == first && other.last == last; | 20 return other.first == first && other.last == last; |
| 21 } | 21 } |
| 22 | 22 |
| 23 int get hashCode => first.hashCode ^ last.hashCode; | 23 int get hashCode => first.hashCode ^ last.hashCode; |
| 24 } | 24 } |
| 25 | 25 |
| 26 /// A class that represents one and only one of two types of values. |
| 27 class Union<E, F> { |
| 28 /// Whether this is a value of type `E`. |
| 29 final bool isType1; |
| 30 |
| 31 /// Whether this is a value of type `F`. |
| 32 bool get isType2 => !isType1; |
| 33 |
| 34 /// The value of type `E`. |
| 35 /// |
| 36 /// It's an error to access this is this is of type `F`. |
| 37 E get type1 { |
| 38 assert(isType1); |
| 39 return _type1; |
| 40 } |
| 41 final E _type1; |
| 42 |
| 43 /// The value of type `F`. |
| 44 /// |
| 45 /// It's an error to access this is this is of type `E`. |
| 46 F get type2 { |
| 47 assert(isType2); |
| 48 return _type2; |
| 49 } |
| 50 final F _type2; |
| 51 |
| 52 /// Creates a union with type `E`. |
| 53 Union.withType1(this._type1) |
| 54 : _type2 = null, |
| 55 isType1 = true; |
| 56 |
| 57 /// Creates a union with type `F`. |
| 58 Union.withType2(this._type2) |
| 59 : _type1 = null, |
| 60 isType1 = false; |
| 61 |
| 62 String toString() => (isType1 ? type1 : type2).toString(); |
| 63 } |
| 64 |
| 26 /// Converts a number in the range [0-255] to a two digit hex string. | 65 /// Converts a number in the range [0-255] to a two digit hex string. |
| 27 /// | 66 /// |
| 28 /// For example, given `255`, returns `ff`. | 67 /// For example, given `255`, returns `ff`. |
| 29 String byteToHex(int byte) { | 68 String byteToHex(int byte) { |
| 30 assert(byte >= 0 && byte <= 255); | 69 assert(byte >= 0 && byte <= 255); |
| 31 | 70 |
| 32 const DIGITS = "0123456789abcdef"; | 71 const DIGITS = "0123456789abcdef"; |
| 33 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16]; | 72 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16]; |
| 34 } | 73 } |
| 35 | 74 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose | 109 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose |
| 71 /// values are the return values of [fn]. | 110 /// values are the return values of [fn]. |
| 72 Map mapMapValues(Map map, fn(key, value)) => | 111 Map mapMapValues(Map map, fn(key, value)) => |
| 73 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); | 112 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); |
| 74 | 113 |
| 75 /// Returns whether [set1] has exactly the same elements as [set2]. | 114 /// Returns whether [set1] has exactly the same elements as [set2]. |
| 76 bool setEquals(Set set1, Set set2) => | 115 bool setEquals(Set set1, Set set2) => |
| 77 set1.length == set2.length && set1.containsAll(set2); | 116 set1.length == set2.length && set1.containsAll(set2); |
| 78 | 117 |
| 79 /// Merges [streams] into a single stream that emits events from all sources. | 118 /// Merges [streams] into a single stream that emits events from all sources. |
| 80 Stream mergeStreams(Iterable<Stream> streams) { | 119 /// |
| 120 /// If [broadcast] is true, this will return a broadcast stream; otherwise, it |
| 121 /// will return a buffered stream. |
| 122 Stream mergeStreams(Iterable<Stream> streams, {bool broadcast: false}) { |
| 81 streams = streams.toList(); | 123 streams = streams.toList(); |
| 82 var doneCount = 0; | 124 var doneCount = 0; |
| 83 // Use a sync stream to preserve the synchrony behavior of the input streams. | 125 // Use a sync stream to preserve the synchrony behavior of the input streams. |
| 84 // If the inputs are sync, then this will be sync as well; if the inputs are | 126 // If the inputs are sync, then this will be sync as well; if the inputs are |
| 85 // async, then the events we receive will also be async, and forwarding them | 127 // async, then the events we receive will also be async, and forwarding them |
| 86 // sync won't change that. | 128 // sync won't change that. |
| 87 var controller = new StreamController(sync: true); | 129 var controller = broadcast ? new StreamController.broadcast(sync: true) |
| 130 : new StreamController(sync: true); |
| 88 | 131 |
| 89 for (var stream in streams) { | 132 for (var stream in streams) { |
| 90 stream.listen((value) { | 133 stream.listen((value) { |
| 91 controller.add(value); | 134 controller.add(value); |
| 92 }, onError: (error) { | 135 }, onError: (error) { |
| 93 controller.addError(error); | 136 controller.addError(error); |
| 94 }, onDone: () { | 137 }, onDone: () { |
| 95 doneCount++; | 138 doneCount++; |
| 96 if (doneCount == streams.length) controller.close(); | 139 if (doneCount == streams.length) controller.close(); |
| 97 }); | 140 }); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 // We use a delayed future to allow runAsync events to finish. The | 165 // We use a delayed future to allow runAsync events to finish. The |
| 123 // Future.value or Future() constructors use runAsync themselves and would | 166 // Future.value or Future() constructors use runAsync themselves and would |
| 124 // therefore not wait for runAsync callbacks that are scheduled after invoking | 167 // therefore not wait for runAsync callbacks that are scheduled after invoking |
| 125 // this method. | 168 // this method. |
| 126 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 169 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 127 } | 170 } |
| 128 | 171 |
| 129 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under | 172 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under |
| 130 /// the covers. | 173 /// the covers. |
| 131 Future newFuture(callback()) => new Future.value().then((_) => callback()); | 174 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| OLD | NEW |