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 var hasNext1 = iter1.moveNext(); |
| 36 var hasNext2 = iter2.moveNext(); |
| 37 if (hasNext1 != hasNext2) return false; |
| 38 if (!hasNext1) return true; |
| 39 if (iter1.current != iter2.current) return false; |
| 40 } |
| 41 } |
| 42 |
| 43 // TODO(nweiz): remove this when issue 8731 is fixed. |
| 44 /// Returns a [Stream] that will immediately emit [error] and then close. |
| 45 Stream errorStream(error) => new Future.immediateError(error).asStream(); |
| 46 |
| 47 /// Returns a buffered stream that will emit the same values as the stream |
| 48 /// returned by [future] once [future] completes. If [future] completes to an |
| 49 /// error, the return value will emit that error and then close. |
| 50 Stream futureStream(Future<Stream> future) { |
| 51 var controller = new StreamController(); |
| 52 future.then((stream) { |
| 53 stream.listen( |
| 54 controller.add, |
| 55 onError: (error) => controller.signalError(error), |
| 56 onDone: controller.close); |
| 57 }).catchError((e) { |
| 58 controller.signalError(e); |
| 59 controller.close(); |
| 60 }); |
| 61 return controller.stream; |
| 62 } |
OLD | NEW |