Commit 3f71627
committed
refactor: extract Queue from IncrementalPublisher and IncrementalGraph
Queue is a batching async-aware iterator-like protocol meant to be consumed in the following (and only the following) pattern:
> let batch;
> if ((batch = queue.currentBatch()) !== undefined) {
> doSomethingWithBatch(batch);
> }
> while ((batch = await queue.nextBatch) !== undefined) {
> doSomethingWithBatch(batch);
> }
A `push()` methods is provided in the Queue constructor (a la repeaters, see https://repeater.js.org/) so that `push()` can be private to the code that constructs the Queue (although it can be saved to be passed to other code).
A `stop()` method is also provided as a the second argument to the constructor for convienence, but it is also available on the queue object itself so that it can be called by the executor or by the consumer. So this works:
> const queue = new Queue(
> async (push, stop) => {
> push(1);
> push(2)
> await Promise.resolve();
> push(3);
> stop();
> },
> );
>
> const batch1 = Array.from(queue.nextBatch()); // batch1 = [1, 2]
> const batch2 = Array.from(await queue.nextBatchAsync()); // batch2 = [3]
as does this:
> let push;
> const queue = new Queue(
> (_push) => {
> push = _push;
> },
> );
>
> push(1);
> push(2);
>
> const batch1 = Array.from(queue.nextBatch()); // batch1 = [1, 2]
>
> const batch2Promise = queue.nextBatchAsync();
>
> await Promise.resolve();
> push(3);
> queue.stop();
>
> const batch2 = await batch2Promise; // batch2 = [3]
Note: concurrent calls to `currentBatch()` and `nextBatch` will return the same batch and are not encouraged.
A `toAsyncIterable(mapFn)` method transforms coalesces each batch of items into a single value (or undefined if not value is to be emitted for the batch).
Using queues, we are able to remove all logic for handling the implicit queue from IncrementalPublisher, retaining only the `_handleCompletedBatch()`, while adding only the required `push()` and `stop()` calls within the IncrementalGraph.
Tests do not change, except that `.return()` and `.throw()` (but not `next()` have an extra tick secondary to the additional layers of `withCleanup()`, so that the tests required slight adjustment.1 parent aea5175 commit 3f71627
File tree
5 files changed
+311
-100
lines changed- src/execution
- __tests__
5 files changed
+311
-100
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
92 | 94 | | |
93 | 95 | | |
94 | 96 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | 97 | | |
119 | 98 | | |
120 | 99 | | |
| |||
146 | 125 | | |
147 | 126 | | |
148 | 127 | | |
| 128 | + | |
149 | 129 | | |
150 | 130 | | |
151 | 131 | | |
152 | 132 | | |
153 | 133 | | |
154 | 134 | | |
155 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
156 | 141 | | |
157 | 142 | | |
158 | 143 | | |
159 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
160 | 150 | | |
161 | 151 | | |
162 | 152 | | |
| |||
246 | 236 | | |
247 | 237 | | |
248 | 238 | | |
249 | | - | |
| 239 | + | |
250 | 240 | | |
251 | | - | |
| 241 | + | |
252 | 242 | | |
253 | 243 | | |
254 | 244 | | |
| |||
266 | 256 | | |
267 | 257 | | |
268 | 258 | | |
269 | | - | |
| 259 | + | |
270 | 260 | | |
271 | 261 | | |
272 | 262 | | |
| |||
290 | 280 | | |
291 | 281 | | |
292 | 282 | | |
293 | | - | |
| 283 | + | |
294 | 284 | | |
295 | 285 | | |
296 | 286 | | |
297 | 287 | | |
298 | 288 | | |
299 | 289 | | |
300 | | - | |
| 290 | + | |
301 | 291 | | |
302 | 292 | | |
303 | 293 | | |
| |||
320 | 310 | | |
321 | 311 | | |
322 | 312 | | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
328 | 316 | | |
329 | | - | |
330 | 317 | | |
331 | 318 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
72 | 71 | | |
73 | | - | |
74 | 72 | | |
75 | 73 | | |
76 | 74 | | |
| |||
95 | 93 | | |
96 | 94 | | |
97 | 95 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
104 | 99 | | |
105 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
157 | 140 | | |
158 | | - | |
159 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
160 | 144 | | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
171 | 149 | | |
172 | | - | |
173 | | - | |
174 | | - | |
| 150 | + | |
175 | 151 | | |
176 | | - | |
177 | | - | |
178 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
179 | 163 | | |
| 164 | + | |
180 | 165 | | |
181 | 166 | | |
182 | 167 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments