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 watcher.utils; | 5 library watcher.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:collection'; | 9 import 'dart:collection'; |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 controller = new StreamController( | 66 controller = new StreamController( |
67 sync: true, onListen: onListen, onCancel: onCancel); | 67 sync: true, onListen: onListen, onCancel: onCancel); |
68 } | 68 } |
69 return controller.stream; | 69 return controller.stream; |
70 } | 70 } |
71 | 71 |
72 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] | 72 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] |
73 /// under the covers. | 73 /// under the covers. |
74 Future newFuture(callback()) => new Future.value().then((_) => callback()); | 74 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
75 | 75 |
| 76 /// Returns a [Future] that completes after pumping the event queue [times] |
| 77 /// times. By default, this should pump the event queue enough times to allow |
| 78 /// any code to run, as long as it's not waiting on some external event. |
| 79 Future pumpEventQueue([int times=20]) { |
| 80 if (times == 0) return new Future.value(); |
| 81 // We use a delayed future to allow microtask events to finish. The |
| 82 // Future.value or Future() constructors use scheduleMicrotask themselves and |
| 83 // would therefore not wait for microtask callbacks that are scheduled after |
| 84 // invoking this method. |
| 85 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 86 } |
| 87 |
76 /// A stream transformer that batches all events that are sent at the same time. | 88 /// A stream transformer that batches all events that are sent at the same time. |
77 /// | 89 /// |
78 /// When multiple events are synchronously added to a stream controller, the | 90 /// When multiple events are synchronously added to a stream controller, the |
79 /// [StreamController] implementation uses [scheduleMicrotask] to schedule the | 91 /// [StreamController] implementation uses [scheduleMicrotask] to schedule the |
80 /// asynchronous firing of each event. In order to recreate the synchronous | 92 /// asynchronous firing of each event. In order to recreate the synchronous |
81 /// batches, this collates all the events that are received in "nearby" | 93 /// batches, this collates all the events that are received in "nearby" |
82 /// microtasks. | 94 /// microtasks. |
83 class BatchedStreamTransformer<T> implements StreamTransformer<T, List<T>> { | 95 class BatchedStreamTransformer<T> implements StreamTransformer<T, List<T>> { |
84 Stream<List<T>> bind(Stream<T> input) { | 96 Stream<List<T>> bind(Stream<T> input) { |
85 var batch = new Queue(); | 97 var batch = new Queue(); |
(...skipping 10 matching lines...) Expand all Loading... |
96 }); | 108 }); |
97 }, handleDone: (sink) { | 109 }, handleDone: (sink) { |
98 if (batch.isNotEmpty) { | 110 if (batch.isNotEmpty) { |
99 sink.add(batch.toList()); | 111 sink.add(batch.toList()); |
100 batch.clear(); | 112 batch.clear(); |
101 } | 113 } |
102 sink.close(); | 114 sink.close(); |
103 }).bind(input); | 115 }).bind(input); |
104 } | 116 } |
105 } | 117 } |
OLD | NEW |