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

Side by Side 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: Code review changes. Created 7 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 Either<E, F> {
28 /// Whether this is a value of type `E`.
29 final bool isFirst;
30
31 /// Whether this is a value of type `F`.
32 bool get isSecond => !isFirst;
33
34 /// The value, either of type `E` or `F`.
35 final _value;
36
37 /// The value of type `E`.
38 ///
39 /// It's an error to access this is this is of type `F`.
40 E get first {
41 assert(isFirst);
42 return _value;
43 }
44
45 /// The value of type `F`.
46 ///
47 /// It's an error to access this is this is of type `E`.
48 F get second {
49 assert(isSecond);
50 return _value;
51 }
52
53 /// Creates an [Either] with type `E`.
54 Either.withFirst(this._value)
55 : isFirst = true;
56
57 /// Creates an [Either] with type `F`.
58 Either.withSecond(this._value)
59 : isFirst = false;
60
61 /// Runs [whenFirst] or [whenSecond] depending on the type of [this].
62 ///
63 /// Returns the result of whichvever function was run.
64 match(whenFirst(E value), whenSecond(F value)) {
65 if (isFirst) return whenFirst(first);
66 return whenSecond(second);
67 }
68
69 String toString() => "$_value (${isFirst? 'first' : 'second'})";
70 }
71
26 /// Converts a number in the range [0-255] to a two digit hex string. 72 /// Converts a number in the range [0-255] to a two digit hex string.
27 /// 73 ///
28 /// For example, given `255`, returns `ff`. 74 /// For example, given `255`, returns `ff`.
29 String byteToHex(int byte) { 75 String byteToHex(int byte) {
30 assert(byte >= 0 && byte <= 255); 76 assert(byte >= 0 && byte <= 255);
31 77
32 const DIGITS = "0123456789abcdef"; 78 const DIGITS = "0123456789abcdef";
33 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16]; 79 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16];
34 } 80 }
35 81
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose 116 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose
71 /// values are the return values of [fn]. 117 /// values are the return values of [fn].
72 Map mapMapValues(Map map, fn(key, value)) => 118 Map mapMapValues(Map map, fn(key, value)) =>
73 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); 119 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key]));
74 120
75 /// Returns whether [set1] has exactly the same elements as [set2]. 121 /// Returns whether [set1] has exactly the same elements as [set2].
76 bool setEquals(Set set1, Set set2) => 122 bool setEquals(Set set1, Set set2) =>
77 set1.length == set2.length && set1.containsAll(set2); 123 set1.length == set2.length && set1.containsAll(set2);
78 124
79 /// Merges [streams] into a single stream that emits events from all sources. 125 /// Merges [streams] into a single stream that emits events from all sources.
80 Stream mergeStreams(Iterable<Stream> streams) { 126 ///
127 /// If [broadcast] is true, this will return a broadcast stream; otherwise, it
128 /// will return a buffered stream.
129 Stream mergeStreams(Iterable<Stream> streams, {bool broadcast: false}) {
81 streams = streams.toList(); 130 streams = streams.toList();
82 var doneCount = 0; 131 var doneCount = 0;
83 // Use a sync stream to preserve the synchrony behavior of the input streams. 132 // 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 133 // 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 134 // async, then the events we receive will also be async, and forwarding them
86 // sync won't change that. 135 // sync won't change that.
87 var controller = new StreamController(sync: true); 136 var controller = broadcast ? new StreamController.broadcast(sync: true)
137 : new StreamController(sync: true);
88 138
89 for (var stream in streams) { 139 for (var stream in streams) {
90 stream.listen((value) { 140 stream.listen((value) {
91 controller.add(value); 141 controller.add(value);
92 }, onError: (error) { 142 }, onError: (error) {
93 controller.addError(error); 143 controller.addError(error);
94 }, onDone: () { 144 }, onDone: () {
95 doneCount++; 145 doneCount++;
96 if (doneCount == streams.length) controller.close(); 146 if (doneCount == streams.length) controller.close();
97 }); 147 });
(...skipping 24 matching lines...) Expand all
122 // We use a delayed future to allow runAsync events to finish. The 172 // We use a delayed future to allow runAsync events to finish. The
123 // Future.value or Future() constructors use runAsync themselves and would 173 // Future.value or Future() constructors use runAsync themselves and would
124 // therefore not wait for runAsync callbacks that are scheduled after invoking 174 // therefore not wait for runAsync callbacks that are scheduled after invoking
125 // this method. 175 // this method.
126 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); 176 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1));
127 } 177 }
128 178
129 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under 179 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under
130 /// the covers. 180 /// the covers.
131 Future newFuture(callback()) => new Future.value().then((_) => callback()); 181 Future newFuture(callback()) => new Future.value().then((_) => callback());
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698