@rocicorp/zero 1.13.0-canary.2 → 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 (86) 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/change-source/pg/change-source-init.js +14 -4
  9. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  10. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +9 -0
  11. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  12. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +23 -1
  13. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  14. package/out/zero-cache/src/services/replicator/write-worker-client.js +17 -3
  15. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  16. package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
  17. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  18. package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
  19. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  20. package/out/zero-client/src/client/custom.js +1 -1
  21. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  22. package/out/zero-client/src/client/version.js +1 -1
  23. package/out/zero-client/src/client/zero.js +1 -1
  24. package/out/zero-protocol/src/ast.d.ts +2 -2
  25. package/out/zero-protocol/src/ast.js +2 -2
  26. package/out/zero-protocol/src/ast.js.map +1 -1
  27. package/out/zero-protocol/src/down.d.ts +27 -27
  28. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  29. package/out/zql/src/ivm/aggregate.js +5 -4
  30. package/out/zql/src/ivm/aggregate.js.map +1 -1
  31. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  32. package/out/zql/src/ivm/cap.js +116 -63
  33. package/out/zql/src/ivm/cap.js.map +1 -1
  34. package/out/zql/src/ivm/deferred-input.js +1 -1
  35. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  36. package/out/zql/src/ivm/exists.js +136 -73
  37. package/out/zql/src/ivm/exists.js.map +1 -1
  38. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  39. package/out/zql/src/ivm/fan-in.js +3 -3
  40. package/out/zql/src/ivm/fan-in.js.map +1 -1
  41. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  42. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  43. package/out/zql/src/ivm/fan-out.js +47 -12
  44. package/out/zql/src/ivm/fan-out.js.map +1 -1
  45. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  46. package/out/zql/src/ivm/filter-operators.js +98 -13
  47. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  48. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  49. package/out/zql/src/ivm/filter-push.js +5 -13
  50. package/out/zql/src/ivm/filter-push.js.map +1 -1
  51. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  52. package/out/zql/src/ivm/filter.js +3 -2
  53. package/out/zql/src/ivm/filter.js.map +1 -1
  54. package/out/zql/src/ivm/flipped-join.js +1 -0
  55. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  56. package/out/zql/src/ivm/group-by.js +1 -1
  57. package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
  58. package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
  59. package/out/zql/src/ivm/join-index-tap.js +9 -7
  60. package/out/zql/src/ivm/join-index-tap.js.map +1 -1
  61. package/out/zql/src/ivm/join.d.ts.map +1 -1
  62. package/out/zql/src/ivm/join.js +72 -67
  63. package/out/zql/src/ivm/join.js.map +1 -1
  64. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  65. package/out/zql/src/ivm/memory-source.js +163 -57
  66. package/out/zql/src/ivm/memory-source.js.map +1 -1
  67. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  68. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  69. package/out/zql/src/ivm/push-accumulated.js +23 -28
  70. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  71. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  72. package/out/zql/src/ivm/skip.js +6 -9
  73. package/out/zql/src/ivm/skip.js.map +1 -1
  74. package/out/zql/src/ivm/stream.d.ts +58 -0
  75. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  76. package/out/zql/src/ivm/stream.js +256 -1
  77. package/out/zql/src/ivm/stream.js.map +1 -1
  78. package/out/zql/src/ivm/take-gate.d.ts +1 -1
  79. package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
  80. package/out/zql/src/ivm/take-gate.js +11 -21
  81. package/out/zql/src/ivm/take-gate.js.map +1 -1
  82. package/out/zql/src/ivm/take.d.ts.map +1 -1
  83. package/out/zql/src/ivm/take.js +243 -217
  84. package/out/zql/src/ivm/take.js.map +1 -1
  85. package/out/zql/src/query/query.d.ts +4 -4
  86. package/package.json +1 -1
@@ -1,8 +1,263 @@
1
+ import { emptyArray } from "../../../shared/src/sentinels.js";
1
2
  //#region ../zql/src/ivm/stream.ts
2
3
  function consume(stream) {
3
4
  for (const _ of stream);
4
5
  }
6
+ /**
7
+ * `stream`, then the stream `rest` returns.
8
+ *
9
+ * A push through a chain of generators costs a generator per operator, though
10
+ * a push rarely yields. So an operator does its push right away and returns
11
+ * `emptyArray` when all of it is done. It returns a stream only when a push it
12
+ * makes downstream, or a fetch it reads, yields. That stream holds the yields
13
+ * and, behind them, the rest of the operator's work.
14
+ *
15
+ * So nothing may follow a downstream push directly. What follows it goes in
16
+ * `rest`:
17
+ *
18
+ * return andThen(output.push(a, this), () => output.push(b, this));
19
+ *
20
+ * `rest` runs right away when the first push is done, and otherwise once the
21
+ * stream of the first push has been read. Only `emptyArray` says that a push
22
+ * is done: any other stream is read first, an empty one too, which is correct
23
+ * and only slower.
24
+ *
25
+ * The stream a push returns must be read: by the time it is returned, the
26
+ * operator may have done part of its work and hold state that only the rest
27
+ * of the stream clears.
28
+ *
29
+ * This is for a fixed number of steps. A loop goes through `eachStream`.
30
+ */
31
+ function andThen(stream, rest) {
32
+ return stream === emptyArray ? rest() : chain(stream, rest);
33
+ }
34
+ function* chain(stream, rest) {
35
+ yield* stream;
36
+ const s = rest();
37
+ if (s !== emptyArray) yield* s;
38
+ }
39
+ /**
40
+ * Runs `cleanup` once `stream` is done, however it ends: `try { yield* stream
41
+ * } finally { cleanup() }` without the generator when there is nothing to
42
+ * wait for. The caller runs `cleanup` itself if getting the stream throws.
43
+ */
44
+ function andFinally(stream, cleanup) {
45
+ if (stream === emptyArray) {
46
+ cleanup();
47
+ return emptyArray;
48
+ }
49
+ return finallyAfter(stream, cleanup);
50
+ }
51
+ function* finallyAfter(stream, cleanup) {
52
+ try {
53
+ yield* stream;
54
+ } finally {
55
+ cleanup();
56
+ }
57
+ }
58
+ /**
59
+ * The streams `next` returns, one after the other, until it returns
60
+ * `undefined`: a loop whose body makes one push per round, without a generator
61
+ * for as long as every push is done.
62
+ *
63
+ * Written with `andThen`, such a loop would nest one generator per round that
64
+ * is not done, and a push to several thousand parents through an output that
65
+ * returns a generator would overflow the stack. Here, the first stream that is
66
+ * not done hands the rest of the loop to a single generator.
67
+ *
68
+ * `open` is an iterator that `next` reads. It is closed if the loop stops
69
+ * before `next` returns `undefined`, because `next` or a stream threw or the
70
+ * consumer stopped reading, as a `for...of` over that iterator would have
71
+ * closed it.
72
+ */
73
+ function eachStream(next, open) {
74
+ for (;;) {
75
+ let s;
76
+ try {
77
+ s = next();
78
+ } catch (e) {
79
+ closeAfterError(open);
80
+ throw e;
81
+ }
82
+ if (s === void 0) return emptyArray;
83
+ if (s !== emptyArray) return restOfEach(s, next, open);
84
+ }
85
+ }
86
+ function* restOfEach(first, next, open) {
87
+ let ended = false;
88
+ try {
89
+ yield* first;
90
+ for (;;) {
91
+ const s = next();
92
+ if (s === void 0) break;
93
+ if (s !== emptyArray) yield* s;
94
+ }
95
+ ended = true;
96
+ } catch (e) {
97
+ ended = true;
98
+ closeAfterError(open);
99
+ throw e;
100
+ } finally {
101
+ if (!ended) open?.return?.();
102
+ }
103
+ }
104
+ /**
105
+ * Closes `it` on the way out with an error. An error from closing it is
106
+ * dropped, as a `for...of` drops it: the first error is the one to report.
107
+ */
108
+ function closeAfterError(it) {
109
+ try {
110
+ it?.return?.();
111
+ } catch {}
112
+ }
113
+ /** One 'yield'. Returned for a round of `eachStream` that was reached by one. */
114
+ var yieldOnce = Object.freeze(["yield"]);
115
+ var doneResult = Object.freeze({
116
+ value: void 0,
117
+ done: true
118
+ });
119
+ var MappedNodes = class {
120
+ #it;
121
+ #fn;
122
+ #done = false;
123
+ constructor(it, fn) {
124
+ this.#it = it;
125
+ this.#fn = fn;
126
+ }
127
+ [Symbol.iterator]() {
128
+ return this;
129
+ }
130
+ next() {
131
+ if (this.#done) return doneResult;
132
+ let r;
133
+ try {
134
+ r = this.#it.next();
135
+ } catch (e) {
136
+ this.#done = true;
137
+ throw e;
138
+ }
139
+ if (r.done) {
140
+ this.#done = true;
141
+ return r;
142
+ }
143
+ if (r.value === "yield") return r;
144
+ let node;
145
+ try {
146
+ node = this.#fn(r.value);
147
+ } catch (e) {
148
+ this.#done = true;
149
+ closeAfterError(this.#it);
150
+ throw e;
151
+ }
152
+ return {
153
+ value: node,
154
+ done: false
155
+ };
156
+ }
157
+ return() {
158
+ if (!this.#done) {
159
+ this.#done = true;
160
+ this.#it.return?.();
161
+ }
162
+ return doneResult;
163
+ }
164
+ };
165
+ /** Yields `fn(node)` for each node of `stream`. */
166
+ function mapNodes(stream, fn) {
167
+ return new MappedNodes(stream[Symbol.iterator](), fn);
168
+ }
169
+ var TappedNodes = class {
170
+ #it;
171
+ #fn;
172
+ #done = false;
173
+ constructor(it, fn) {
174
+ this.#it = it;
175
+ this.#fn = fn;
176
+ }
177
+ [Symbol.iterator]() {
178
+ return this;
179
+ }
180
+ next() {
181
+ if (this.#done) return doneResult;
182
+ let r;
183
+ try {
184
+ r = this.#it.next();
185
+ } catch (e) {
186
+ this.#done = true;
187
+ throw e;
188
+ }
189
+ if (r.done) this.#done = true;
190
+ else if (r.value !== "yield") try {
191
+ this.#fn(r.value);
192
+ } catch (e) {
193
+ this.#done = true;
194
+ closeAfterError(this.#it);
195
+ throw e;
196
+ }
197
+ return r;
198
+ }
199
+ return() {
200
+ if (!this.#done) {
201
+ this.#done = true;
202
+ this.#it.return?.();
203
+ }
204
+ return doneResult;
205
+ }
206
+ };
207
+ /** Yields the nodes of `stream`, calling `fn` on each as it goes by. */
208
+ function tapNodes(stream, fn) {
209
+ return new TappedNodes(stream[Symbol.iterator](), fn);
210
+ }
211
+ var NodesWhile = class {
212
+ #it;
213
+ #fn;
214
+ #done = false;
215
+ constructor(it, fn) {
216
+ this.#it = it;
217
+ this.#fn = fn;
218
+ }
219
+ [Symbol.iterator]() {
220
+ return this;
221
+ }
222
+ next() {
223
+ if (this.#done) return doneResult;
224
+ let r;
225
+ try {
226
+ r = this.#it.next();
227
+ } catch (e) {
228
+ this.#done = true;
229
+ throw e;
230
+ }
231
+ if (r.done) {
232
+ this.#done = true;
233
+ return r;
234
+ }
235
+ if (r.value !== "yield") {
236
+ let passes;
237
+ try {
238
+ passes = this.#fn(r.value);
239
+ } catch (e) {
240
+ this.#done = true;
241
+ closeAfterError(this.#it);
242
+ throw e;
243
+ }
244
+ if (!passes) return this.return();
245
+ }
246
+ return r;
247
+ }
248
+ return() {
249
+ if (!this.#done) {
250
+ this.#done = true;
251
+ this.#it.return?.();
252
+ }
253
+ return doneResult;
254
+ }
255
+ };
256
+ /** Yields the nodes of `stream` up to the first for which `fn` is false. */
257
+ function nodesWhile(stream, fn) {
258
+ return new NodesWhile(stream[Symbol.iterator](), fn);
259
+ }
5
260
  //#endregion
6
- export { consume };
261
+ export { andFinally, andThen, consume, doneResult, eachStream, mapNodes, nodesWhile, tapNodes, yieldOnce };
7
262
 
8
263
  //# sourceMappingURL=stream.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","names":[],"sources":["../../../../../zql/src/ivm/stream.ts"],"sourcesContent":["/**\n * streams are lazy forward-only iterables.\n * Once a stream reaches the end it can't be restarted.\n * They are iterable, not iterator, so that they can be used in for-each,\n * and so that we know when consumer has stopped iterating the stream. This allows us\n * to clean up resources like sql statements.\n */\nexport type Stream<T> = Iterable<T>;\n\nexport function* take<T>(stream: Stream<T>, limit: number): Stream<T> {\n if (limit < 1) {\n return;\n }\n let count = 0;\n for (const v of stream) {\n yield v;\n if (++count === limit) {\n break;\n }\n }\n}\n\nexport function consume<T>(stream: Stream<T>): void {\n // Required to prevent some minifiers (e.g. Terser) from removing this empty loop\n for (const _ of stream);\n}\n"],"mappings":";AAsBA,SAAgB,QAAW,QAAyB;CAElD,KAAK,MAAM,KAAK;AAClB"}
1
+ {"version":3,"file":"stream.js","names":[],"sources":["../../../../../zql/src/ivm/stream.ts"],"sourcesContent":["import {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Node} from './data.ts';\n\n/**\n * streams are lazy forward-only iterables.\n * Once a stream reaches the end it can't be restarted.\n * They are iterable, not iterator, so that they can be used in for-each,\n * and so that we know when consumer has stopped iterating the stream. This allows us\n * to clean up resources like sql statements.\n */\nexport type Stream<T> = Iterable<T>;\n\nexport function* take<T>(stream: Stream<T>, limit: number): Stream<T> {\n if (limit < 1) {\n return;\n }\n let count = 0;\n for (const v of stream) {\n yield v;\n if (++count === limit) {\n break;\n }\n }\n}\n\nexport function consume<T>(stream: Stream<T>): void {\n // Required to prevent some minifiers (e.g. Terser) from removing this empty loop\n for (const _ of stream);\n}\n/**\n * `stream`, then the stream `rest` returns.\n *\n * A push through a chain of generators costs a generator per operator, though\n * a push rarely yields. So an operator does its push right away and returns\n * `emptyArray` when all of it is done. It returns a stream only when a push it\n * makes downstream, or a fetch it reads, yields. That stream holds the yields\n * and, behind them, the rest of the operator's work.\n *\n * So nothing may follow a downstream push directly. What follows it goes in\n * `rest`:\n *\n * return andThen(output.push(a, this), () => output.push(b, this));\n *\n * `rest` runs right away when the first push is done, and otherwise once the\n * stream of the first push has been read. Only `emptyArray` says that a push\n * is done: any other stream is read first, an empty one too, which is correct\n * and only slower.\n *\n * The stream a push returns must be read: by the time it is returned, the\n * operator may have done part of its work and hold state that only the rest\n * of the stream clears.\n *\n * This is for a fixed number of steps. A loop goes through `eachStream`.\n */\nexport function andThen(\n stream: Stream<'yield'>,\n rest: () => Stream<'yield'>,\n): Stream<'yield'> {\n return stream === emptyArray ? rest() : chain(stream, rest);\n}\n\nfunction* chain(\n stream: Stream<'yield'>,\n rest: () => Stream<'yield'>,\n): Stream<'yield'> {\n yield* stream;\n const s = rest();\n if (s !== emptyArray) {\n yield* s;\n }\n}\n\n/**\n * Runs `cleanup` once `stream` is done, however it ends: `try { yield* stream\n * } finally { cleanup() }` without the generator when there is nothing to\n * wait for. The caller runs `cleanup` itself if getting the stream throws.\n */\nexport function andFinally(\n stream: Stream<'yield'>,\n cleanup: () => void,\n): Stream<'yield'> {\n if (stream === emptyArray) {\n cleanup();\n return emptyArray;\n }\n return finallyAfter(stream, cleanup);\n}\n\nfunction* finallyAfter(\n stream: Stream<'yield'>,\n cleanup: () => void,\n): Stream<'yield'> {\n try {\n yield* stream;\n } finally {\n cleanup();\n }\n}\n\n/**\n * The streams `next` returns, one after the other, until it returns\n * `undefined`: a loop whose body makes one push per round, without a generator\n * for as long as every push is done.\n *\n * Written with `andThen`, such a loop would nest one generator per round that\n * is not done, and a push to several thousand parents through an output that\n * returns a generator would overflow the stack. Here, the first stream that is\n * not done hands the rest of the loop to a single generator.\n *\n * `open` is an iterator that `next` reads. It is closed if the loop stops\n * before `next` returns `undefined`, because `next` or a stream threw or the\n * consumer stopped reading, as a `for...of` over that iterator would have\n * closed it.\n */\nexport function eachStream(\n next: () => Stream<'yield'> | undefined,\n open?: Iterator<unknown>,\n): Stream<'yield'> {\n for (;;) {\n let s: Stream<'yield'> | undefined;\n try {\n s = next();\n } catch (e) {\n closeAfterError(open);\n throw e;\n }\n if (s === undefined) {\n return emptyArray;\n }\n if (s !== emptyArray) {\n return restOfEach(s, next, open);\n }\n }\n}\n\nfunction* restOfEach(\n first: Stream<'yield'>,\n next: () => Stream<'yield'> | undefined,\n open: Iterator<unknown> | undefined,\n): Stream<'yield'> {\n let ended = false;\n try {\n yield* first;\n for (;;) {\n const s = next();\n if (s === undefined) {\n break;\n }\n if (s !== emptyArray) {\n yield* s;\n }\n }\n ended = true;\n } catch (e) {\n ended = true;\n closeAfterError(open);\n throw e;\n } finally {\n // The consumer stopped reading.\n if (!ended) {\n open?.return?.();\n }\n }\n}\n\n/**\n * Closes `it` on the way out with an error. An error from closing it is\n * dropped, as a `for...of` drops it: the first error is the one to report.\n */\nfunction closeAfterError(it: Iterator<unknown> | undefined): void {\n try {\n it?.return?.();\n } catch {\n // The first error wins.\n }\n}\n\n/** One 'yield'. Returned for a round of `eachStream` that was reached by one. */\nexport const yieldOnce: Stream<'yield'> = Object.freeze(['yield'] as const);\n\n// The per-row steps of a fetch are iterators rather than generators, as\n// `SkipYieldsStream` is: resuming a generator costs several times what a call\n// to `next()` does, and a fetch pays it per row for each operator the row\n// passes through. Each one is both the iterable and the iterator, passes\n// 'yield' through, hands on the inner iterator's result object where the value\n// is unchanged, and closes the inner iterator when it stops early or its\n// callback throws, as a generator's `for...of` would.\n\ntype NodeResult = IteratorResult<Node | 'yield'>;\n\nexport const doneResult: IteratorReturnResult<undefined> = Object.freeze({\n value: undefined,\n done: true,\n});\n\nclass MappedNodes implements IterableIterator<Node | 'yield'> {\n readonly #it: Iterator<Node | 'yield'>;\n readonly #fn: (node: Node) => Node;\n #done = false;\n\n constructor(it: Iterator<Node | 'yield'>, fn: (node: Node) => Node) {\n this.#it = it;\n this.#fn = fn;\n }\n\n [Symbol.iterator](): this {\n return this;\n }\n\n next(): NodeResult {\n if (this.#done) {\n return doneResult;\n }\n let r: NodeResult;\n try {\n r = this.#it.next();\n } catch (e) {\n // A fetch that threw is over, as a generator's is.\n this.#done = true;\n throw e;\n }\n if (r.done) {\n this.#done = true;\n return r;\n }\n if (r.value === 'yield') {\n return r;\n }\n let node: Node;\n try {\n node = this.#fn(r.value);\n } catch (e) {\n this.#done = true;\n closeAfterError(this.#it);\n throw e;\n }\n return {value: node, done: false};\n }\n\n return(): NodeResult {\n if (!this.#done) {\n this.#done = true;\n this.#it.return?.();\n }\n return doneResult;\n }\n}\n\n/** Yields `fn(node)` for each node of `stream`. */\nexport function mapNodes(\n stream: Stream<Node | 'yield'>,\n fn: (node: Node) => Node,\n): Stream<Node | 'yield'> {\n return new MappedNodes(stream[Symbol.iterator](), fn);\n}\n\nclass TappedNodes implements IterableIterator<Node | 'yield'> {\n readonly #it: Iterator<Node | 'yield'>;\n readonly #fn: (node: Node) => void;\n #done = false;\n\n constructor(it: Iterator<Node | 'yield'>, fn: (node: Node) => void) {\n this.#it = it;\n this.#fn = fn;\n }\n\n [Symbol.iterator](): this {\n return this;\n }\n\n next(): NodeResult {\n if (this.#done) {\n return doneResult;\n }\n let r: NodeResult;\n try {\n r = this.#it.next();\n } catch (e) {\n // A fetch that threw is over, as a generator's is.\n this.#done = true;\n throw e;\n }\n if (r.done) {\n this.#done = true;\n } else if (r.value !== 'yield') {\n try {\n this.#fn(r.value);\n } catch (e) {\n this.#done = true;\n closeAfterError(this.#it);\n throw e;\n }\n }\n return r;\n }\n\n return(): NodeResult {\n if (!this.#done) {\n this.#done = true;\n this.#it.return?.();\n }\n return doneResult;\n }\n}\n\n/** Yields the nodes of `stream`, calling `fn` on each as it goes by. */\nexport function tapNodes(\n stream: Stream<Node | 'yield'>,\n fn: (node: Node) => void,\n): Stream<Node | 'yield'> {\n return new TappedNodes(stream[Symbol.iterator](), fn);\n}\n\nclass NodesWhile implements IterableIterator<Node | 'yield'> {\n readonly #it: Iterator<Node | 'yield'>;\n readonly #fn: (node: Node) => boolean;\n #done = false;\n\n constructor(it: Iterator<Node | 'yield'>, fn: (node: Node) => boolean) {\n this.#it = it;\n this.#fn = fn;\n }\n\n [Symbol.iterator](): this {\n return this;\n }\n\n next(): NodeResult {\n if (this.#done) {\n return doneResult;\n }\n let r: NodeResult;\n try {\n r = this.#it.next();\n } catch (e) {\n // A fetch that threw is over, as a generator's is.\n this.#done = true;\n throw e;\n }\n if (r.done) {\n this.#done = true;\n return r;\n }\n if (r.value !== 'yield') {\n let passes: boolean;\n try {\n passes = this.#fn(r.value);\n } catch (e) {\n this.#done = true;\n closeAfterError(this.#it);\n throw e;\n }\n if (!passes) {\n return this.return();\n }\n }\n return r;\n }\n\n return(): NodeResult {\n if (!this.#done) {\n this.#done = true;\n this.#it.return?.();\n }\n return doneResult;\n }\n}\n\n/** Yields the nodes of `stream` up to the first for which `fn` is false. */\nexport function nodesWhile(\n stream: Stream<Node | 'yield'>,\n fn: (node: Node) => boolean,\n): Stream<Node | 'yield'> {\n return new NodesWhile(stream[Symbol.iterator](), fn);\n}\n"],"mappings":";;AAyBA,SAAgB,QAAW,QAAyB;CAElD,KAAK,MAAM,KAAK;AAClB;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,QACd,QACA,MACiB;CACjB,OAAO,WAAW,aAAa,KAAK,IAAI,MAAM,QAAQ,IAAI;AAC5D;AAEA,UAAU,MACR,QACA,MACiB;CACjB,OAAO;CACP,MAAM,IAAI,KAAK;CACf,IAAI,MAAM,YACR,OAAO;AAEX;;;;;;AAOA,SAAgB,WACd,QACA,SACiB;CACjB,IAAI,WAAW,YAAY;EACzB,QAAQ;EACR,OAAO;CACT;CACA,OAAO,aAAa,QAAQ,OAAO;AACrC;AAEA,UAAU,aACR,QACA,SACiB;CACjB,IAAI;EACF,OAAO;CACT,UAAU;EACR,QAAQ;CACV;AACF;;;;;;;;;;;;;;;;AAiBA,SA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;EACpB,SAAS,GAAG;GAEV,KAAK,QAAQ;GACb,MAAM;EACR;EACA,IAAI,EAAE,MAAM;GACV,KAAK,QAAQ;GACb,OAAO;EACT;EACA,IAAI,EAAE,UAAU,SAAS;GACvB,IAAI;GACJ,IAAI;IACF,SAAS,KAAK,IAAI,EAAE,KAAK;GAC3B,SAAS,GAAG;IACV,KAAK,QAAQ;IACb,gBAAgB,KAAK,GAAG;IACxB,MAAM;GACR;GACA,IAAI,CAAC,QACH,OAAO,KAAK,OAAO;EAEvB;EACA,OAAO;CACT;CAEA,SAAqB;EACnB,IAAI,CAAC,KAAK,OAAO;GACf,KAAK,QAAQ;GACb,KAAK,IAAI,SAAS;EACpB;EACA,OAAO;CACT;AACF;;AAGA,SAAgB,WACd,QACA,IACwB;CACxB,OAAO,IAAI,WAAW,OAAO,OAAO,SAAS,CAAC,GAAG,EAAE;AACrD"}
@@ -4,7 +4,7 @@ import { type Constraint } from './constraint.ts';
4
4
  import type { Comparator, Node } from './data.ts';
5
5
  import { type FetchRequest, type Input, type InputBase, type Operator, type Output } from './operator.ts';
6
6
  import type { SourceSchema } from './schema.ts';
7
- import type { Stream } from './stream.ts';
7
+ import { type Stream } from './stream.ts';
8
8
  export interface TakeBoundProvider {
9
9
  getBound(constraint?: Constraint): Row | undefined;
10
10
  }
@@ -1 +1 @@
1
- {"version":3,"file":"take-gate.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/take-gate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAC5D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAuB,KAAK,UAAU,EAAC,MAAM,iBAAiB,CAAC;AACtE,OAAO,KAAK,EAAC,UAAU,EAAE,IAAI,EAAC,MAAM,WAAW,CAAC;AAChD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,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AAG,EACR,WAAW,EAAE,UAAU,GACtB,OAAO,CAOT;AAED;;;;;;;;GAQG;AACH,qBAAa,QAAS,YAAW,QAAQ,EAAE,iBAAiB;;IAO1D,YAAY,KAAK,EAAE,KAAK,EAMvB;IAED,IAAI,IAAI,IAAI,CAEX;IAED,KAAK,IAAI,IAAI,CAGZ;IAED,MAAM,IAAI,OAAO,CAEhB;IAED,gBAAgB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAElD;IAED,gBAAgB,IAAI,iBAAiB,GAAG,SAAS,CAEhD;IAED,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,GAAG,GAAG,SAAS,CAKjD;IAED,SAAS,IAAI,YAAY,CAExB;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,OAAO,IAAI,IAAI,CAEd;IAED,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAExD;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAE7C;IAED,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAe/C;CACF"}
@@ -1,5 +1,7 @@
1
1
  import { assert } from "../../../shared/src/asserts.js";
2
2
  import { throwOutput } from "./operator.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
4
+ import { nodesWhile } from "./stream.js";
3
5
  import { constraintMatchesRow } from "./constraint.js";
4
6
  //#region ../zql/src/ivm/take-gate.ts
5
7
  /**
@@ -74,30 +76,18 @@ var TakeGate = class {
74
76
  destroy() {
75
77
  this.#input.destroy();
76
78
  }
77
- *push(change, _pusher) {
78
- yield* this.#output.push(change, this);
79
+ push(change, _pusher) {
80
+ return this.#output.push(change, this);
79
81
  }
80
- *reconcile(_pusher) {
81
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
82
+ reconcile(_pusher) {
83
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
82
84
  }
83
- *fetch(req) {
84
- if (this.#openDepth > 0 || req.reverse) {
85
- yield* this.#input.fetch(req);
86
- return;
87
- }
85
+ fetch(req) {
86
+ if (this.#openDepth > 0 || req.reverse) return this.#input.fetch(req);
88
87
  const bound = this.#boundProvider?.getBound(req.constraint);
89
- if (!bound) {
90
- yield* this.#input.fetch(req);
91
- return;
92
- }
93
- for (const node of this.#input.fetch(req)) {
94
- if (node === "yield") {
95
- yield "yield";
96
- continue;
97
- }
98
- if (this.#comparator(node.row, bound) > 0) return;
99
- yield node;
100
- }
88
+ if (!bound) return this.#input.fetch(req);
89
+ const comparator = this.#comparator;
90
+ return nodesWhile(this.#input.fetch(req), (node) => comparator(node.row, bound) <= 0);
101
91
  }
102
92
  };
103
93
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"take-gate.js","names":[],"sources":["../../../../../zql/src/ivm/take-gate.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {Change} from './change.ts';\nimport {constraintMatchesRow, type Constraint} from './constraint.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport interface TakeBoundProvider {\n getBound(constraint?: Constraint): Row | undefined;\n}\n\n/**\n * The bound that a TakeGate caps one parent fetch of a join's child push at.\n * `undefined` means the fetch is not capped.\n */\nexport type ParentFetchBound = {\n readonly constraint: Constraint;\n readonly bound: Row | undefined;\n};\n\n/**\n * Reads the bounds for the parent fetches of a child push before the join\n * starts them. TakeGate reads its bound once, when a fetch starts, so the set\n * of parents that get the push stays fixed even when Take moves its bound\n * during the push. Split-push overlays must use these bounds, not the bound\n * Take reports later in the push.\n */\nexport function readParentFetchBounds(\n boundProvider: TakeBoundProvider,\n constraints: readonly Constraint[],\n): ParentFetchBound[] {\n return constraints.map(constraint => ({\n constraint,\n bound: boundProvider.getBound(constraint),\n }));\n}\n\n/**\n * Returns true if one of the parent fetches of the push yields `row`: `row`\n * matches the fetch's constraint and is not after the fetch's bound.\n */\nexport function isInParentFetch(\n bounds: readonly ParentFetchBound[],\n row: Row,\n compareRows: Comparator,\n): boolean {\n for (const {constraint, bound} of bounds) {\n if (constraintMatchesRow(constraint, row)) {\n return bound === undefined || compareRows(row, bound) <= 0;\n }\n }\n return false;\n}\n\n/**\n * TakeGate bounds upstream parent fetches using the active bound from a\n * downstream Take operator.\n *\n * When a child table changes, joins fetch matching parent rows. Without\n * TakeGate, this fetch scans all matching parent rows (which may be thousands)\n * even if downstream Take only needs a few. TakeGate intercepts this fetch and\n * stops the upstream stream once rows exceed Take's bound.\n */\nexport class TakeGate implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #comparator: Comparator;\n #boundProvider: TakeBoundProvider | undefined;\n #output: Output = throwOutput;\n #openDepth = 0;\n\n constructor(input: Input) {\n const {sort, compareRows} = input.getSchema();\n assert(sort !== undefined, 'TakeGate requires sorted input');\n this.#input = input;\n this.#comparator = compareRows;\n input.setOutput(this);\n }\n\n open(): void {\n this.#openDepth++;\n }\n\n close(): void {\n assert(this.#openDepth > 0, 'TakeGate.close called without matching open');\n this.#openDepth--;\n }\n\n isOpen(): boolean {\n return this.#openDepth > 0;\n }\n\n setBoundProvider(provider: TakeBoundProvider): void {\n this.#boundProvider = provider;\n }\n\n getBoundProvider(): TakeBoundProvider | undefined {\n return this.#boundProvider;\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (this.#openDepth > 0) {\n return undefined;\n }\n return this.#boundProvider?.getBound(constraint);\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n *push(change: Change, _pusher: InputBase): Stream<'yield'> {\n yield* this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (this.#openDepth > 0 || req.reverse) {\n yield* this.#input.fetch(req);\n return;\n }\n\n const bound = this.#boundProvider?.getBound(req.constraint);\n if (!bound) {\n yield* this.#input.fetch(req);\n return;\n }\n\n for (const node of this.#input.fetch(req)) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n if (this.#comparator(node.row, bound) > 0) {\n return;\n }\n yield node;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAoCA,SAAgB,sBACd,eACA,aACoB;CACpB,OAAO,YAAY,KAAI,gBAAe;EACpC;EACA,OAAO,cAAc,SAAS,UAAU;CAC1C,EAAE;AACJ;;;;;AAMA,SAAgB,gBACd,QACA,KACA,aACS;CACT,KAAK,MAAM,EAAC,YAAY,WAAU,QAChC,IAAI,qBAAqB,YAAY,GAAG,GACtC,OAAO,UAAU,KAAA,KAAa,YAAY,KAAK,KAAK,KAAK;CAG7D,OAAO;AACT;;;;;;;;;;AAWA,IAAa,WAAb,MAA6D;CAC3D;CACA;CACA;CACA,UAAkB;CAClB,aAAa;CAEb,YAAY,OAAc;EACxB,MAAM,EAAC,MAAM,gBAAe,MAAM,UAAU;EAC5C,OAAO,SAAS,KAAA,GAAW,gCAAgC;EAC3D,KAAK,SAAS;EACd,KAAK,cAAc;EACnB,MAAM,UAAU,IAAI;CACtB;CAEA,OAAa;EACX,KAAK;CACP;CAEA,QAAc;EACZ,OAAO,KAAK,aAAa,GAAG,6CAA6C;EACzE,KAAK;CACP;CAEA,SAAkB;EAChB,OAAO,KAAK,aAAa;CAC3B;CAEA,iBAAiB,UAAmC;EAClD,KAAK,iBAAiB;CACxB;CAEA,mBAAkD;EAChD,OAAO,KAAK;CACd;CAEA,SAAS,YAA0C;EACjD,IAAI,KAAK,aAAa,GACpB;EAEF,OAAO,KAAK,gBAAgB,SAAS,UAAU;CACjD;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,UAAU,QAAsB;EAC9B,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,CAAC,KAAK,QAAgB,SAAqC;EACzD,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CACvC;CAEA,CAAC,UAAU,SAAqC;EAC9C,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;CAEA,CAAC,MAAM,KAA2C;EAChD,IAAI,KAAK,aAAa,KAAK,IAAI,SAAS;GACtC,OAAO,KAAK,OAAO,MAAM,GAAG;GAC5B;EACF;EAEA,MAAM,QAAQ,KAAK,gBAAgB,SAAS,IAAI,UAAU;EAC1D,IAAI,CAAC,OAAO;GACV,OAAO,KAAK,OAAO,MAAM,GAAG;GAC5B;EACF;EAEA,KAAK,MAAM,QAAQ,KAAK,OAAO,MAAM,GAAG,GAAG;GACzC,IAAI,SAAS,SAAS;IACpB,MAAM;IACN;GACF;GACA,IAAI,KAAK,YAAY,KAAK,KAAK,KAAK,IAAI,GACtC;GAEF,MAAM;EACR;CACF;AACF"}
1
+ {"version":3,"file":"take-gate.js","names":[],"sources":["../../../../../zql/src/ivm/take-gate.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {Change} from './change.ts';\nimport {constraintMatchesRow, type Constraint} from './constraint.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {nodesWhile, type Stream} from './stream.ts';\n\nexport interface TakeBoundProvider {\n getBound(constraint?: Constraint): Row | undefined;\n}\n\n/**\n * The bound that a TakeGate caps one parent fetch of a join's child push at.\n * `undefined` means the fetch is not capped.\n */\nexport type ParentFetchBound = {\n readonly constraint: Constraint;\n readonly bound: Row | undefined;\n};\n\n/**\n * Reads the bounds for the parent fetches of a child push before the join\n * starts them. TakeGate reads its bound once, when a fetch starts, so the set\n * of parents that get the push stays fixed even when Take moves its bound\n * during the push. Split-push overlays must use these bounds, not the bound\n * Take reports later in the push.\n */\nexport function readParentFetchBounds(\n boundProvider: TakeBoundProvider,\n constraints: readonly Constraint[],\n): ParentFetchBound[] {\n return constraints.map(constraint => ({\n constraint,\n bound: boundProvider.getBound(constraint),\n }));\n}\n\n/**\n * Returns true if one of the parent fetches of the push yields `row`: `row`\n * matches the fetch's constraint and is not after the fetch's bound.\n */\nexport function isInParentFetch(\n bounds: readonly ParentFetchBound[],\n row: Row,\n compareRows: Comparator,\n): boolean {\n for (const {constraint, bound} of bounds) {\n if (constraintMatchesRow(constraint, row)) {\n return bound === undefined || compareRows(row, bound) <= 0;\n }\n }\n return false;\n}\n\n/**\n * TakeGate bounds upstream parent fetches using the active bound from a\n * downstream Take operator.\n *\n * When a child table changes, joins fetch matching parent rows. Without\n * TakeGate, this fetch scans all matching parent rows (which may be thousands)\n * even if downstream Take only needs a few. TakeGate intercepts this fetch and\n * stops the upstream stream once rows exceed Take's bound.\n */\nexport class TakeGate implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #comparator: Comparator;\n #boundProvider: TakeBoundProvider | undefined;\n #output: Output = throwOutput;\n #openDepth = 0;\n\n constructor(input: Input) {\n const {sort, compareRows} = input.getSchema();\n assert(sort !== undefined, 'TakeGate requires sorted input');\n this.#input = input;\n this.#comparator = compareRows;\n input.setOutput(this);\n }\n\n open(): void {\n this.#openDepth++;\n }\n\n close(): void {\n assert(this.#openDepth > 0, 'TakeGate.close called without matching open');\n this.#openDepth--;\n }\n\n isOpen(): boolean {\n return this.#openDepth > 0;\n }\n\n setBoundProvider(provider: TakeBoundProvider): void {\n this.#boundProvider = provider;\n }\n\n getBoundProvider(): TakeBoundProvider | undefined {\n return this.#boundProvider;\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (this.#openDepth > 0) {\n return undefined;\n }\n return this.#boundProvider?.getBound(constraint);\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n push(change: Change, _pusher: InputBase): Stream<'yield'> {\n return this.#output.push(change, this);\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (this.#openDepth > 0 || req.reverse) {\n return this.#input.fetch(req);\n }\n\n const bound = this.#boundProvider?.getBound(req.constraint);\n if (!bound) {\n return this.#input.fetch(req);\n }\n\n const comparator = this.#comparator;\n return nodesWhile(\n this.#input.fetch(req),\n node => comparator(node.row, bound) <= 0,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAqCA,SAAgB,sBACd,eACA,aACoB;CACpB,OAAO,YAAY,KAAI,gBAAe;EACpC;EACA,OAAO,cAAc,SAAS,UAAU;CAC1C,EAAE;AACJ;;;;;AAMA,SAAgB,gBACd,QACA,KACA,aACS;CACT,KAAK,MAAM,EAAC,YAAY,WAAU,QAChC,IAAI,qBAAqB,YAAY,GAAG,GACtC,OAAO,UAAU,KAAA,KAAa,YAAY,KAAK,KAAK,KAAK;CAG7D,OAAO;AACT;;;;;;;;;;AAWA,IAAa,WAAb,MAA6D;CAC3D;CACA;CACA;CACA,UAAkB;CAClB,aAAa;CAEb,YAAY,OAAc;EACxB,MAAM,EAAC,MAAM,gBAAe,MAAM,UAAU;EAC5C,OAAO,SAAS,KAAA,GAAW,gCAAgC;EAC3D,KAAK,SAAS;EACd,KAAK,cAAc;EACnB,MAAM,UAAU,IAAI;CACtB;CAEA,OAAa;EACX,KAAK;CACP;CAEA,QAAc;EACZ,OAAO,KAAK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