Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Unified Diff: pkg/barback/lib/src/utils.dart

Issue 23469003: Add an AssetStream class to Barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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> {
+ /// 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;
+
+ /// 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;
+
+ /// 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();
+}
+
/// 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) {

Powered by Google App Engine
This is Rietveld 408576698