Chromium Code Reviews| Index: pkg/barback/lib/src/utils.dart |
| diff --git a/pkg/barback/lib/src/utils.dart b/pkg/barback/lib/src/utils.dart |
| index 3272e9e0e3af008ef946a12c364fbf0dc587fdb7..482fb81910596c882ddad1d334303dae3087ad53 100644 |
| --- a/pkg/barback/lib/src/utils.dart |
| +++ b/pkg/barback/lib/src/utils.dart |
| @@ -23,6 +23,45 @@ class Pair<E, F> { |
| int get hashCode => first.hashCode ^ last.hashCode; |
| } |
| +/// A class that represents one and only one of two types of values. |
| +class Union<E, F> { |
|
Bob Nystrom
2013/08/27 17:20:29
In Haskell, this type is called "Either", which I
nweiz
2013/08/27 17:47:52
Done.
|
| + /// Whether this is a value of type `E`. |
| + final bool isType1; |
| + |
| + /// Whether this is a value of type `F`. |
| + bool get isType2 => !isType1; |
| + |
| + /// The value of type `E`. |
| + /// |
| + /// It's an error to access this is this is of type `F`. |
| + E get type1 { |
| + assert(isType1); |
| + return _type1; |
| + } |
| + final E _type1; |
|
Bob Nystrom
2013/08/27 17:20:29
You can just use a single var _value for either va
nweiz
2013/08/27 17:47:52
Done.
|
| + |
| + /// The value of type `F`. |
| + /// |
| + /// It's an error to access this is this is of type `E`. |
| + F get type2 { |
| + assert(isType2); |
| + return _type2; |
| + } |
| + final F _type2; |
|
Bob Nystrom
2013/08/27 17:20:29
See above and remove this.
nweiz
2013/08/27 17:47:52
Done.
|
| + |
| + /// Creates a union with type `E`. |
| + Union.withType1(this._type1) |
| + : _type2 = null, |
| + isType1 = true; |
| + |
| + /// Creates a union with type `F`. |
| + Union.withType2(this._type2) |
| + : _type1 = null, |
| + isType1 = false; |
| + |
| + String toString() => (isType1 ? type1 : type2).toString(); |
|
Bob Nystrom
2013/08/27 17:20:29
You should include the value of isType1 in the out
nweiz
2013/08/27 17:47:52
Done.
|
| +} |
| + |
| /// Converts a number in the range [0-255] to a two digit hex string. |
| /// |
| /// For example, given `255`, returns `ff`. |
| @@ -77,14 +116,18 @@ bool setEquals(Set set1, Set set2) => |
| set1.length == set2.length && set1.containsAll(set2); |
| /// Merges [streams] into a single stream that emits events from all sources. |
| -Stream mergeStreams(Iterable<Stream> streams) { |
| +/// |
| +/// If [broadcast] is true, this will return a broadcast stream; otherwise, it |
| +/// will return a buffered stream. |
| +Stream mergeStreams(Iterable<Stream> streams, {bool broadcast: false}) { |
| streams = streams.toList(); |
| var doneCount = 0; |
| // Use a sync stream to preserve the synchrony behavior of the input streams. |
| // If the inputs are sync, then this will be sync as well; if the inputs are |
| // async, then the events we receive will also be async, and forwarding them |
| // sync won't change that. |
| - var controller = new StreamController(sync: true); |
| + var controller = broadcast ? new StreamController.broadcast(sync: true) |
| + : new StreamController(sync: true); |
| for (var stream in streams) { |
| stream.listen((value) { |