@rocicorp/zero 0.25.0-canary.14 → 0.25.0-canary.15

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 (165) hide show
  1. package/out/shared/src/record-proxy.d.ts +13 -0
  2. package/out/shared/src/record-proxy.d.ts.map +1 -0
  3. package/out/shared/src/record-proxy.js +59 -0
  4. package/out/shared/src/record-proxy.js.map +1 -0
  5. package/out/zero/package.json.js +1 -1
  6. package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
  7. package/out/zero-cache/src/auth/write-authorizer.js +20 -13
  8. package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
  9. package/out/zero-cache/src/config/zero-config.d.ts +8 -0
  10. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  11. package/out/zero-cache/src/config/zero-config.js +12 -0
  12. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  13. package/out/zero-cache/src/server/otel-start.d.ts.map +1 -1
  14. package/out/zero-cache/src/server/otel-start.js +1 -5
  15. package/out/zero-cache/src/server/otel-start.js.map +1 -1
  16. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  17. package/out/zero-cache/src/server/syncer.js +6 -1
  18. package/out/zero-cache/src/server/syncer.js.map +1 -1
  19. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +8 -9
  20. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  21. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +17 -11
  22. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  23. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +2 -2
  24. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  25. package/out/zero-cache/src/services/view-syncer/snapshotter.js +19 -4
  26. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  27. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  28. package/out/zero-cache/src/services/view-syncer/view-syncer.js +1 -7
  29. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  30. package/out/zero-client/src/client/crud.d.ts +3 -3
  31. package/out/zero-client/src/client/crud.d.ts.map +1 -1
  32. package/out/zero-client/src/client/crud.js +23 -13
  33. package/out/zero-client/src/client/crud.js.map +1 -1
  34. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  35. package/out/zero-client/src/client/custom.js +4 -11
  36. package/out/zero-client/src/client/custom.js.map +1 -1
  37. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
  38. package/out/zero-client/src/client/ivm-branch.js +20 -13
  39. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  40. package/out/zero-client/src/client/make-mutate-property.d.ts +1 -1
  41. package/out/zero-client/src/client/make-mutate-property.d.ts.map +1 -1
  42. package/out/zero-client/src/client/make-mutate-property.js.map +1 -1
  43. package/out/zero-client/src/client/make-replicache-mutators.d.ts.map +1 -1
  44. package/out/zero-client/src/client/make-replicache-mutators.js +10 -6
  45. package/out/zero-client/src/client/make-replicache-mutators.js.map +1 -1
  46. package/out/zero-client/src/client/version.js +1 -1
  47. package/out/zero-client/src/client/zero.js +1 -1
  48. package/out/zero-client/src/client/zero.js.map +1 -1
  49. package/out/zero-server/src/custom.d.ts.map +1 -1
  50. package/out/zero-server/src/custom.js +12 -28
  51. package/out/zero-server/src/custom.js.map +1 -1
  52. package/out/zero-solid/src/solid-view.d.ts +1 -1
  53. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  54. package/out/zero-solid/src/solid-view.js +2 -0
  55. package/out/zero-solid/src/solid-view.js.map +1 -1
  56. package/out/zero-types/src/schema.d.ts +4 -4
  57. package/out/zql/src/builder/builder.d.ts.map +1 -1
  58. package/out/zql/src/builder/builder.js +1 -11
  59. package/out/zql/src/builder/builder.js.map +1 -1
  60. package/out/zql/src/error.js +1 -10
  61. package/out/zql/src/error.js.map +1 -1
  62. package/out/zql/src/ivm/array-view.d.ts +1 -1
  63. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  64. package/out/zql/src/ivm/array-view.js +2 -0
  65. package/out/zql/src/ivm/array-view.js.map +1 -1
  66. package/out/zql/src/ivm/exists.d.ts +3 -2
  67. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  68. package/out/zql/src/ivm/exists.js +25 -23
  69. package/out/zql/src/ivm/exists.js.map +1 -1
  70. package/out/zql/src/ivm/fan-in.d.ts +3 -3
  71. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  72. package/out/zql/src/ivm/fan-in.js +6 -5
  73. package/out/zql/src/ivm/fan-in.js.map +1 -1
  74. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  75. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  76. package/out/zql/src/ivm/fan-out.js +5 -5
  77. package/out/zql/src/ivm/fan-out.js.map +1 -1
  78. package/out/zql/src/ivm/filter-operators.d.ts +5 -5
  79. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  80. package/out/zql/src/ivm/filter-operators.js +8 -8
  81. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  82. package/out/zql/src/ivm/filter-push.d.ts +2 -1
  83. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  84. package/out/zql/src/ivm/filter-push.js +5 -5
  85. package/out/zql/src/ivm/filter-push.js.map +1 -1
  86. package/out/zql/src/ivm/filter.d.ts +2 -2
  87. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  88. package/out/zql/src/ivm/filter.js +4 -4
  89. package/out/zql/src/ivm/filter.js.map +1 -1
  90. package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
  91. package/out/zql/src/ivm/flipped-join.js +100 -83
  92. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  93. package/out/zql/src/ivm/join.d.ts.map +1 -1
  94. package/out/zql/src/ivm/join.js +52 -50
  95. package/out/zql/src/ivm/join.js.map +1 -1
  96. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
  97. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
  98. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +4 -4
  99. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
  100. package/out/zql/src/ivm/memory-source.d.ts +3 -3
  101. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  102. package/out/zql/src/ivm/memory-source.js +7 -4
  103. package/out/zql/src/ivm/memory-source.js.map +1 -1
  104. package/out/zql/src/ivm/operator.d.ts +10 -3
  105. package/out/zql/src/ivm/operator.d.ts.map +1 -1
  106. package/out/zql/src/ivm/operator.js.map +1 -1
  107. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  108. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  109. package/out/zql/src/ivm/push-accumulated.js +8 -8
  110. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  111. package/out/zql/src/ivm/skip.d.ts +1 -1
  112. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  113. package/out/zql/src/ivm/skip.js +8 -3
  114. package/out/zql/src/ivm/skip.js.map +1 -1
  115. package/out/zql/src/ivm/source.d.ts +15 -7
  116. package/out/zql/src/ivm/source.d.ts.map +1 -1
  117. package/out/zql/src/ivm/stream.d.ts +2 -0
  118. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  119. package/out/zql/src/ivm/stream.js +5 -14
  120. package/out/zql/src/ivm/stream.js.map +1 -1
  121. package/out/zql/src/ivm/take.d.ts +1 -1
  122. package/out/zql/src/ivm/take.d.ts.map +1 -1
  123. package/out/zql/src/ivm/take.js +164 -147
  124. package/out/zql/src/ivm/take.js.map +1 -1
  125. package/out/zql/src/ivm/union-fan-in.d.ts +2 -2
  126. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  127. package/out/zql/src/ivm/union-fan-in.js +7 -7
  128. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  129. package/out/zql/src/ivm/union-fan-out.d.ts +1 -1
  130. package/out/zql/src/ivm/union-fan-out.d.ts.map +1 -1
  131. package/out/zql/src/ivm/union-fan-out.js +3 -3
  132. package/out/zql/src/ivm/union-fan-out.js.map +1 -1
  133. package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
  134. package/out/zql/src/mutate/mutator-registry.js +1 -0
  135. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  136. package/out/zql/src/mutate/mutator.d.ts +10 -0
  137. package/out/zql/src/mutate/mutator.d.ts.map +1 -1
  138. package/out/zql/src/mutate/mutator.js.map +1 -1
  139. package/out/zql/src/planner/planner-builder.d.ts +2 -1
  140. package/out/zql/src/planner/planner-builder.d.ts.map +1 -1
  141. package/out/zql/src/planner/planner-builder.js +5 -5
  142. package/out/zql/src/planner/planner-builder.js.map +1 -1
  143. package/out/zql/src/planner/planner-graph.d.ts +3 -1
  144. package/out/zql/src/planner/planner-graph.d.ts.map +1 -1
  145. package/out/zql/src/planner/planner-graph.js +5 -5
  146. package/out/zql/src/planner/planner-graph.js.map +1 -1
  147. package/out/zql/src/query/create-builder.d.ts.map +1 -1
  148. package/out/zql/src/query/create-builder.js +7 -36
  149. package/out/zql/src/query/create-builder.js.map +1 -1
  150. package/out/zql/src/query/measure-push-operator.d.ts +1 -1
  151. package/out/zql/src/query/measure-push-operator.d.ts.map +1 -1
  152. package/out/zql/src/query/measure-push-operator.js +2 -2
  153. package/out/zql/src/query/measure-push-operator.js.map +1 -1
  154. package/out/zqlite/src/internal/sql-inline.d.ts +13 -0
  155. package/out/zqlite/src/internal/sql-inline.d.ts.map +1 -0
  156. package/out/zqlite/src/internal/sql-inline.js +45 -0
  157. package/out/zqlite/src/internal/sql-inline.js.map +1 -0
  158. package/out/zqlite/src/sqlite-cost-model.d.ts.map +1 -1
  159. package/out/zqlite/src/sqlite-cost-model.js +2 -2
  160. package/out/zqlite/src/sqlite-cost-model.js.map +1 -1
  161. package/out/zqlite/src/table-source.d.ts +3 -2
  162. package/out/zqlite/src/table-source.d.ts.map +1 -1
  163. package/out/zqlite/src/table-source.js +5 -2
  164. package/out/zqlite/src/table-source.js.map +1 -1
  165. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","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 first<T>(stream: Stream<T>): T | undefined {\n const it = stream[Symbol.iterator]();\n const {value} = it.next();\n it.return?.();\n return value;\n}\n"],"names":[],"mappings":"AASO,UAAU,KAAQ,QAAmB,OAA0B;AACpE,MAAI,QAAQ,GAAG;AACb;AAAA,EACF;AACA,MAAI,QAAQ;AACZ,aAAW,KAAK,QAAQ;AACtB,UAAM;AACN,QAAI,EAAE,UAAU,OAAO;AACrB;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,MAAS,QAAkC;AACzD,QAAM,KAAK,OAAO,OAAO,QAAQ,EAAA;AACjC,QAAM,EAAC,MAAA,IAAS,GAAG,KAAA;AACnB,KAAG,SAAA;AACH,SAAO;AACT;"}
1
+ {"version":3,"file":"stream.js","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 first<T>(stream: Stream<T>): T | undefined {\n const it = stream[Symbol.iterator]();\n const {value} = it.next();\n it.return?.();\n return value;\n}\n\nexport function consume<T>(stream: Stream<T>) {\n [...stream];\n}\n\nexport function drainGenerator<Yield, Return>(\n gen: Generator<Yield, Return, unknown>,\n): Return {\n let result = gen.next();\n while (!result.done) {\n result = gen.next();\n }\n return result.value;\n}\n"],"names":[],"mappings":"AAsBO,SAAS,MAAS,QAAkC;AACzD,QAAM,KAAK,OAAO,OAAO,QAAQ,EAAA;AACjC,QAAM,EAAC,MAAA,IAAS,GAAG,KAAA;AACnB,KAAG,SAAA;AACH,SAAO;AACT;AAEO,SAAS,QAAW,QAAmB;AAC5C,GAAC,GAAG,MAAM;AACZ;"}
@@ -22,7 +22,7 @@ export declare class Take implements Operator {
22
22
  setOutput(output: Output): void;
23
23
  getSchema(): SourceSchema;
24
24
  fetch(req: FetchRequest): Stream<Node | 'yield'>;
25
- push(change: Change): void;
25
+ push(change: Change): Stream<'yield'>;
26
26
  destroy(): void;
27
27
  }
28
28
  //# sourceMappingURL=take.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"take.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/take.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,2CAA2C,CAAC;AAE1E,OAAO,EAAC,KAAK,MAAM,EAAqC,MAAM,aAAa,CAAC;AAE5E,OAAO,EAAiC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACpE,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAc,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AAiBrD,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC;AAEtC;;;;;;;;;;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IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IA0ctC,OAAO,IAAI,IAAI;CAGhB"}
@@ -1,10 +1,8 @@
1
1
  import { assert, unreachable } from "../../../shared/src/asserts.js";
2
2
  import { hasOwn } from "../../../shared/src/has-own.js";
3
- import { must } from "../../../shared/src/must.js";
4
3
  import { assertOrderingIncludesPK } from "../query/complete-ordering.js";
5
4
  import { compareValues } from "./data.js";
6
- import { throwOutput, skipYields } from "./operator.js";
7
- import { first, take } from "./stream.js";
5
+ import { throwOutput } from "./operator.js";
8
6
  const MAX_BOUND_KEY = "maxBound";
9
7
  class Take {
10
8
  #input;
@@ -146,9 +144,9 @@ class Take {
146
144
  }
147
145
  return { takeState, takeStateKey, maxBound, constraint };
148
146
  }
149
- push(change) {
147
+ *push(change) {
150
148
  if (change.type === "edit") {
151
- this.#pushEditChange(change);
149
+ yield* this.#pushEditChange(change);
152
150
  return;
153
151
  }
154
152
  const { takeState, takeStateKey, maxBound, constraint } = this.#getStateAndConstraint(change.node.row);
@@ -164,7 +162,7 @@ class Take {
164
162
  takeState.bound === void 0 || compareRows(takeState.bound, change.node.row) < 0 ? change.node.row : takeState.bound,
165
163
  maxBound
166
164
  );
167
- this.#output.push(change, this);
165
+ yield* this.#output.push(change, this);
168
166
  return;
169
167
  }
170
168
  if (takeState.bound === void 0 || compareRows(change.node.row, takeState.bound) >= 0) {
@@ -173,34 +171,41 @@ class Take {
173
171
  let beforeBoundNode;
174
172
  let boundNode;
175
173
  if (this.#limit === 1) {
176
- boundNode = must(
177
- first(
178
- skipYields(
179
- this.#input.fetch({
180
- start: {
181
- row: takeState.bound,
182
- basis: "at"
183
- },
184
- constraint
185
- })
186
- )
187
- )
188
- );
174
+ for (const node of this.#input.fetch({
175
+ start: {
176
+ row: takeState.bound,
177
+ basis: "at"
178
+ },
179
+ constraint
180
+ })) {
181
+ if (node === "yield") {
182
+ yield node;
183
+ continue;
184
+ }
185
+ boundNode = node;
186
+ break;
187
+ }
189
188
  } else {
190
- [boundNode, beforeBoundNode] = take(
191
- skipYields(
192
- this.#input.fetch({
193
- start: {
194
- row: takeState.bound,
195
- basis: "at"
196
- },
197
- constraint,
198
- reverse: true
199
- })
200
- ),
201
- 2
202
- );
189
+ for (const node of this.#input.fetch({
190
+ start: {
191
+ row: takeState.bound,
192
+ basis: "at"
193
+ },
194
+ constraint,
195
+ reverse: true
196
+ })) {
197
+ if (node === "yield") {
198
+ yield node;
199
+ continue;
200
+ } else if (boundNode === void 0) {
201
+ boundNode = node;
202
+ } else {
203
+ beforeBoundNode = node;
204
+ break;
205
+ }
206
+ }
203
207
  }
208
+ assert(boundNode !== void 0);
204
209
  const removeChange = {
205
210
  type: "remove",
206
211
  node: boundNode
@@ -211,10 +216,8 @@ class Take {
211
216
  beforeBoundNode === void 0 || compareRows(change.node.row, beforeBoundNode.row) > 0 ? change.node.row : beforeBoundNode.row,
212
217
  maxBound
213
218
  );
214
- this.#withRowHiddenFromFetch(change.node.row, () => {
215
- this.#output.push(removeChange, this);
216
- });
217
- this.#output.push(change, this);
219
+ yield* this.#pushWithRowHiddenFromFetch(change.node.row, removeChange);
220
+ yield* this.#output.push(change, this);
218
221
  } else if (change.type === "remove") {
219
222
  if (takeState.bound === void 0) {
220
223
  return;
@@ -223,19 +226,22 @@ class Take {
223
226
  if (compToBound > 0) {
224
227
  return;
225
228
  }
226
- const [beforeBoundNode] = take(
227
- skipYields(
228
- this.#input.fetch({
229
- start: {
230
- row: takeState.bound,
231
- basis: "after"
232
- },
233
- constraint,
234
- reverse: true
235
- })
236
- ),
237
- 1
238
- );
229
+ let beforeBoundNode;
230
+ for (const node of this.#input.fetch({
231
+ start: {
232
+ row: takeState.bound,
233
+ basis: "after"
234
+ },
235
+ constraint,
236
+ reverse: true
237
+ })) {
238
+ if (node === "yield") {
239
+ yield node;
240
+ continue;
241
+ }
242
+ beforeBoundNode = node;
243
+ break;
244
+ }
239
245
  let newBound;
240
246
  if (beforeBoundNode) {
241
247
  const push = compareRows(beforeBoundNode.row, takeState.bound) > 0;
@@ -245,15 +251,17 @@ class Take {
245
251
  };
246
252
  }
247
253
  if (!newBound?.push) {
248
- for (const node of skipYields(
249
- this.#input.fetch({
250
- start: {
251
- row: takeState.bound,
252
- basis: "at"
253
- },
254
- constraint
255
- })
256
- )) {
254
+ for (const node of this.#input.fetch({
255
+ start: {
256
+ row: takeState.bound,
257
+ basis: "at"
258
+ },
259
+ constraint
260
+ })) {
261
+ if (node === "yield") {
262
+ yield node;
263
+ continue;
264
+ }
257
265
  const push = compareRows(node.row, takeState.bound) > 0;
258
266
  newBound = {
259
267
  node,
@@ -265,14 +273,14 @@ class Take {
265
273
  }
266
274
  }
267
275
  if (newBound?.push) {
268
- this.#output.push(change, this);
276
+ yield* this.#output.push(change, this);
269
277
  this.#setTakeState(
270
278
  takeStateKey,
271
279
  takeState.size,
272
280
  newBound.node.row,
273
281
  maxBound
274
282
  );
275
- this.#output.push(
283
+ yield* this.#output.push(
276
284
  {
277
285
  type: "add",
278
286
  node: newBound.node
@@ -287,14 +295,14 @@ class Take {
287
295
  newBound?.node.row,
288
296
  maxBound
289
297
  );
290
- this.#output.push(change, this);
298
+ yield* this.#output.push(change, this);
291
299
  } else if (change.type === "child") {
292
300
  if (takeState.bound && compareRows(change.node.row, takeState.bound) <= 0) {
293
- this.#output.push(change, this);
301
+ yield* this.#output.push(change, this);
294
302
  }
295
303
  }
296
304
  }
297
- #pushEditChange(change) {
305
+ *#pushEditChange(change) {
298
306
  assert(
299
307
  !this.#partitionKeyComparator || this.#partitionKeyComparator(change.oldNode.row, change.node.row) === 0,
300
308
  "Unexpected change of partition key"
@@ -307,64 +315,71 @@ class Take {
307
315
  const { compareRows } = this.getSchema();
308
316
  const oldCmp = compareRows(change.oldNode.row, takeState.bound);
309
317
  const newCmp = compareRows(change.node.row, takeState.bound);
310
- const replaceBoundAndForwardChange = () => {
311
- this.#setTakeState(
318
+ const that = this;
319
+ const replaceBoundAndForwardChange = function* () {
320
+ that.#setTakeState(
312
321
  takeStateKey,
313
322
  takeState.size,
314
323
  change.node.row,
315
324
  maxBound
316
325
  );
317
- this.#output.push(change, this);
326
+ yield* that.#output.push(change, that);
318
327
  };
319
328
  if (oldCmp === 0) {
320
329
  if (newCmp === 0) {
321
- this.#output.push(change, this);
330
+ yield* this.#output.push(change, this);
322
331
  return;
323
332
  }
324
333
  if (newCmp < 0) {
325
334
  if (this.#limit === 1) {
326
- replaceBoundAndForwardChange();
335
+ yield* replaceBoundAndForwardChange();
327
336
  return;
328
337
  }
329
- const beforeBoundNode = must(
330
- first(
331
- skipYields(
332
- this.#input.fetch({
333
- start: {
334
- row: takeState.bound,
335
- basis: "after"
336
- },
337
- constraint,
338
- reverse: true
339
- })
340
- )
341
- )
342
- );
338
+ let beforeBoundNode;
339
+ for (const node of this.#input.fetch({
340
+ start: {
341
+ row: takeState.bound,
342
+ basis: "after"
343
+ },
344
+ constraint,
345
+ reverse: true
346
+ })) {
347
+ if (node === "yield") {
348
+ yield node;
349
+ continue;
350
+ }
351
+ beforeBoundNode = node;
352
+ break;
353
+ }
354
+ assert(beforeBoundNode !== void 0);
343
355
  this.#setTakeState(
344
356
  takeStateKey,
345
357
  takeState.size,
346
358
  beforeBoundNode.row,
347
359
  maxBound
348
360
  );
349
- this.#output.push(change, this);
361
+ yield* this.#output.push(change, this);
350
362
  return;
351
363
  }
352
364
  assert(newCmp > 0, "New comparison must be greater than 0");
353
- const newBoundNode = must(
354
- first(
355
- skipYields(
356
- this.#input.fetch({
357
- start: {
358
- row: takeState.bound,
359
- basis: "at"
360
- },
361
- constraint
362
- })
363
- )
364
- )
365
- );
365
+ let newBoundNode;
366
+ for (const node of this.#input.fetch({
367
+ start: {
368
+ row: takeState.bound,
369
+ basis: "at"
370
+ },
371
+ constraint
372
+ })) {
373
+ if (node === "yield") {
374
+ yield node;
375
+ continue;
376
+ }
377
+ newBoundNode = node;
378
+ break;
379
+ }
380
+ assert(newBoundNode !== void 0);
366
381
  if (compareRows(newBoundNode.row, change.node.row) === 0) {
367
- replaceBoundAndForwardChange();
382
+ yield* replaceBoundAndForwardChange();
368
383
  return;
369
384
  }
370
385
  this.#setTakeState(
@@ -373,16 +388,11 @@ class Take {
373
388
  newBoundNode.row,
374
389
  maxBound
375
390
  );
376
- this.#withRowHiddenFromFetch(newBoundNode.row, () => {
377
- this.#output.push(
378
- {
379
- type: "remove",
380
- node: change.oldNode
381
- },
382
- this
383
- );
391
+ yield* this.#pushWithRowHiddenFromFetch(newBoundNode.row, {
392
+ type: "remove",
393
+ node: change.oldNode
384
394
  });
385
- this.#output.push(
395
+ yield* this.#output.push(
386
396
  {
387
397
  type: "add",
388
398
  node: newBoundNode
@@ -397,35 +407,39 @@ class Take {
397
407
  return;
398
408
  }
399
409
  assert(newCmp < 0, "New comparison must be less than 0");
400
- const [oldBoundNode, newBoundNode] = take(
401
- skipYields(
402
- this.#input.fetch({
403
- start: {
404
- row: takeState.bound,
405
- basis: "at"
406
- },
407
- constraint,
408
- reverse: true
409
- })
410
- ),
411
- 2
412
- );
410
+ let oldBoundNode;
411
+ let newBoundNode;
412
+ for (const node of this.#input.fetch({
413
+ start: {
414
+ row: takeState.bound,
415
+ basis: "at"
416
+ },
417
+ constraint,
418
+ reverse: true
419
+ })) {
420
+ if (node === "yield") {
421
+ yield node;
422
+ continue;
423
+ } else if (oldBoundNode === void 0) {
424
+ oldBoundNode = node;
425
+ } else {
426
+ newBoundNode = node;
427
+ break;
428
+ }
429
+ }
430
+ assert(oldBoundNode !== void 0);
431
+ assert(newBoundNode !== void 0);
413
432
  this.#setTakeState(
414
433
  takeStateKey,
415
434
  takeState.size,
416
435
  newBoundNode.row,
417
436
  maxBound
418
437
  );
419
- this.#withRowHiddenFromFetch(change.node.row, () => {
420
- this.#output.push(
421
- {
422
- type: "remove",
423
- node: oldBoundNode
424
- },
425
- this
426
- );
438
+ yield* this.#pushWithRowHiddenFromFetch(change.node.row, {
439
+ type: "remove",
440
+ node: oldBoundNode
427
441
  });
428
- this.#output.push(
442
+ yield* this.#output.push(
429
443
  {
430
444
  type: "add",
431
445
  node: change.node
@@ -437,28 +451,31 @@ class Take {
437
451
  if (oldCmp < 0) {
438
452
  assert(newCmp !== 0, "Invalid state. Row has duplicate primary key");
439
453
  if (newCmp < 0) {
440
- this.#output.push(change, this);
454
+ yield* this.#output.push(change, this);
441
455
  return;
442
456
  }
443
457
  assert(newCmp > 0, "New comparison must be greater than 0");
444
- const afterBoundNode = must(
445
- first(
446
- skipYields(
447
- this.#input.fetch({
448
- start: {
449
- row: takeState.bound,
450
- basis: "after"
451
- },
452
- constraint
453
- })
454
- )
455
- )
456
- );
458
+ let afterBoundNode;
459
+ for (const node of this.#input.fetch({
460
+ start: {
461
+ row: takeState.bound,
462
+ basis: "after"
463
+ },
464
+ constraint
465
+ })) {
466
+ if (node === "yield") {
467
+ yield node;
468
+ continue;
469
+ }
470
+ afterBoundNode = node;
471
+ break;
472
+ }
473
+ assert(afterBoundNode !== void 0);
457
474
  if (compareRows(afterBoundNode.row, change.node.row) === 0) {
458
- replaceBoundAndForwardChange();
475
+ yield* replaceBoundAndForwardChange();
459
476
  return;
460
477
  }
461
- this.#output.push(
478
+ yield* this.#output.push(
462
479
  {
463
480
  type: "remove",
464
481
  node: change.oldNode
@@ -471,7 +488,7 @@ class Take {
471
488
  afterBoundNode.row,
472
489
  maxBound
473
490
  );
474
- this.#output.push(
491
+ yield* this.#output.push(
475
492
  {
476
493
  type: "add",
477
494
  node: afterBoundNode
@@ -482,10 +499,10 @@ class Take {
482
499
  }
483
500
  unreachable();
484
501
  }
485
- #withRowHiddenFromFetch(row, fn) {
502
+ *#pushWithRowHiddenFromFetch(row, change) {
486
503
  this.#rowHiddenFromFetch = row;
487
504
  try {
488
- fn();
505
+ yield* this.#output.push(change, this);
489
506
  } finally {
490
507
  this.#rowHiddenFromFetch = void 0;
491
508
  }
@@ -1 +1 @@
1
- {"version":3,"file":"take.js","sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {type Change, type EditChange, type RemoveChange} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, type Comparator, type Node} from './data.ts';\nimport {\n skipYields,\n throwOutput,\n type FetchRequest,\n type Input,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {first, take, type Stream} from './stream.ts';\n\nconst MAX_BOUND_KEY = 'maxBound';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: typeof MAX_BOUND_KEY): Row | undefined;\n get(key: string): TakeState | undefined;\n set(key: typeof MAX_BOUND_KEY, value: Row): void;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #output: Output = throwOutput;\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n assertOrderingIncludesPK(\n input.getSchema().sort,\n input.getSchema().primaryKey,\n );\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (\n !this.#partitionKey ||\n (req.constraint &&\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey))\n ) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n yield* this.#initialFetch(req);\n return;\n }\n if (takeState.bound === undefined) {\n return;\n }\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) {\n return;\n }\n if (\n this.#rowHiddenFromFetch &&\n this.getSchema().compareRows(\n this.#rowHiddenFromFetch,\n inputNode.row,\n ) === 0\n ) {\n continue;\n }\n yield inputNode;\n }\n return;\n }\n // There is a partition key, but the fetch is not constrained or constrained\n // on a different key. Thus we don't have a single take state to bound by.\n // This currently only happens with nested sub-queries\n // e.g. issues include issuelabels include label. We could remove this\n // case if we added a translation layer (powered by some state) in join.\n // Specifically we need joinKeyValue => parent constraint key\n const maxBound = this.#storage.get(MAX_BOUND_KEY);\n if (maxBound === undefined) {\n return;\n }\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(inputNode.row, maxBound) > 0) {\n return;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, inputNode.row);\n const takeState = this.#storage.get(takeStateKey);\n if (\n takeState?.bound !== undefined &&\n this.getSchema().compareRows(takeState.bound, inputNode.row) >= 0\n ) {\n yield inputNode;\n }\n }\n }\n\n *#initialFetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n if (this.#limit === 0) {\n return;\n }\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n assert(\n this.#storage.get(takeStateKey) === undefined,\n 'Take state should be undefined',\n );\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(\n takeStateKey,\n size,\n bound,\n this.#storage.get(MAX_BOUND_KEY),\n );\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let maxBound: Row | undefined;\n let constraint: Constraint | undefined;\n if (takeState) {\n maxBound = this.#storage.get(MAX_BOUND_KEY);\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, maxBound, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n maxBound: undefined;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n maxBound: Row | undefined;\n constraint: Constraint | undefined;\n };\n }\n\n push(change: Change): void {\n if (change.type === 'edit') {\n this.#pushEditChange(change);\n return;\n }\n\n const {takeState, takeStateKey, maxBound, constraint} =\n this.#getStateAndConstraint(change.node.row);\n if (!takeState) {\n return;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change.type === 'add') {\n if (takeState.size < this.#limit) {\n this.#setTakeState(\n takeStateKey,\n takeState.size + 1,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change.node.row) < 0\n ? change.node.row\n : takeState.bound,\n maxBound,\n );\n this.#output.push(change, this);\n return;\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change.node.row, takeState.bound) >= 0\n ) {\n return;\n }\n // added row < bound\n let beforeBoundNode: Node | undefined;\n let boundNode: Node;\n if (this.#limit === 1) {\n boundNode = must(\n first(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n }),\n ),\n ),\n );\n } else {\n [boundNode, beforeBoundNode] = take(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n }),\n ),\n 2,\n );\n }\n const removeChange: RemoveChange = {\n type: 'remove',\n node: boundNode,\n };\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change.node.row, beforeBoundNode.row) > 0\n ? change.node.row\n : beforeBoundNode.row,\n maxBound,\n );\n this.#withRowHiddenFromFetch(change.node.row, () => {\n this.#output.push(removeChange, this);\n });\n this.#output.push(change, this);\n } else if (change.type === 'remove') {\n if (takeState.bound === undefined) {\n // change is after bound\n return;\n }\n const compToBound = compareRows(change.node.row, takeState.bound);\n if (compToBound > 0) {\n // change is after bound\n return;\n }\n const [beforeBoundNode] = take(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n }),\n ),\n 1,\n );\n\n let newBound: {node: Node; push: boolean} | undefined;\n if (beforeBoundNode) {\n const push = compareRows(beforeBoundNode.row, takeState.bound) > 0;\n newBound = {\n node: beforeBoundNode,\n push,\n };\n }\n if (!newBound?.push) {\n for (const node of skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n }),\n )) {\n const push = compareRows(node.row, takeState.bound) > 0;\n newBound = {\n node,\n push,\n };\n if (push) {\n break;\n }\n }\n }\n\n if (newBound?.push) {\n this.#output.push(change, this);\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBound.node.row,\n maxBound,\n );\n this.#output.push(\n {\n type: 'add',\n node: newBound.node,\n },\n this,\n );\n return;\n }\n this.#setTakeState(\n takeStateKey,\n takeState.size - 1,\n newBound?.node.row,\n maxBound,\n );\n this.#output.push(change, this);\n } else if (change.type === 'child') {\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change.node.row, takeState.bound) <= 0\n ) {\n this.#output.push(change, this);\n }\n }\n }\n\n #pushEditChange(change: EditChange): void {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(change.oldNode.row, change.node.row) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, maxBound, constraint} =\n this.#getStateAndConstraint(change.oldNode.row);\n if (!takeState) {\n return;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(change.oldNode.row, takeState.bound);\n const newCmp = compareRows(change.node.row, takeState.bound);\n\n const replaceBoundAndForwardChange = () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n change.node.row,\n maxBound,\n );\n this.#output.push(change, this);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n this.#output.push(change, this);\n return;\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n replaceBoundAndForwardChange();\n return;\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n\n const beforeBoundNode = must(\n first(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n }),\n ),\n ),\n );\n\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode.row,\n maxBound,\n );\n this.#output.push(change, this);\n return;\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n const newBoundNode = must(\n first(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n }),\n ),\n ),\n );\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (compareRows(newBoundNode.row, change.node.row) === 0) {\n replaceBoundAndForwardChange();\n return;\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBoundNode.row,\n maxBound,\n );\n this.#withRowHiddenFromFetch(newBoundNode.row, () => {\n this.#output.push(\n {\n type: 'remove',\n node: change.oldNode,\n },\n this,\n );\n });\n this.#output.push(\n {\n type: 'add',\n node: newBoundNode,\n },\n this,\n );\n return;\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n const [oldBoundNode, newBoundNode] = take(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n }),\n ),\n 2,\n );\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBoundNode.row,\n maxBound,\n );\n this.#withRowHiddenFromFetch(change.node.row, () => {\n this.#output.push(\n {\n type: 'remove',\n node: oldBoundNode,\n },\n this,\n );\n });\n this.#output.push(\n {\n type: 'add',\n node: change.node,\n },\n this,\n );\n\n return;\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n this.#output.push(change, this);\n return;\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n const afterBoundNode = must(\n first(\n skipYields(\n this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n }),\n ),\n ),\n );\n\n // The new row is the new bound. Use an edit change.\n if (compareRows(afterBoundNode.row, change.node.row) === 0) {\n replaceBoundAndForwardChange();\n return;\n }\n\n this.#output.push(\n {\n type: 'remove',\n node: change.oldNode,\n },\n this,\n );\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n afterBoundNode.row,\n maxBound,\n );\n this.#output.push(\n {\n type: 'add',\n node: afterBoundNode,\n },\n this,\n );\n return;\n }\n\n unreachable();\n }\n\n #withRowHiddenFromFetch(row: Row, fn: () => void) {\n this.#rowHiddenFromFetch = row;\n try {\n fn();\n } finally {\n this.#rowHiddenFromFetch = undefined;\n }\n }\n\n #setTakeState(\n takeStateKey: string,\n size: number,\n bound: Row | undefined,\n maxBound: Row | undefined,\n ) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n if (\n bound !== undefined &&\n (maxBound === undefined ||\n this.getSchema().compareRows(bound, maxBound) > 0)\n ) {\n this.#storage.set(MAX_BOUND_KEY, bound);\n }\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nfunction constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nfunction makePartitionKeyComparator(partitionKey: PartitionKey): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"names":[],"mappings":";;;;;;;AAqBA,MAAM,gBAAgB;AA4Bf,MAAM,KAAyB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAET;AAAA,EAEA,UAAkB;AAAA,EAElB,YACE,OACA,SACA,OACA,cACA;AACA,WAAO,SAAS,GAAG,4BAA4B;AAC/C;AAAA,MACE,MAAM,YAAY;AAAA,MAClB,MAAM,YAAY;AAAA,IAAA;AAEpB,UAAM,UAAU,IAAI;AACpB,SAAK,SAAS;AACd,SAAK,WAAW;AAChB,SAAK,SAAS;AACd,SAAK,gBAAgB;AACrB,SAAK,0BACH,gBAAgB,2BAA2B,YAAY;AAAA,EAC3D;AAAA,EAEA,UAAU,QAAsB;AAC9B,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,YAA0B;AACxB,WAAO,KAAK,OAAO,UAAA;AAAA,EACrB;AAAA,EAEA,CAAC,MAAM,KAA2C;AAChD,QACE,CAAC,KAAK,iBACL,IAAI,cACH,8BAA8B,IAAI,YAAY,KAAK,aAAa,GAClE;AACA,YAAM,eAAe,gBAAgB,KAAK,eAAe,IAAI,UAAU;AACvE,YAAM,YAAY,KAAK,SAAS,IAAI,YAAY;AAChD,UAAI,CAAC,WAAW;AACd,eAAO,KAAK,cAAc,GAAG;AAC7B;AAAA,MACF;AACA,UAAI,UAAU,UAAU,QAAW;AACjC;AAAA,MACF;AACA,iBAAW,aAAa,KAAK,OAAO,MAAM,GAAG,GAAG;AAC9C,YAAI,cAAc,SAAS;AACzB,gBAAM;AACN;A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B;AAAA,MACA;AAAA,IAAA,CACD;AACD,QACE,UAAU,WACT,aAAa,UACZ,KAAK,UAAA,EAAY,YAAY,OAAO,QAAQ,IAAI,IAClD;AACA,WAAK,SAAS,IAAI,eAAe,KAAK;AAAA,IACxC;AAAA,EACF;AAAA,EAEA,UAAgB;AACd,SAAK,OAAO,QAAA;AAAA,EACd;AACF;AAEA,SAAS,gBACP,cACA,iBACQ;AAGR,QAAM,kBAA2B,CAAA;AAEjC,MAAI,gBAAgB,iBAAiB;AACnC,eAAW,OAAO,cAAc;AAC9B,sBAAgB,KAAK,gBAAgB,GAAG,CAAC;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO,KAAK,UAAU,CAAC,QAAQ,GAAG,eAAe,CAAC;AACpD;AAEA,SAAS,8BACP,YACA,cACS;AACT,MAAI,eAAe,UAAa,iBAAiB,QAAW;AAC1D,WAAO,eAAe;AAAA,EACxB;AACA,MAAI,aAAa,WAAW,OAAO,KAAK,UAAU,EAAE,QAAQ;AAC1D,WAAO;AAAA,EACT;AACA,aAAW,OAAO,cAAc;AAC9B,QAAI,CAAC,OAAO,YAAY,GAAG,GAAG;AAC5B,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,2BAA2B,cAAwC;AAC1E,SAAO,CAAC,GAAG,MAAM;AACf,eAAW,OAAO,cAAc;AAC9B,YAAM,MAAM,cAAc,EAAE,GAAG,GAAG,EAAE,GAAG,CAAC;AACxC,UAAI,QAAQ,GAAG;AACb,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;"}
1
+ {"version":3,"file":"take.js","sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {type Change, type EditChange, type RemoveChange} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, type Comparator, type Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\nconst MAX_BOUND_KEY = 'maxBound';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: typeof MAX_BOUND_KEY): Row | undefined;\n get(key: string): TakeState | undefined;\n set(key: typeof MAX_BOUND_KEY, value: Row): void;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #output: Output = throwOutput;\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n assertOrderingIncludesPK(\n input.getSchema().sort,\n input.getSchema().primaryKey,\n );\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n if (\n !this.#partitionKey ||\n (req.constraint &&\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey))\n ) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n yield* this.#initialFetch(req);\n return;\n }\n if (takeState.bound === undefined) {\n return;\n }\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) {\n return;\n }\n if (\n this.#rowHiddenFromFetch &&\n this.getSchema().compareRows(\n this.#rowHiddenFromFetch,\n inputNode.row,\n ) === 0\n ) {\n continue;\n }\n yield inputNode;\n }\n return;\n }\n // There is a partition key, but the fetch is not constrained or constrained\n // on a different key. Thus we don't have a single take state to bound by.\n // This currently only happens with nested sub-queries\n // e.g. issues include issuelabels include label. We could remove this\n // case if we added a translation layer (powered by some state) in join.\n // Specifically we need joinKeyValue => parent constraint key\n const maxBound = this.#storage.get(MAX_BOUND_KEY);\n if (maxBound === undefined) {\n return;\n }\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(inputNode.row, maxBound) > 0) {\n return;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, inputNode.row);\n const takeState = this.#storage.get(takeStateKey);\n if (\n takeState?.bound !== undefined &&\n this.getSchema().compareRows(takeState.bound, inputNode.row) >= 0\n ) {\n yield inputNode;\n }\n }\n }\n\n *#initialFetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n if (this.#limit === 0) {\n return;\n }\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n assert(\n this.#storage.get(takeStateKey) === undefined,\n 'Take state should be undefined',\n );\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(\n takeStateKey,\n size,\n bound,\n this.#storage.get(MAX_BOUND_KEY),\n );\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let maxBound: Row | undefined;\n let constraint: Constraint | undefined;\n if (takeState) {\n maxBound = this.#storage.get(MAX_BOUND_KEY);\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, maxBound, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n maxBound: undefined;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n maxBound: Row | undefined;\n constraint: Constraint | undefined;\n };\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change.type === 'edit') {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const {takeState, takeStateKey, maxBound, constraint} =\n this.#getStateAndConstraint(change.node.row);\n if (!takeState) {\n return;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change.type === 'add') {\n if (takeState.size < this.#limit) {\n this.#setTakeState(\n takeStateKey,\n takeState.size + 1,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change.node.row) < 0\n ? change.node.row\n : takeState.bound,\n maxBound,\n );\n yield* this.#output.push(change, this);\n return;\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change.node.row, takeState.bound) >= 0\n ) {\n return;\n }\n // added row < bound\n let beforeBoundNode: Node | undefined;\n let boundNode: Node | undefined;\n if (this.#limit === 1) {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n boundNode = node;\n break;\n }\n } else {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (boundNode === undefined) {\n boundNode = node;\n } else {\n beforeBoundNode = node;\n break;\n }\n }\n }\n assert(boundNode !== undefined);\n const removeChange: RemoveChange = {\n type: 'remove',\n node: boundNode,\n };\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change.node.row, beforeBoundNode.row) > 0\n ? change.node.row\n : beforeBoundNode.row,\n maxBound,\n );\n yield* this.#pushWithRowHiddenFromFetch(change.node.row, removeChange);\n yield* this.#output.push(change, this);\n } else if (change.type === 'remove') {\n if (takeState.bound === undefined) {\n // change is after bound\n return;\n }\n const compToBound = compareRows(change.node.row, takeState.bound);\n if (compToBound > 0) {\n // change is after bound\n return;\n }\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n\n let newBound: {node: Node; push: boolean} | undefined;\n if (beforeBoundNode) {\n const push = compareRows(beforeBoundNode.row, takeState.bound) > 0;\n newBound = {\n node: beforeBoundNode,\n push,\n };\n }\n if (!newBound?.push) {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n const push = compareRows(node.row, takeState.bound) > 0;\n newBound = {\n node,\n push,\n };\n if (push) {\n break;\n }\n }\n }\n\n if (newBound?.push) {\n yield* this.#output.push(change, this);\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBound.node.row,\n maxBound,\n );\n yield* this.#output.push(\n {\n type: 'add',\n node: newBound.node,\n },\n this,\n );\n return;\n }\n this.#setTakeState(\n takeStateKey,\n takeState.size - 1,\n newBound?.node.row,\n maxBound,\n );\n yield* this.#output.push(change, this);\n } else if (change.type === 'child') {\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change.node.row, takeState.bound) <= 0\n ) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n *#pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(change.oldNode.row, change.node.row) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, maxBound, constraint} =\n this.#getStateAndConstraint(change.oldNode.row);\n if (!takeState) {\n return;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(change.oldNode.row, takeState.bound);\n const newCmp = compareRows(change.node.row, takeState.bound);\n\n const that = this;\n const replaceBoundAndForwardChange = function* () {\n that.#setTakeState(\n takeStateKey,\n takeState.size,\n change.node.row,\n maxBound,\n );\n yield* that.#output.push(change, that);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n yield* this.#output.push(change, this);\n return;\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n assert(beforeBoundNode !== undefined);\n\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode.row,\n maxBound,\n );\n yield* this.#output.push(change, this);\n return;\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n let newBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n newBoundNode = node;\n break;\n }\n assert(newBoundNode !== undefined);\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (compareRows(newBoundNode.row, change.node.row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBoundNode.row,\n maxBound,\n );\n yield* this.#pushWithRowHiddenFromFetch(newBoundNode.row, {\n type: 'remove',\n node: change.oldNode,\n });\n yield* this.#output.push(\n {\n type: 'add',\n node: newBoundNode,\n },\n this,\n );\n return;\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n let oldBoundNode: Node | undefined;\n let newBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (oldBoundNode === undefined) {\n oldBoundNode = node;\n } else {\n newBoundNode = node;\n break;\n }\n }\n assert(oldBoundNode !== undefined);\n assert(newBoundNode !== undefined);\n\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n newBoundNode.row,\n maxBound,\n );\n yield* this.#pushWithRowHiddenFromFetch(change.node.row, {\n type: 'remove',\n node: oldBoundNode,\n });\n yield* this.#output.push(\n {\n type: 'add',\n node: change.node,\n },\n this,\n );\n\n return;\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n yield* this.#output.push(change, this);\n return;\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n let afterBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n afterBoundNode = node;\n break;\n }\n assert(afterBoundNode !== undefined);\n\n // The new row is the new bound. Use an edit change.\n if (compareRows(afterBoundNode.row, change.node.row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n yield* this.#output.push(\n {\n type: 'remove',\n node: change.oldNode,\n },\n this,\n );\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n afterBoundNode.row,\n maxBound,\n );\n yield* this.#output.push(\n {\n type: 'add',\n node: afterBoundNode,\n },\n this,\n );\n return;\n }\n\n unreachable();\n }\n\n *#pushWithRowHiddenFromFetch(row: Row, change: Change) {\n this.#rowHiddenFromFetch = row;\n try {\n yield* this.#output.push(change, this);\n } finally {\n this.#rowHiddenFromFetch = undefined;\n }\n }\n\n #setTakeState(\n takeStateKey: string,\n size: number,\n bound: Row | undefined,\n maxBound: Row | undefined,\n ) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n if (\n bound !== undefined &&\n (maxBound === undefined ||\n this.getSchema().compareRows(bound, maxBound) > 0)\n ) {\n this.#storage.set(MAX_BOUND_KEY, bound);\n }\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nfunction constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nfunction makePartitionKeyComparator(partitionKey: PartitionKey): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"names":[],"mappings":";;;;;AAmBA,MAAM,gBAAgB;AA4Bf,MAAM,KAAyB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAET;AAAA,EAEA,UAAkB;AAAA,EAElB,YACE,OACA,SACA,OACA,cACA;AACA,WAAO,SAAS,GAAG,4BAA4B;AAC/C;AAAA,MACE,MAAM,YAAY;AAAA,MAClB,MAAM,YAAY;AAAA,IAAA;AAEpB,UAAM,UAAU,IAAI;AACpB,SAAK,SAAS;AACd,SAAK,WAAW;AAChB,SAAK,SAAS;AACd,SAAK,gBAAgB;AACrB,SAAK,0BACH,gBAAgB,2BAA2B,YAAY;AAAA,EAC3D;AAAA,EAEA,UAAU,QAAsB;AAC9B,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,YAA0B;AACxB,WAAO,KAAK,OAAO,UAAA;AAAA,EACrB;AAAA,EAEA,CAAC,MAAM,KAA2C;AAChD,QACE,CAAC,KAAK,iBACL,IAAI,cACH,8BAA8B,IAAI,YAAY,KAAK,aAA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AAQ,GAAG,eAAe,CAAC;AACpD;AAEA,SAAS,8BACP,YACA,cACS;AACT,MAAI,eAAe,UAAa,iBAAiB,QAAW;AAC1D,WAAO,eAAe;AAAA,EACxB;AACA,MAAI,aAAa,WAAW,OAAO,KAAK,UAAU,EAAE,QAAQ;AAC1D,WAAO;AAAA,EACT;AACA,aAAW,OAAO,cAAc;AAC9B,QAAI,CAAC,OAAO,YAAY,GAAG,GAAG;AAC5B,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,2BAA2B,cAAwC;AAC1E,SAAO,CAAC,GAAG,MAAM;AACf,eAAW,OAAO,cAAc;AAC9B,YAAM,MAAM,cAAc,EAAE,GAAG,GAAG,EAAE,GAAG,CAAC;AACxC,UAAI,QAAQ,GAAG;AACb,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;"}
@@ -10,9 +10,9 @@ export declare class UnionFanIn implements Operator {
10
10
  destroy(): void;
11
11
  fetch(req: FetchRequest): Stream<Node | 'yield'>;
12
12
  getSchema(): SourceSchema;
13
- push(change: Change, pusher: InputBase): void;
13
+ push(change: Change, pusher: InputBase): Stream<'yield'>;
14
14
  fanOutStartedPushing(): void;
15
- fanOutDonePushing(fanOutChangeType: Change['type']): void;
15
+ fanOutDonePushing(fanOutChangeType: Change['type']): Stream<'yield'>;
16
16
  setOutput(output: Output): void;
17
17
  }
18
18
  export declare function mergeFetches(fetches: Iterable<Node | 'yield'>[], comparator: (l: Node, r: Node) => number): IterableIterator<Node | 'yield'>;