Commit fc37884
authored
refactor: extract Queue from IncrementalPublisher and IncrementalGraph (#4498)
A Queue is a batching async-generator meant to be consumed in the following pattern:
```ts
for await (const item of queue.subscribe(mapBatch)) {
.... // use item
}
```
where `mapBatch` is a function that takes a batch of type `Generator<T>` and returns a single item of type `U | undefined` where `undefined` means no item is emitted.
Items are produced as managed by an executor function passed to the Queue constructor, a la repeaters, see https://repeater.js.org/)
A `push()` method is provided as an argument to the executor 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 executor for convenience, but it is also available on the queue object itself so that it can be called within the executor or by the consumer to end early. So this works:
```ts
const queue = new Queue(
async (push, stop) => {
push(1);
push(2)
await Promise.resolve();
push(3);
stop();
},
);
const sub = queue.subscribe(batch => Array.from(batch));
const batch1 = await sub.next(); // batch1 = [1, 2]
const batch2 = await sub.next(); // batch2 = [3]
```
as does this:
```ts
let push;
const queue = new Queue(
(_push) => {
push = _push;
},
);
push(1);
push(2);
const sub = queue.subscribe(batch => Array.from(batch));
const batch1 = await sub.next(); // batch1 = [1, 2]
const batch2Promise = sub.next();
await Promise.resolve();
push(3);
queue.stop(); // using the stop() method on queue avoid the need to set stop = _stop
const batch2 = await batch2Promise; // batch2 = [3]
```
Note: concurrent calls to `subscribe` will reference the same queue and are not encouraged.
Using queues, we are able to remove all logic for handling the implicit queue from the IncrementalGraph and IncrementalPublisher, retaining only the `_handleCompletedBatch()`, while adding only the required `push()` and `stop()` calls within the IncrementalGraph.
Tests do not change, except that we have some extra ticks from the use of our new generators (including the use of `withCleanup()` to wrap, and so we have to adjust a few tick-sensitive tests.1 parent a401bf6 commit fc37884
File tree
5 files changed
+355
-100
lines changed- src/execution
- __tests__
5 files changed
+355
-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 | + | |
| 150 | + | |
| 151 | + | |
160 | 152 | | |
161 | 153 | | |
162 | 154 | | |
| |||
246 | 238 | | |
247 | 239 | | |
248 | 240 | | |
249 | | - | |
| 241 | + | |
250 | 242 | | |
251 | | - | |
| 243 | + | |
252 | 244 | | |
253 | 245 | | |
254 | 246 | | |
| |||
266 | 258 | | |
267 | 259 | | |
268 | 260 | | |
269 | | - | |
| 261 | + | |
270 | 262 | | |
271 | 263 | | |
272 | 264 | | |
| |||
290 | 282 | | |
291 | 283 | | |
292 | 284 | | |
293 | | - | |
| 285 | + | |
294 | 286 | | |
295 | 287 | | |
296 | 288 | | |
297 | 289 | | |
298 | 290 | | |
299 | 291 | | |
300 | | - | |
| 292 | + | |
301 | 293 | | |
302 | 294 | | |
303 | 295 | | |
| |||
320 | 312 | | |
321 | 313 | | |
322 | 314 | | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
328 | 318 | | |
329 | | - | |
330 | 319 | | |
331 | 320 | | |
| 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 | + | |
0 commit comments