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 utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 /// Configures [future] so that its result (success or exception) is passed on | 9 /// Configures [future] so that its result (success or exception) is passed on |
10 /// to [completer]. | 10 /// to [completer]. |
11 void chainToCompleter(Future future, Completer completer) { | 11 void chainToCompleter(Future future, Completer completer) { |
12 future.then((value) => completer.complete(value), | 12 future.then((value) => completer.complete(value), |
13 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 13 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
14 } | 14 } |
15 | 15 |
16 /// Prepends each line in [text] with [prefix]. | 16 /// Prepends each line in [text] with [prefix]. |
17 String prefixLines(String text, {String prefix: '| '}) => | 17 String prefixLines(String text, {String prefix: '| '}) => |
18 text.split('\n').map((line) => '$prefix$line').join('\n'); | 18 text.split('\n').map((line) => '$prefix$line').join('\n'); |
19 | 19 |
20 /// Returns a [Future] that completes after pumping the event queue [times] | 20 /// Returns a [Future] that completes after pumping the event queue [times] |
21 /// times. By default, this should pump the event queue enough times to allow | 21 /// times. By default, this should pump the event queue enough times to allow |
22 /// any code to run, as long as it's not waiting on some external event. | 22 /// any code to run, as long as it's not waiting on some external event. |
23 Future pumpEventQueue([int times=200]) { | 23 Future pumpEventQueue([int times=200]) { |
24 if (times == 0) return new Future.immediate(null); | 24 if (times == 0) return new Future.immediate(null); |
25 return new Future.immediate(null).then((_) => pumpEventQueue(times - 1)); | 25 return new Future.immediate(null).then((_) => pumpEventQueue(times - 1)); |
26 } | 26 } |
27 | |
28 /// Returns whether [iterable1] has the same elements in the same order as | |
29 /// [iterable2]. The elements are compared using `==`. | |
30 bool orderedIterableEquals(Iterable iterable1, Iterable iterable2) { | |
31 var iter1 = iterable1.iterator; | |
32 var iter2 = iterable2.iterator; | |
33 | |
34 while (true) { | |
35 if (iter1.current != iter2.current) return false; | |
Bob Nystrom
2013/02/22 17:58:21
The iterator protocol says you need to call moveNe
nweiz
2013/02/22 20:57:20
Done.
| |
36 var hasNext1 = iter1.moveNext(); | |
37 var hasNext2 = iter2.moveNext(); | |
38 if (hasNext1 != hasNext2) return false; | |
39 if (!hasNext1) return true; | |
40 } | |
41 } | |
42 | |
43 /// Returns a [Stream] that will immediately emit [error] and then close. | |
Bob Nystrom
2013/02/22 17:58:21
Given corelib has Future.immediateError, I wonder
nweiz
2013/02/22 20:57:20
Done.
| |
44 Stream errorStream(error) => new Future.immediateError(error).asStream(); | |
45 | |
46 /// Returns a buffered stream that will emit the same values as the stream | |
47 /// returned by [future] once [future] completes. If [future] completes to an | |
48 /// error, the return value will emit that error and then close. | |
49 Stream liftStream(Future<Stream> future) { | |
Bob Nystrom
2013/02/22 17:58:21
Instead of "lift", how about "deferred" or just "f
nweiz
2013/02/22 20:57:20
Done.
| |
50 var controller = new StreamController(); | |
51 future.then((stream) { | |
52 stream.listen( | |
53 controller.add, | |
54 onError: (error) => controller.signalError(error), | |
55 onDone: controller.close); | |
56 }).catchError((e) { | |
57 controller.signalError(e); | |
58 controller.close(); | |
59 }); | |
60 return controller.stream; | |
61 } | |
OLD | NEW |