@rocicorp/zero 1.13.0-canary.3 → 1.13.0-canary.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/out/z2s/src/compiler.d.ts.map +1 -1
  2. package/out/z2s/src/compiler.js +6 -6
  3. package/out/z2s/src/compiler.js.map +1 -1
  4. package/out/zero/package.js +1 -1
  5. package/out/zero/package.js.map +1 -1
  6. package/out/zero-cache/src/server/replicator.js +3 -3
  7. package/out/zero-cache/src/server/replicator.js.map +1 -1
  8. package/out/zero-cache/src/services/replicator/write-worker-client.js +17 -3
  9. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  10. package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
  11. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  12. package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
  13. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  14. package/out/zero-client/src/client/custom.js +1 -1
  15. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  16. package/out/zero-client/src/client/version.js +1 -1
  17. package/out/zero-client/src/client/zero.js +1 -1
  18. package/out/zero-protocol/src/ast.d.ts +2 -2
  19. package/out/zero-protocol/src/ast.js +2 -2
  20. package/out/zero-protocol/src/ast.js.map +1 -1
  21. package/out/zero-protocol/src/down.d.ts +27 -27
  22. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  23. package/out/zql/src/ivm/aggregate.js +5 -4
  24. package/out/zql/src/ivm/aggregate.js.map +1 -1
  25. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  26. package/out/zql/src/ivm/cap.js +116 -63
  27. package/out/zql/src/ivm/cap.js.map +1 -1
  28. package/out/zql/src/ivm/deferred-input.js +1 -1
  29. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  30. package/out/zql/src/ivm/exists.js +136 -73
  31. package/out/zql/src/ivm/exists.js.map +1 -1
  32. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  33. package/out/zql/src/ivm/fan-in.js +3 -3
  34. package/out/zql/src/ivm/fan-in.js.map +1 -1
  35. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  36. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  37. package/out/zql/src/ivm/fan-out.js +47 -12
  38. package/out/zql/src/ivm/fan-out.js.map +1 -1
  39. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  40. package/out/zql/src/ivm/filter-operators.js +98 -13
  41. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  42. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  43. package/out/zql/src/ivm/filter-push.js +5 -13
  44. package/out/zql/src/ivm/filter-push.js.map +1 -1
  45. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  46. package/out/zql/src/ivm/filter.js +3 -2
  47. package/out/zql/src/ivm/filter.js.map +1 -1
  48. package/out/zql/src/ivm/flipped-join.js +1 -0
  49. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  50. package/out/zql/src/ivm/group-by.js +1 -1
  51. package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
  52. package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
  53. package/out/zql/src/ivm/join-index-tap.js +9 -7
  54. package/out/zql/src/ivm/join-index-tap.js.map +1 -1
  55. package/out/zql/src/ivm/join.d.ts.map +1 -1
  56. package/out/zql/src/ivm/join.js +72 -67
  57. package/out/zql/src/ivm/join.js.map +1 -1
  58. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  59. package/out/zql/src/ivm/memory-source.js +163 -57
  60. package/out/zql/src/ivm/memory-source.js.map +1 -1
  61. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  62. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  63. package/out/zql/src/ivm/push-accumulated.js +23 -28
  64. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  65. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  66. package/out/zql/src/ivm/skip.js +6 -9
  67. package/out/zql/src/ivm/skip.js.map +1 -1
  68. package/out/zql/src/ivm/stream.d.ts +58 -0
  69. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  70. package/out/zql/src/ivm/stream.js +256 -1
  71. package/out/zql/src/ivm/stream.js.map +1 -1
  72. package/out/zql/src/ivm/take-gate.d.ts +1 -1
  73. package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
  74. package/out/zql/src/ivm/take-gate.js +11 -21
  75. package/out/zql/src/ivm/take-gate.js.map +1 -1
  76. package/out/zql/src/ivm/take.d.ts.map +1 -1
  77. package/out/zql/src/ivm/take.js +243 -217
  78. package/out/zql/src/ivm/take.js.map +1 -1
  79. package/out/zql/src/query/query.d.ts +4 -4
  80. package/package.json +1 -1
@@ -3,6 +3,8 @@ import { hasOwn } from "../../../shared/src/has-own.js";
3
3
  import { makeAddChange, makeRemoveChange } from "./change.js";
4
4
  import { compareValues, valueToKey } from "./data.js";
5
5
  import { throwOutput } from "./operator.js";
6
+ import { emptyArray } from "../../../shared/src/sentinels.js";
7
+ import { andFinally, andThen, doneResult, eachStream } from "./stream.js";
6
8
  import { assertOrderingIncludesPK } from "../query/complete-ordering.js";
7
9
  //#region ../zql/src/ivm/take.ts
8
10
  /**
@@ -62,28 +64,18 @@ var Take = class {
62
64
  if (!takeState || takeState.size < this.#limit) return;
63
65
  return takeState.bound;
64
66
  }
65
- *fetch(req) {
67
+ fetch(req) {
66
68
  assert(!this.#partitionKey || req.constraint !== void 0 && constraintContainsPartitionKey(req.constraint, this.#partitionKey), "Partitioned take does not allow unpartitioned fetches");
67
69
  const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);
68
70
  const takeState = this.#storage.get(takeStateKey);
69
71
  if (!takeState) {
70
- if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) yield* this.#initialFetch(req, takeStateKey);
71
- return;
72
- }
73
- if (takeState.bound === void 0) return;
74
- let count = 0;
75
- for (const inputNode of this.#input.fetch(req)) {
76
- if (inputNode === "yield") {
77
- yield inputNode;
78
- continue;
79
- }
80
- if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) return;
81
- if (this.#rowHiddenFromFetch && this.getSchema().compareRows(this.#rowHiddenFromFetch, inputNode.row) === 0) continue;
82
- yield inputNode;
83
- count++;
84
- if (count >= this.#limit) return;
72
+ if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) return this.#initialFetch(req, takeStateKey);
73
+ return emptyArray;
85
74
  }
75
+ if (takeState.bound === void 0) return emptyArray;
76
+ return new TakeNodes(this.#input.fetch(req)[Symbol.iterator](), this.getSchema().compareRows, takeState.bound, this.#limit, this.#getRowHiddenFromFetch);
86
77
  }
78
+ #getRowHiddenFromFetch = () => this.#rowHiddenFromFetch;
87
79
  *#initialFetch(req, takeStateKey) {
88
80
  assert(req.start === void 0, "Start should be undefined");
89
81
  assert(!req.reverse, "Reverse should be false");
@@ -126,242 +118,220 @@ var Take = class {
126
118
  constraint
127
119
  };
128
120
  }
129
- *push(change) {
130
- if (change[0] === 2) {
131
- yield* this.#pushEditChange(change);
132
- return;
133
- }
121
+ push(change) {
122
+ if (change[0] === 2) return this.#pushEditChange(change);
134
123
  const { takeState, takeStateKey, constraint } = this.#getStateAndConstraint(change[1].row);
135
- if (!takeState) return;
124
+ if (!takeState) return emptyArray;
136
125
  const { compareRows } = this.getSchema();
137
126
  if (change[0] === 0) {
138
127
  if (takeState.size < this.#limit) {
139
128
  if (this.#dirtyPartitions.has(takeStateKey)) {
140
- if (takeState.bound === void 0 || compareRows(change[1].row, takeState.bound) >= 0) return;
129
+ if (takeState.bound === void 0 || compareRows(change[1].row, takeState.bound) >= 0) return emptyArray;
141
130
  }
142
131
  const nextSize = takeState.size + 1;
143
132
  if (nextSize === this.#limit) this.#dirtyPartitions.delete(takeStateKey);
144
133
  this.#setTakeState(takeStateKey, nextSize, takeState.bound === void 0 || compareRows(takeState.bound, change[1].row) < 0 ? change[1].row : takeState.bound);
145
- yield* this.#output.push(change, this);
146
- return;
134
+ return this.#output.push(change, this);
147
135
  }
148
- if (takeState.bound === void 0 || compareRows(change[1].row, takeState.bound) >= 0) return;
149
- let beforeBoundNode;
150
- let boundNode;
151
- if (this.#limit === 1) for (const node of this.#input.fetch({
136
+ if (takeState.bound === void 0 || compareRows(change[1].row, takeState.bound) >= 0) return emptyArray;
137
+ return this.#withFirst(this.#limit === 1 ? {
152
138
  start: {
153
139
  row: takeState.bound,
154
140
  basis: "at"
155
141
  },
156
142
  constraint
157
- })) {
158
- if (node === "yield") {
159
- yield node;
160
- continue;
161
- }
162
- boundNode = node;
163
- break;
164
- }
165
- else for (const node of this.#input.fetch({
143
+ } : {
166
144
  start: {
167
145
  row: takeState.bound,
168
146
  basis: "at"
169
147
  },
170
148
  constraint,
171
149
  reverse: true
172
- })) if (node === "yield") {
173
- yield node;
174
- continue;
175
- } else if (boundNode === void 0) boundNode = node;
176
- else {
177
- beforeBoundNode = node;
178
- break;
179
- }
180
- assert(boundNode !== void 0, "Take: boundNode must be found during fetch");
181
- const removeChange = makeRemoveChange(boundNode);
182
- this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode === void 0 || compareRows(change[1].row, beforeBoundNode.row) > 0 ? change[1].row : beforeBoundNode.row);
183
- yield* this.#pushWithRowHiddenFromFetch(change[1].row, removeChange);
184
- yield* this.#output.push(change, this);
150
+ }, this.#limit === 1 ? 1 : 2, false, ([boundNode, beforeBoundNode]) => {
151
+ assert(boundNode !== void 0, "Take: boundNode must be found during fetch");
152
+ const removeChange = makeRemoveChange(boundNode);
153
+ this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode === void 0 || compareRows(change[1].row, beforeBoundNode.row) > 0 ? change[1].row : beforeBoundNode.row);
154
+ return andThen(this.#pushWithRowHiddenFromFetch(change[1].row, removeChange), () => this.#output.push(change, this));
155
+ });
185
156
  } else if (change[0] === 1) {
186
- if (takeState.bound === void 0) return;
157
+ if (takeState.bound === void 0) return emptyArray;
187
158
  const compToBound = compareRows(change[1].row, takeState.bound);
188
- if (compToBound > 0 || compToBound < 0 && this.#limit === 1) return;
189
- let beforeBoundNode;
190
- for (const node of this.#input.fetch({
159
+ if (compToBound > 0 || compToBound < 0 && this.#limit === 1) return emptyArray;
160
+ const { bound } = takeState;
161
+ return this.#withFirst({
191
162
  start: {
192
- row: takeState.bound,
163
+ row: bound,
193
164
  basis: "after"
194
165
  },
195
166
  constraint,
196
167
  reverse: true
197
- })) {
198
- if (node === "yield") {
199
- yield node;
168
+ }, 1, false, ([beforeBoundNode]) => {
169
+ const finalBound = takeState.size - 1 === 0 ? void 0 : compToBound < 0 ? bound : beforeBoundNode?.row;
170
+ if (!this.#dirtyPartitions.has(takeStateKey)) this.#dirtyPartitions.set(takeStateKey, {
171
+ constraint,
172
+ gateBound: bound
173
+ });
174
+ this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);
175
+ return this.#output.push(change, this);
176
+ });
177
+ }
178
+ if (takeState.bound && compareRows(change[1].row, takeState.bound) <= 0) return this.#output.push(change, this);
179
+ return emptyArray;
180
+ }
181
+ /**
182
+ * Reads the first `n` nodes of a fetch from the input and calls `rest` with
183
+ * them (fewer if the fetch has fewer). With `gated`, the take gate is open
184
+ * while the fetch is read.
185
+ *
186
+ * `rest` runs right away unless the fetch yields. Then the stream returned
187
+ * holds the yields, with what is left of the fetch and `rest` behind them
188
+ * (see `andThen` in stream.ts).
189
+ */
190
+ #withFirst(req, n, gated, rest) {
191
+ const nodes = [];
192
+ let closeGate = gated;
193
+ if (gated) this.#takeGate?.open();
194
+ try {
195
+ const it = this.#input.fetch(req)[Symbol.iterator]();
196
+ for (;;) {
197
+ const r = it.next();
198
+ if (r.done) break;
199
+ if (r.value === "yield") {
200
+ closeGate = false;
201
+ return this.#withFirstRest(it, nodes, n, gated, rest);
202
+ }
203
+ nodes.push(r.value);
204
+ if (nodes.length === n) {
205
+ it.return?.();
206
+ break;
207
+ }
208
+ }
209
+ } finally {
210
+ if (closeGate) this.#takeGate?.close();
211
+ }
212
+ return rest(nodes);
213
+ }
214
+ /** #withFirst from the 'yield' it stopped at. */
215
+ *#withFirstRest(it, nodes, n, gated, rest) {
216
+ let finished = false;
217
+ try {
218
+ yield "yield";
219
+ for (;;) {
220
+ const r = it.next();
221
+ if (r.done) {
222
+ finished = true;
223
+ break;
224
+ }
225
+ if (r.value === "yield") {
226
+ yield "yield";
200
227
  continue;
201
228
  }
202
- beforeBoundNode = node;
203
- break;
229
+ nodes.push(r.value);
230
+ if (nodes.length === n) break;
204
231
  }
205
- const finalBound = takeState.size - 1 === 0 ? void 0 : compToBound < 0 ? takeState.bound : beforeBoundNode?.row;
206
- if (!this.#dirtyPartitions.has(takeStateKey)) this.#dirtyPartitions.set(takeStateKey, {
207
- constraint,
208
- gateBound: takeState.bound
209
- });
210
- this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);
211
- yield* this.#output.push(change, this);
212
- } else if (change[0] === 3) {
213
- if (takeState.bound && compareRows(change[1].row, takeState.bound) <= 0) yield* this.#output.push(change, this);
232
+ } finally {
233
+ if (!finished) it.return?.();
234
+ if (gated) this.#takeGate?.close();
214
235
  }
236
+ yield* rest(nodes);
215
237
  }
216
- *#pushEditChange(change) {
238
+ #pushEditChange(change) {
217
239
  assert(!this.#partitionKeyComparator || this.#partitionKeyComparator(change[2].row, change[1].row) === 0, "Unexpected change of partition key");
218
240
  const { takeState, takeStateKey, constraint } = this.#getStateAndConstraint(change[2].row);
219
- if (!takeState) return;
241
+ if (!takeState) return emptyArray;
220
242
  assert(takeState.bound, "Bound should be set");
243
+ const { bound } = takeState;
221
244
  const { compareRows } = this.getSchema();
222
- const oldCmp = compareRows(change[2].row, takeState.bound);
223
- const newCmp = compareRows(change[1].row, takeState.bound);
245
+ const oldCmp = compareRows(change[2].row, bound);
246
+ const newCmp = compareRows(change[1].row, bound);
224
247
  const replaceBoundAndForwardChange = () => {
225
248
  this.#setTakeState(takeStateKey, takeState.size, change[1].row);
226
249
  return this.#output.push(change, this);
227
250
  };
228
251
  if (oldCmp === 0) {
229
- if (newCmp === 0) {
230
- yield* this.#output.push(change, this);
231
- return;
232
- }
252
+ if (newCmp === 0) return this.#output.push(change, this);
233
253
  if (newCmp < 0) {
234
- if (this.#limit === 1) {
235
- yield* replaceBoundAndForwardChange();
236
- return;
237
- }
238
- let beforeBoundNode;
239
- for (const node of this.#input.fetch({
254
+ if (this.#limit === 1) return replaceBoundAndForwardChange();
255
+ return this.#withFirst({
240
256
  start: {
241
- row: takeState.bound,
257
+ row: bound,
242
258
  basis: "after"
243
259
  },
244
260
  constraint,
245
261
  reverse: true
246
- })) {
247
- if (node === "yield") {
248
- yield node;
249
- continue;
250
- }
251
- beforeBoundNode = node;
252
- break;
253
- }
254
- assert(beforeBoundNode !== void 0, "Take: beforeBoundNode must be found during fetch");
255
- this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row);
256
- yield* this.#output.push(change, this);
257
- return;
262
+ }, 1, false, ([beforeBoundNode]) => {
263
+ assert(beforeBoundNode !== void 0, "Take: beforeBoundNode must be found during fetch");
264
+ this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row);
265
+ return this.#output.push(change, this);
266
+ });
258
267
  }
259
268
  assert(newCmp > 0, "New comparison must be greater than 0");
260
- let newBoundNode;
261
- this.#takeGate?.open();
262
- try {
263
- for (const node of this.#input.fetch({
264
- start: {
265
- row: takeState.bound,
266
- basis: "at"
267
- },
268
- constraint
269
- })) {
270
- if (node === "yield") {
271
- yield node;
272
- continue;
273
- }
274
- newBoundNode = node;
275
- break;
276
- }
277
- } finally {
278
- this.#takeGate?.close();
279
- }
280
- assert(newBoundNode !== void 0, "Take: newBoundNode must be found during fetch");
281
- if (compareRows(newBoundNode.row, change[1].row) === 0) {
282
- yield* replaceBoundAndForwardChange();
283
- return;
284
- }
285
- this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);
286
- yield* this.#pushWithRowHiddenFromFetch(newBoundNode.row, makeRemoveChange(change[2]));
287
- yield* this.#output.push(makeAddChange(newBoundNode), this);
288
- return;
269
+ return this.#withFirst({
270
+ start: {
271
+ row: bound,
272
+ basis: "at"
273
+ },
274
+ constraint
275
+ }, 1, true, ([newBoundNode]) => {
276
+ assert(newBoundNode !== void 0, "Take: newBoundNode must be found during fetch");
277
+ if (compareRows(newBoundNode.row, change[1].row) === 0) return replaceBoundAndForwardChange();
278
+ this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);
279
+ return andThen(this.#pushWithRowHiddenFromFetch(newBoundNode.row, makeRemoveChange(change[2])), () => this.#output.push(makeAddChange(newBoundNode), this));
280
+ });
289
281
  }
290
282
  if (oldCmp > 0) {
291
283
  assert(newCmp !== 0, "Invalid state. Row has duplicate primary key");
292
- if (newCmp > 0) return;
284
+ if (newCmp > 0) return emptyArray;
293
285
  assert(newCmp < 0, "New comparison must be less than 0");
294
- let oldBoundNode;
295
- let newBoundNode;
296
- for (const node of this.#input.fetch({
286
+ return this.#withFirst({
297
287
  start: {
298
- row: takeState.bound,
288
+ row: bound,
299
289
  basis: "at"
300
290
  },
301
291
  constraint,
302
292
  reverse: true
303
- })) if (node === "yield") {
304
- yield node;
305
- continue;
306
- } else if (oldBoundNode === void 0) oldBoundNode = node;
307
- else {
308
- newBoundNode = node;
309
- break;
310
- }
311
- assert(oldBoundNode !== void 0, "Take: oldBoundNode must be found during fetch");
312
- assert(newBoundNode !== void 0, "Take: newBoundNode must be found during fetch");
313
- this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);
314
- yield* this.#pushWithRowHiddenFromFetch(change[1].row, makeRemoveChange(oldBoundNode));
315
- yield* this.#output.push(makeAddChange(change[1]), this);
316
- return;
293
+ }, 2, false, ([oldBoundNode, newBoundNode]) => {
294
+ assert(oldBoundNode !== void 0, "Take: oldBoundNode must be found during fetch");
295
+ assert(newBoundNode !== void 0, "Take: newBoundNode must be found during fetch");
296
+ this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);
297
+ return andThen(this.#pushWithRowHiddenFromFetch(change[1].row, makeRemoveChange(oldBoundNode)), () => this.#output.push(makeAddChange(change[1]), this));
298
+ });
317
299
  }
318
300
  if (oldCmp < 0) {
319
301
  assert(newCmp !== 0, "Invalid state. Row has duplicate primary key");
320
- if (newCmp < 0) {
321
- yield* this.#output.push(change, this);
322
- return;
323
- }
302
+ if (newCmp < 0) return this.#output.push(change, this);
324
303
  assert(newCmp > 0, "New comparison must be greater than 0");
325
- let afterBoundNode;
326
- this.#takeGate?.open();
327
- try {
328
- for (const node of this.#input.fetch({
329
- start: {
330
- row: takeState.bound,
331
- basis: "after"
332
- },
333
- constraint
334
- })) {
335
- if (node === "yield") {
336
- yield node;
337
- continue;
338
- }
339
- afterBoundNode = node;
340
- break;
341
- }
342
- } finally {
343
- this.#takeGate?.close();
344
- }
345
- assert(afterBoundNode !== void 0, "Take: afterBoundNode must be found during fetch");
346
- if (compareRows(afterBoundNode.row, change[1].row) === 0) {
347
- yield* replaceBoundAndForwardChange();
348
- return;
349
- }
350
- yield* this.#output.push(makeRemoveChange(change[2]), this);
351
- this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row);
352
- yield* this.#output.push(makeAddChange(afterBoundNode), this);
353
- return;
304
+ return this.#withFirst({
305
+ start: {
306
+ row: bound,
307
+ basis: "after"
308
+ },
309
+ constraint
310
+ }, 1, true, ([afterBoundNode]) => {
311
+ assert(afterBoundNode !== void 0, "Take: afterBoundNode must be found during fetch");
312
+ if (compareRows(afterBoundNode.row, change[1].row) === 0) return replaceBoundAndForwardChange();
313
+ return andThen(this.#output.push(makeRemoveChange(change[2]), this), () => {
314
+ this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row);
315
+ return this.#output.push(makeAddChange(afterBoundNode), this);
316
+ });
317
+ });
354
318
  }
355
319
  unreachable();
356
320
  }
357
- *#pushWithRowHiddenFromFetch(row, change) {
321
+ #pushWithRowHiddenFromFetch(row, change) {
358
322
  this.#rowHiddenFromFetch = row;
323
+ let s;
359
324
  try {
360
- yield* this.#output.push(change, this);
361
- } finally {
325
+ s = this.#output.push(change, this);
326
+ } catch (e) {
362
327
  this.#rowHiddenFromFetch = void 0;
328
+ throw e;
363
329
  }
330
+ return andFinally(s, this.#showHiddenRow);
364
331
  }
332
+ #showHiddenRow = () => {
333
+ this.#rowHiddenFromFetch = void 0;
334
+ };
365
335
  #setTakeState(takeStateKey, size, bound) {
366
336
  this.#storage.set(takeStateKey, {
367
337
  size,
@@ -372,45 +342,101 @@ var Take = class {
372
342
  this.#input.destroy();
373
343
  this.#storage.destroy();
374
344
  }
375
- *reconcile(_pusher) {
376
- if (this.#dirtyPartitions.size > 0) {
377
- const dirty = [...this.#dirtyPartitions.entries()];
378
- this.#dirtyPartitions.clear();
379
- for (const [takeStateKey, { constraint }] of dirty) {
380
- const takeState = this.#storage.get(takeStateKey);
381
- assert(takeState !== void 0, "Take: dirty partition must exist in storage");
382
- const deficit = this.#limit - takeState.size;
383
- assert(deficit > 0, "Take: dirty partition must have a deficit");
384
- const toPush = [];
385
- this.#takeGate?.open();
345
+ reconcile(_pusher) {
346
+ if (this.#dirtyPartitions.size === 0) return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
347
+ const dirty = [...this.#dirtyPartitions.entries()];
348
+ this.#dirtyPartitions.clear();
349
+ let i = 0;
350
+ return andThen(eachStream(() => i === dirty.length ? void 0 : this.#refill(dirty[i++])), () => this.#output.reconcile ? this.#output.reconcile(this) : emptyArray);
351
+ }
352
+ /** Refills a dirty partition with the rows that follow its bound. */
353
+ #refill([takeStateKey, { constraint }]) {
354
+ const takeState = this.#storage.get(takeStateKey);
355
+ assert(takeState !== void 0, "Take: dirty partition must exist in storage");
356
+ const deficit = this.#limit - takeState.size;
357
+ assert(deficit > 0, "Take: dirty partition must have a deficit");
358
+ return this.#withFirst({
359
+ start: takeState.bound ? {
360
+ row: takeState.bound,
361
+ basis: "after"
362
+ } : void 0,
363
+ constraint
364
+ }, deficit, true, (toPush) => {
365
+ let size = takeState.size;
366
+ let j = 0;
367
+ return eachStream(() => {
368
+ if (j === toPush.length) return;
369
+ const node = toPush[j++];
370
+ this.#setTakeState(takeStateKey, ++size, node.row);
371
+ return this.#output.push(makeAddChange(node), this);
372
+ });
373
+ });
374
+ }
375
+ };
376
+ /**
377
+ * The rows of a hydrated Take: its input up to the bound or the limit. An
378
+ * iterator rather than a generator, see `mapNodes` in stream.ts.
379
+ */
380
+ var TakeNodes = class {
381
+ #it;
382
+ #compareRows;
383
+ #bound;
384
+ #limit;
385
+ #getHidden;
386
+ #count = 0;
387
+ #done = false;
388
+ constructor(it, compareRows, bound, limit, getHidden) {
389
+ this.#it = it;
390
+ this.#compareRows = compareRows;
391
+ this.#bound = bound;
392
+ this.#limit = limit;
393
+ this.#getHidden = getHidden;
394
+ }
395
+ [Symbol.iterator]() {
396
+ return this;
397
+ }
398
+ next() {
399
+ if (this.#done) return doneResult;
400
+ if (this.#count >= this.#limit) return this.return();
401
+ for (;;) {
402
+ let r;
403
+ try {
404
+ r = this.#it.next();
405
+ } catch (e) {
406
+ this.#done = true;
407
+ throw e;
408
+ }
409
+ if (r.done) {
410
+ this.#done = true;
411
+ return r;
412
+ }
413
+ const node = r.value;
414
+ if (node === "yield") return r;
415
+ let past;
416
+ let hidden;
417
+ try {
418
+ past = this.#compareRows(this.#bound, node.row) < 0;
419
+ const hiddenRow = this.#getHidden();
420
+ hidden = !past && hiddenRow !== void 0 && this.#compareRows(hiddenRow, node.row) === 0;
421
+ } catch (e) {
422
+ this.#done = true;
386
423
  try {
387
- const stream = this.#input.fetch({
388
- start: takeState.bound ? {
389
- row: takeState.bound,
390
- basis: "after"
391
- } : void 0,
392
- constraint
393
- });
394
- for (const node of stream) {
395
- if (node === "yield") {
396
- yield "yield";
397
- continue;
398
- }
399
- toPush.push(node);
400
- if (toPush.length === deficit) break;
401
- }
402
- } finally {
403
- this.#takeGate?.close();
404
- }
405
- let currentSize = takeState.size;
406
- for (const node of toPush) {
407
- currentSize++;
408
- this.#setTakeState(takeStateKey, currentSize, node.row);
409
- yield* this.#output.push(makeAddChange(node), this);
410
- }
424
+ this.#it.return?.();
425
+ } catch {}
426
+ throw e;
411
427
  }
428
+ if (past) return this.return();
429
+ if (hidden) continue;
430
+ this.#count++;
431
+ return r;
432
+ }
433
+ }
434
+ return() {
435
+ if (!this.#done) {
436
+ this.#done = true;
437
+ this.#it.return?.();
412
438
  }
413
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
439
+ return doneResult;
414
440
  }
415
441
  };
416
442
  function getTakeStateKey(partitionKey, rowOrConstraint) {