Chromium Code Reviews| 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 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> { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 lines = lines.skip(1).map((line) => '$prefix$line').toList(); | 161 lines = lines.skip(1).map((line) => '$prefix$line').toList(); |
| 162 lines.insert(0, firstLine); | 162 lines.insert(0, firstLine); |
| 163 return lines.join('\n'); | 163 return lines.join('\n'); |
| 164 } | 164 } |
| 165 | 165 |
| 166 /// Returns a [Future] that completes after pumping the event queue [times] | 166 /// Returns a [Future] that completes after pumping the event queue [times] |
| 167 /// times. By default, this should pump the event queue enough times to allow | 167 /// times. By default, this should pump the event queue enough times to allow |
| 168 /// any code to run, as long as it's not waiting on some external event. | 168 /// any code to run, as long as it's not waiting on some external event. |
| 169 Future pumpEventQueue([int times=20]) { | 169 Future pumpEventQueue([int times=20]) { |
| 170 if (times == 0) return new Future.value(); | 170 if (times == 0) return new Future.value(); |
| 171 // We use a delayed future to allow runAsync events to finish. The | 171 // We use a delayed future to allow microtask events to finish. The |
| 172 // Future.value or Future() constructors use runAsync themselves and would | 172 // Future.value or Future() constructors use scheduleMicrotask themselves and |
|
Lasse Reichstein Nielsen
2013/10/07 07:17:03
`scheduleMicrotask` or [scheduleMicrotask]
floitsch
2013/10/10 16:00:36
In normal comments it's ok.
| |
| 173 // therefore not wait for runAsync callbacks that are scheduled after invoking | 173 // would therefore not wait for microtask callbacks that are scheduled after |
| 174 // this method. | 174 // invoking this method. |
| 175 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 175 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under | 178 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under |
| 179 /// the covers. | 179 /// the covers. |
| 180 Future newFuture(callback()) => new Future.value().then((_) => callback()); | 180 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| 181 | 181 |
| 182 /// Returns a buffered stream that will emit the same values as the stream | 182 /// Returns a buffered stream that will emit the same values as the stream |
| 183 /// returned by [future] once [future] completes. If [future] completes to an | 183 /// returned by [future] once [future] completes. If [future] completes to an |
| 184 /// error, the return value will emit that error and then close. | 184 /// error, the return value will emit that error and then close. |
| 185 Stream futureStream(Future<Stream> future) { | 185 Stream futureStream(Future<Stream> future) { |
| 186 var controller = new StreamController(sync: true); | 186 var controller = new StreamController(sync: true); |
| 187 future.then((stream) { | 187 future.then((stream) { |
| 188 stream.listen( | 188 stream.listen( |
| 189 controller.add, | 189 controller.add, |
| 190 onError: controller.addError, | 190 onError: controller.addError, |
| 191 onDone: controller.close); | 191 onDone: controller.close); |
| 192 }).catchError((e, StackTrace stackTrace) { | 192 }).catchError((e, StackTrace stackTrace) { |
| 193 controller.addError(e, stackTrace); | 193 controller.addError(e, stackTrace); |
| 194 controller.close(); | 194 controller.close(); |
| 195 }); | 195 }); |
| 196 return controller.stream; | 196 return controller.stream; |
| 197 } | 197 } |
| OLD | NEW |