@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 +1 @@
1
- {"version":3,"file":"push-accumulated.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/push-accumulated.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAKL,KAAK,MAAM,EACZ,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,SAAS,EAAE,MAAM,EAAC,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC;;;;;;;;;;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AAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,EAC3D,iBAAiB,EAAE,MAAM,EAAE,QAO5B"}
@@ -2,6 +2,7 @@ import { assert, unreachable } from "../../../shared/src/asserts.js";
2
2
  import { must } from "../../../shared/src/must.js";
3
3
  import { makeAddChange, makeChildChange, makeEditChange, makeRemoveChange } from "./change.js";
4
4
  import { emptyArray } from "../../../shared/src/sentinels.js";
5
+ import { andThen, eachStream } from "./stream.js";
5
6
  //#region ../zql/src/ivm/push-accumulated.ts
6
7
  /**
7
8
  * # pushAccumulatedChanges
@@ -72,8 +73,8 @@ import { emptyArray } from "../../../shared/src/sentinels.js";
72
73
  * If an edit enters and is converted to only add or only remove, it exits as that change.
73
74
  * If an edit enters and exits as edits only, it exits as a single edit.
74
75
  */
75
- function* pushAccumulatedChanges(accumulatedPushes, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel) {
76
- if (accumulatedPushes.length === 0) return;
76
+ function pushAccumulatedChanges(accumulatedPushes, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel) {
77
+ if (accumulatedPushes.length === 0) return emptyArray;
77
78
  const witnessesLeft = [];
78
79
  const witnessesEntered = [];
79
80
  const candidatesToPush = /* @__PURE__ */ new Map();
@@ -90,11 +91,16 @@ function* pushAccumulatedChanges(accumulatedPushes, output, pusher, fanOutChange
90
91
  }
91
92
  accumulatedPushes.length = 0;
92
93
  assert(candidatesToPush.size > 0, "Fan-in: witness changes without a change for their row");
93
- for (const change of witnessesLeft) yield* output.push(change, pusher);
94
- yield* pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel);
95
- for (const change of witnessesEntered) yield* output.push(change, pusher);
94
+ const collapsed = () => pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel);
95
+ if (witnessesLeft.length === 0 && witnessesEntered.length === 0) return collapsed();
96
+ return andThen(pushAll(witnessesLeft, output, pusher), () => andThen(collapsed(), () => pushAll(witnessesEntered, output, pusher)));
96
97
  }
97
- function* pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel) {
98
+ /** Pushes `changes`, one after the other. */
99
+ function pushAll(changes, output, pusher) {
100
+ let i = 0;
101
+ return eachStream(() => i === changes.length ? void 0 : output.push(changes[i++], pusher));
102
+ }
103
+ function pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, mergeRelationships, addEmptyRelationships, isBranchRel) {
98
104
  const types = [...candidatesToPush.keys()];
99
105
  /**
100
106
  * Based on the received `fanOutChangeType` only certain output types are valid.
@@ -109,39 +115,30 @@ function* pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, merg
109
115
  switch (fanOutChangeType) {
110
116
  case 1:
111
117
  assert(types.length === 1 && types[0] === 1, "Fan-in:remove expected all removes");
112
- yield* output.push(addEmptyRelationships(must(candidatesToPush.get(1))), pusher);
113
- return;
118
+ return output.push(addEmptyRelationships(must(candidatesToPush.get(1))), pusher);
114
119
  case 0:
115
120
  assert(types.length === 1 && types[0] === 0, "Fan-in:add expected all adds");
116
- yield* output.push(addEmptyRelationships(must(candidatesToPush.get(0))), pusher);
117
- return;
121
+ return output.push(addEmptyRelationships(must(candidatesToPush.get(0))), pusher);
118
122
  case 2: {
119
123
  assert(types.every((type) => type === 0 || type === 1 || type === 2), "Fan-in:edit expected all adds, removes, or edits");
120
124
  const addChange = candidatesToPush.get(0);
121
125
  const removeChange = candidatesToPush.get(1);
122
126
  let editChange = candidatesToPush.get(2);
123
127
  if (isBranchRel && (editChange || addChange && removeChange)) {
124
- if (removeChange) yield* pushWitnessDeltas(removeChange[1], 1, isBranchRel, output, pusher);
125
128
  const strip = (n) => ({
126
129
  row: n.row,
127
130
  relationships: Object.fromEntries(Object.entries(n.relationships).filter(([k]) => !isBranchRel(k)))
128
131
  });
129
- yield* output.push(addEmptyRelationships(editChange ?? makeEditChange(strip(must(addChange)[1]), strip(must(removeChange)[1]))), pusher);
130
- if (addChange) yield* pushWitnessDeltas(addChange[1], 0, isBranchRel, output, pusher);
131
- return;
132
+ const edit = editChange;
133
+ return andThen(removeChange ? pushWitnessDeltas(removeChange[1], 1, isBranchRel, output, pusher) : emptyArray, () => andThen(output.push(addEmptyRelationships(edit ?? makeEditChange(strip(must(addChange)[1]), strip(must(removeChange)[1]))), pusher), () => addChange ? pushWitnessDeltas(addChange[1], 0, isBranchRel, output, pusher) : emptyArray));
132
134
  }
133
135
  if (editChange) {
134
136
  if (addChange) editChange = mergeRelationships(editChange, addChange);
135
137
  if (removeChange) editChange = mergeRelationships(editChange, removeChange);
136
- yield* output.push(addEmptyRelationships(editChange), pusher);
137
- return;
138
- }
139
- if (addChange && removeChange) {
140
- yield* output.push(addEmptyRelationships(makeEditChange(addChange[1], removeChange[1])), pusher);
141
- return;
138
+ return output.push(addEmptyRelationships(editChange), pusher);
142
139
  }
143
- yield* output.push(addEmptyRelationships(must(addChange ?? removeChange)), pusher);
144
- return;
140
+ if (addChange && removeChange) return output.push(addEmptyRelationships(makeEditChange(addChange[1], removeChange[1])), pusher);
141
+ return output.push(addEmptyRelationships(must(addChange ?? removeChange)), pusher);
145
142
  }
146
143
  case 3: {
147
144
  assert(types.every((type) => type === 0 || type === 1 || type === 3), "Fan-in:child expected all adds, removes, or children");
@@ -150,18 +147,16 @@ function* pushCollapsed(candidatesToPush, output, pusher, fanOutChangeType, merg
150
147
  if (childChange) {
151
148
  const left = candidatesToPush.get(1);
152
149
  const entered = candidatesToPush.get(0);
153
- if (isBranchRel && left) yield* pushWitnessDeltas(left[1], 1, isBranchRel, output, pusher);
154
- yield* output.push(childChange, pusher);
155
- if (isBranchRel && entered) yield* pushWitnessDeltas(entered[1], 0, isBranchRel, output, pusher);
156
- return;
150
+ if (!isBranchRel) return output.push(childChange, pusher);
151
+ return andThen(left ? pushWitnessDeltas(left[1], 1, isBranchRel, output, pusher) : emptyArray, () => andThen(output.push(childChange, pusher), () => entered ? pushWitnessDeltas(entered[1], 0, isBranchRel, output, pusher) : emptyArray));
157
152
  }
158
153
  const addChange = candidatesToPush.get(0);
159
154
  const removeChange = candidatesToPush.get(1);
160
155
  assert(addChange === void 0 || removeChange === void 0, "Fan-in:child expected either add or remove, not both");
161
- yield* output.push(addEmptyRelationships(must(addChange ?? removeChange)), pusher);
162
- return;
156
+ return output.push(addEmptyRelationships(must(addChange ?? removeChange)), pusher);
163
157
  }
164
158
  }
159
+ return emptyArray;
165
160
  }
166
161
  /**
167
162
  * The row stays downstream, but it gained (`ADD`) or lost (`REMOVE`) every
@@ -1 +1 @@
1
- {"version":3,"file":"push-accumulated.js","names":[],"sources":["../../../../../zql/src/ivm/push-accumulated.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeChildChange,\n makeEditChange,\n makeRemoveChange,\n type Change,\n} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * # pushAccumulatedChanges\n *\n * Pushes the changes that were accumulated by\n * [fan-out, fan-in] or [ufo, ufi] sub-graphs.\n *\n * This function is called at the end of the sub-graph.\n *\n * The sub-graphs represents `OR`s.\n *\n * Changes that can enter the subgraphs:\n * 1. child (due to exist joins being above the sub-graph)\n * 2. add\n * 3. remove\n * 4. edit\n *\n * # Changes that can exit into `pushAccumulatedChanges`:\n *\n * ## Child\n * If a `child` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `child` change\n * - stop the `child` change (e.g., filter)\n * - convert it to an `add` or `remove` (e.g., exists filter)\n *\n * ## Add\n * If an `add` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `add` change\n * - hide the change (e.g., filter)\n *\n * ## Remove\n * If a `remove` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `remove` change\n * - hide the change (e.g., filter)\n *\n * ## Edit\n * If an `edit` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `edit` change\n * - convert it to an `add` (e.g., filter where old didn't match but new does)\n * - convert it to a `remove` (e.g., filter where old matched but new doesn't)\n *\n * This results in some invariants:\n * - an add coming in will only create adds coming out\n * - a remove coming in will only create removes coming out\n * - an edit coming in can create adds, removes, and edits coming out\n * - a child coming in can create adds, removes, and children coming out\n *\n * # Return of `pushAccumulatedChanges`\n *\n * This function will only push a single change.\n * Given the above invariants, how is this possible?\n *\n * An add that becomes many `adds` results in a single add\n * as the `add` is the same row across all adds. Branches do not change the row.\n *\n * A remove that becomes many `removes` results in a single remove\n * for the same reason.\n *\n * If a child enters and exits, it takes precedence over all other changes.\n * If a child enters and is converted only to add and remove it exits as an edit.\n * If a child enters and is converted to only add or only remove, it exits as that change.\n *\n * If an edit enters and is converted to add and remove it exits as an edit.\n * If an edit enters and is converted to only add or only remove, it exits as that change.\n * If an edit enters and exits as edits only, it exits as a single edit.\n */\nexport function* pushAccumulatedChanges(\n accumulatedPushes: Change[],\n output: Output,\n pusher: InputBase,\n fanOutChangeType: ChangeType,\n mergeRelationships: (existing: Change, incoming: Change) => Change,\n addEmptyRelationships: (change: Change) => Change,\n // Relationships a branch adds (a flipped join's witnesses). Only the union\n // fan has any; the filter fan passes nothing and behaves as before.\n isBranchRel?: (name: string) => boolean,\n): Stream<'yield'> {\n if (accumulatedPushes.length === 0) {\n // It is possible for no forks to pass along the push.\n // E.g., if no filters match in any fork.\n return;\n }\n\n // A union nested in a branch keeps the row but reports its witness changes\n // as child changes on its own branch relationships, around the change it\n // pushes for the row. (A child change entering through the fan-out is on\n // one of the fan-out's relationships, never a branch's.) Those are not\n // candidates to collapse: pass them through around this fan-in's change,\n // removals (old row) before and additions (new row) after, as the nested\n // union ordered them.\n const witnessesLeft: Change[] = [];\n const witnessesEntered: Change[] = [];\n\n // collapse down to a single change per type\n const candidatesToPush = new Map<ChangeType, Change>();\n for (const change of accumulatedPushes) {\n if (\n isBranchRel &&\n change[ChangeIndex.TYPE] === ChangeType.CHILD &&\n isBranchRel(change[ChangeIndex.CHILD_DATA].relationshipName)\n ) {\n (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.REMOVE\n ? witnessesLeft\n : witnessesEntered\n ).push(change);\n continue;\n }\n if (\n fanOutChangeType === ChangeType.CHILD &&\n change[ChangeIndex.TYPE] !== ChangeType.CHILD\n ) {\n assert(\n candidatesToPush.has(change[ChangeIndex.TYPE]) === false,\n () =>\n `Fan-in:child expected at most one ${change[ChangeIndex.TYPE]} when fan-out is of type child`,\n );\n }\n\n const existing = candidatesToPush.get(change[ChangeIndex.TYPE]);\n let mergedChange = change;\n if (existing) {\n // merge in relationships\n mergedChange = mergeRelationships(existing, change);\n }\n candidatesToPush.set(change[ChangeIndex.TYPE], mergedChange);\n }\n\n accumulatedPushes.length = 0;\n\n assert(\n candidatesToPush.size > 0,\n 'Fan-in: witness changes without a change for their row',\n );\n for (const change of witnessesLeft) {\n yield* output.push(change, pusher);\n }\n yield* pushCollapsed(\n candidatesToPush,\n output,\n pusher,\n fanOutChangeType,\n mergeRelationships,\n addEmptyRelationships,\n isBranchRel,\n );\n for (const change of witnessesEntered) {\n yield* output.push(change, pusher);\n }\n}\n\nfunction* pushCollapsed(\n candidatesToPush: Map<ChangeType, Change>,\n output: Output,\n pusher: InputBase,\n fanOutChangeType: ChangeType,\n mergeRelationships: (existing: Change, incoming: Change) => Change,\n addEmptyRelationships: (change: Change) => Change,\n isBranchRel: ((name: string) => boolean) | undefined,\n): Stream<'yield'> {\n const types = [...candidatesToPush.keys()];\n /**\n * Based on the received `fanOutChangeType` only certain output types are valid.\n *\n * - remove must result in all removes\n * - add must result in all adds\n * - edit must result in add or removes or edits\n * - child must result in a single add or single remove or many child changes\n * - Single add or remove because the relationship will be unique to one exist check within the fan-out,fan-in sub-graph\n * - Many child changes because other operators may preserve the child change\n */\n switch (fanOutChangeType) {\n case ChangeType.REMOVE:\n assert(\n types.length === 1 && types[0] === ChangeType.REMOVE,\n 'Fan-in:remove expected all removes',\n );\n yield* output.push(\n addEmptyRelationships(must(candidatesToPush.get(ChangeType.REMOVE))),\n pusher,\n );\n return;\n case ChangeType.ADD:\n assert(\n types.length === 1 && types[0] === ChangeType.ADD,\n 'Fan-in:add expected all adds',\n );\n yield* output.push(\n addEmptyRelationships(must(candidatesToPush.get(ChangeType.ADD))),\n pusher,\n );\n return;\n case ChangeType.EDIT: {\n assert(\n types.every(\n type =>\n type === ChangeType.ADD ||\n type === ChangeType.REMOVE ||\n type === ChangeType.EDIT,\n ),\n 'Fan-in:edit expected all adds, removes, or edits',\n );\n const addChange = candidatesToPush.get(ChangeType.ADD);\n const removeChange = candidatesToPush.get(ChangeType.REMOVE);\n let editChange = candidatesToPush.get(ChangeType.EDIT);\n\n // The row stays downstream (a branch kept the edit, or it moved from\n // one branch to another). Branches it left take their witnesses with\n // them; branches it entered bring theirs. An edit syncs one row, so\n // those must travel as child changes around it.\n if (isBranchRel && (editChange || (addChange && removeChange))) {\n if (removeChange) {\n yield* pushWitnessDeltas(\n removeChange[ChangeIndex.NODE],\n ChangeType.REMOVE,\n isBranchRel,\n output,\n pusher,\n );\n }\n const strip = (n: Node): Node => ({\n row: n.row,\n relationships: Object.fromEntries(\n Object.entries(n.relationships).filter(([k]) => !isBranchRel(k)),\n ),\n });\n yield* output.push(\n addEmptyRelationships(\n editChange ??\n makeEditChange(\n strip(must(addChange)[ChangeIndex.NODE]),\n strip(must(removeChange)[ChangeIndex.NODE]),\n ),\n ),\n pusher,\n );\n if (addChange) {\n yield* pushWitnessDeltas(\n addChange[ChangeIndex.NODE],\n ChangeType.ADD,\n isBranchRel,\n output,\n pusher,\n );\n }\n return;\n }\n\n // If an `edit` is present, it supersedes `add` and `remove`\n // as it semantically represents both.\n if (editChange) {\n if (addChange) {\n editChange = mergeRelationships(editChange, addChange);\n }\n if (removeChange) {\n editChange = mergeRelationships(editChange, removeChange);\n }\n yield* output.push(addEmptyRelationships(editChange), pusher);\n return;\n }\n\n // If `edit` didn't make it through but both `add` and `remove` did,\n // convert back to an edit.\n //\n // When can this happen?\n //\n // EDIT old: a=1, new: a=2\n // |\n // FanOut\n // / \\\n // a=1 a=2\n // | |\n // remove add\n // \\ /\n // FanIn\n //\n // The left filter converts the edit into a remove.\n // The right filter converts the edit into an add.\n if (addChange && removeChange) {\n yield* output.push(\n addEmptyRelationships(\n makeEditChange(\n addChange[ChangeIndex.NODE],\n removeChange[ChangeIndex.NODE],\n ),\n ),\n pusher,\n );\n return;\n }\n\n yield* output.push(\n addEmptyRelationships(must(addChange ?? removeChange)),\n pusher,\n );\n return;\n }\n case ChangeType.CHILD: {\n assert(\n types.every(\n type =>\n type === ChangeType.ADD || // exists can change child to add or remove\n type === ChangeType.REMOVE || // exists can change child to add or remove\n type === ChangeType.CHILD, // other operators may preserve the child change\n ),\n 'Fan-in:child expected all adds, removes, or children',\n );\n assert(\n types.length <= 2,\n 'Fan-in:child expected at most 2 types on a child change from fan-out',\n );\n\n // If any branch preserved the original child change, that takes precedence over all other changes.\n const childChange = candidatesToPush.get(ChangeType.CHILD);\n if (childChange) {\n const left = candidatesToPush.get(ChangeType.REMOVE);\n const entered = candidatesToPush.get(ChangeType.ADD);\n if (isBranchRel && left) {\n yield* pushWitnessDeltas(\n left[ChangeIndex.NODE],\n ChangeType.REMOVE,\n isBranchRel,\n output,\n pusher,\n );\n }\n yield* output.push(childChange, pusher);\n if (isBranchRel && entered) {\n yield* pushWitnessDeltas(\n entered[ChangeIndex.NODE],\n ChangeType.ADD,\n isBranchRel,\n output,\n pusher,\n );\n }\n return;\n }\n\n const addChange = candidatesToPush.get(ChangeType.ADD);\n const removeChange = candidatesToPush.get(ChangeType.REMOVE);\n\n assert(\n addChange === undefined || removeChange === undefined,\n 'Fan-in:child expected either add or remove, not both',\n );\n\n yield* output.push(\n addEmptyRelationships(must(addChange ?? removeChange)),\n pusher,\n );\n return;\n }\n default:\n fanOutChangeType satisfies never;\n }\n}\n\n/**\n * The row stays downstream, but it gained (`ADD`) or lost (`REMOVE`) every\n * witness in `node`'s branch relationships. One child change per witness.\n */\nexport function* pushWitnessDeltas(\n node: Node,\n type: ChangeType.ADD | ChangeType.REMOVE,\n isBranchRel: (name: string) => boolean,\n output: Output,\n pusher: InputBase,\n): Stream<'yield'> {\n for (const [relationshipName, children] of Object.entries(\n node.relationships,\n )) {\n if (!isBranchRel(relationshipName)) {\n continue;\n }\n for (const child of children()) {\n if (child === 'yield') {\n yield child;\n continue;\n }\n yield* output.push(\n makeChildChange(node, {\n relationshipName,\n change:\n type === ChangeType.ADD\n ? makeAddChange(child)\n : makeRemoveChange(child),\n }),\n pusher,\n );\n }\n }\n}\n\n/**\n * Puts relationships from `right` into `left` if they don't already exist in `left`.\n */\nexport function mergeRelationships(left: Change, right: Change): Change {\n // change types will always match\n // unless we have an edit on the left\n // then the right could be edit, add, or remove\n if (left[ChangeIndex.TYPE] === right[ChangeIndex.TYPE]) {\n switch (left[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n return makeAddChange({\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n });\n }\n case ChangeType.REMOVE: {\n return makeRemoveChange({\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n });\n }\n case ChangeType.EDIT: {\n assert(\n right[ChangeIndex.TYPE] === ChangeType.EDIT,\n () =>\n `mergeRelationships: when left.type is edit and types match, right.type must be edit, got ${right[ChangeIndex.TYPE]}`,\n );\n // merge edits into a single edit\n return makeEditChange(\n {\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n {\n row: left[ChangeIndex.OLD_NODE].row,\n relationships: {\n ...right[ChangeIndex.OLD_NODE].relationships,\n ...left[ChangeIndex.OLD_NODE].relationships,\n },\n },\n );\n }\n case ChangeType.CHILD: {\n // Multiple branches may preserve the same child change, each adding\n // different relationships. Merge the relationships, keeping the child\n // (which should be identical across branches).\n assert(\n right[ChangeIndex.TYPE] === ChangeType.CHILD,\n () =>\n `mergeRelationships: when left.type is child and types match, right.type must be child, got ${right[ChangeIndex.TYPE]}`,\n );\n return makeChildChange(\n {\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n left[ChangeIndex.CHILD_DATA],\n );\n }\n }\n }\n\n // left is always an edit here\n assert(\n left[ChangeIndex.TYPE] === ChangeType.EDIT,\n () =>\n `mergeRelationships: when types differ, left.type must be edit, got left.type=${left[ChangeIndex.TYPE]}, right.type=${right[ChangeIndex.TYPE]}`,\n );\n switch (right[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n return makeEditChange(\n {\n ...left[ChangeIndex.NODE],\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n left[ChangeIndex.OLD_NODE],\n );\n }\n case ChangeType.REMOVE: {\n return makeEditChange(left[ChangeIndex.NODE], {\n ...left[ChangeIndex.OLD_NODE],\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.OLD_NODE].relationships,\n },\n });\n }\n }\n\n unreachable();\n}\n\nexport function makeAddEmptyRelationships(\n schema: SourceSchema,\n): (change: Change) => Change {\n return (change: Change): Change => {\n if (Object.keys(schema.relationships).length === 0) {\n return change;\n }\n\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const relationships = {...change[ChangeIndex.NODE].relationships};\n mergeEmpty(relationships, Object.keys(schema.relationships));\n return makeAddChange({\n row: change[ChangeIndex.NODE].row,\n relationships,\n });\n }\n case ChangeType.REMOVE: {\n const relationships = {...change[ChangeIndex.NODE].relationships};\n mergeEmpty(relationships, Object.keys(schema.relationships));\n return makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships,\n });\n }\n case ChangeType.EDIT: {\n const nodeRelationships = {...change[ChangeIndex.NODE].relationships};\n const oldNodeRelationships = {\n ...change[ChangeIndex.OLD_NODE].relationships,\n };\n mergeEmpty(nodeRelationships, Object.keys(schema.relationships));\n mergeEmpty(oldNodeRelationships, Object.keys(schema.relationships));\n return makeEditChange(\n {row: change[ChangeIndex.NODE].row, relationships: nodeRelationships},\n {\n row: change[ChangeIndex.OLD_NODE].row,\n relationships: oldNodeRelationships,\n },\n );\n }\n case ChangeType.CHILD:\n return change; // children only have relationships along the path to the change\n }\n };\n}\n\n/**\n * For each relationship in `schema` that does not exist\n * in `relationships`, add it with an empty stream.\n *\n * This modifies the `relationships` object in place.\n */\nexport function mergeEmpty(\n relationships: Record<string, () => Stream<Node | 'yield'>>,\n relationshipNames: string[],\n) {\n for (const relName of relationshipNames) {\n if (relationships[relName] === undefined) {\n relationships[relName] = () => emptyArray;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsFA,UAAiB,uBACf,mBACA,QACA,QACA,kBACA,oBACA,uBAGA,aACiB;CACjB,IAAI,kBAAkB,WAAW,GAG/B;CAUF,MAAM,gBAA0B,CAAC;CACjC,MAAM,mBAA6B,CAAC;CAGpC,MAAM,mCAAmB,IAAI,IAAwB;CACrD,KAAK,MAAM,UAAU,mBAAmB;EACtC,IACE,eACA,OAAO,OAAsB,KAC7B,YAAY,OAAO,EAAuB,CAAC,gBAAgB,GAC3D;GACA,CAAC,OAAO,EAAuB,CAAC,OAAO,OACvC,IACI,gBACA,iBAAA,CACF,KAAK,MA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GACH;GACA,KAAK,GAAiB;IACpB,MAAM,oBAAoB,EAAC,GAAG,OAAO,EAAiB,CAAC,cAAa;IACpE,MAAM,uBAAuB,EAC3B,GAAG,OAAO,EAAqB,CAAC,cAClC;IACA,WAAW,mBAAmB,OAAO,KAAK,OAAO,aAAa,CAAC;IAC/D,WAAW,sBAAsB,OAAO,KAAK,OAAO,aAAa,CAAC;IAClE,OAAO,eACL;KAAC,KAAK,OAAO,EAAiB,CAAC;KAAK,eAAe;IAAiB,GACpE;KACE,KAAK,OAAO,EAAqB,CAAC;KAClC,eAAe;IACjB,CACF;GACF;GACA,KAAK,GACH,OAAO;EACX;CACF;AACF;;;;;;;AAQA,SAAgB,WACd,eACA,mBACA;CACA,KAAK,MAAM,WAAW,mBACpB,IAAI,cAAc,aAAa,KAAA,GAC7B,cAAc,iBAAiB;AAGrC"}
1
+ {"version":3,"file":"push-accumulated.js","names":[],"sources":["../../../../../zql/src/ivm/push-accumulated.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeChildChange,\n makeEditChange,\n makeRemoveChange,\n type Change,\n} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {andThen, eachStream, type Stream} from './stream.ts';\n\n/**\n * # pushAccumulatedChanges\n *\n * Pushes the changes that were accumulated by\n * [fan-out, fan-in] or [ufo, ufi] sub-graphs.\n *\n * This function is called at the end of the sub-graph.\n *\n * The sub-graphs represents `OR`s.\n *\n * Changes that can enter the subgraphs:\n * 1. child (due to exist joins being above the sub-graph)\n * 2. add\n * 3. remove\n * 4. edit\n *\n * # Changes that can exit into `pushAccumulatedChanges`:\n *\n * ## Child\n * If a `child` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `child` change\n * - stop the `child` change (e.g., filter)\n * - convert it to an `add` or `remove` (e.g., exists filter)\n *\n * ## Add\n * If an `add` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `add` change\n * - hide the change (e.g., filter)\n *\n * ## Remove\n * If a `remove` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `remove` change\n * - hide the change (e.g., filter)\n *\n * ## Edit\n * If an `edit` change enters a sub-graph, it will flow to all branches.\n * Each branch will either:\n * - preserve the `edit` change\n * - convert it to an `add` (e.g., filter where old didn't match but new does)\n * - convert it to a `remove` (e.g., filter where old matched but new doesn't)\n *\n * This results in some invariants:\n * - an add coming in will only create adds coming out\n * - a remove coming in will only create removes coming out\n * - an edit coming in can create adds, removes, and edits coming out\n * - a child coming in can create adds, removes, and children coming out\n *\n * # Return of `pushAccumulatedChanges`\n *\n * This function will only push a single change.\n * Given the above invariants, how is this possible?\n *\n * An add that becomes many `adds` results in a single add\n * as the `add` is the same row across all adds. Branches do not change the row.\n *\n * A remove that becomes many `removes` results in a single remove\n * for the same reason.\n *\n * If a child enters and exits, it takes precedence over all other changes.\n * If a child enters and is converted only to add and remove it exits as an edit.\n * If a child enters and is converted to only add or only remove, it exits as that change.\n *\n * If an edit enters and is converted to add and remove it exits as an edit.\n * If an edit enters and is converted to only add or only remove, it exits as that change.\n * If an edit enters and exits as edits only, it exits as a single edit.\n */\nexport function pushAccumulatedChanges(\n accumulatedPushes: Change[],\n output: Output,\n pusher: InputBase,\n fanOutChangeType: ChangeType,\n mergeRelationships: (existing: Change, incoming: Change) => Change,\n addEmptyRelationships: (change: Change) => Change,\n // Relationships a branch adds (a flipped join's witnesses). Only the union\n // fan has any; the filter fan passes nothing and behaves as before.\n isBranchRel?: (name: string) => boolean,\n): Stream<'yield'> {\n if (accumulatedPushes.length === 0) {\n // It is possible for no forks to pass along the push.\n // E.g., if no filters match in any fork.\n return emptyArray;\n }\n\n // A union nested in a branch keeps the row but reports its witness changes\n // as child changes on its own branch relationships, around the change it\n // pushes for the row. (A child change entering through the fan-out is on\n // one of the fan-out's relationships, never a branch's.) Those are not\n // candidates to collapse: pass them through around this fan-in's change,\n // removals (old row) before and additions (new row) after, as the nested\n // union ordered them.\n const witnessesLeft: Change[] = [];\n const witnessesEntered: Change[] = [];\n\n // collapse down to a single change per type\n const candidatesToPush = new Map<ChangeType, Change>();\n for (const change of accumulatedPushes) {\n if (\n isBranchRel &&\n change[ChangeIndex.TYPE] === ChangeType.CHILD &&\n isBranchRel(change[ChangeIndex.CHILD_DATA].relationshipName)\n ) {\n (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.REMOVE\n ? witnessesLeft\n : witnessesEntered\n ).push(change);\n continue;\n }\n if (\n fanOutChangeType === ChangeType.CHILD &&\n change[ChangeIndex.TYPE] !== ChangeType.CHILD\n ) {\n assert(\n candidatesToPush.has(change[ChangeIndex.TYPE]) === false,\n () =>\n `Fan-in:child expected at most one ${change[ChangeIndex.TYPE]} when fan-out is of type child`,\n );\n }\n\n const existing = candidatesToPush.get(change[ChangeIndex.TYPE]);\n let mergedChange = change;\n if (existing) {\n // merge in relationships\n mergedChange = mergeRelationships(existing, change);\n }\n candidatesToPush.set(change[ChangeIndex.TYPE], mergedChange);\n }\n\n accumulatedPushes.length = 0;\n\n assert(\n candidatesToPush.size > 0,\n 'Fan-in: witness changes without a change for their row',\n );\n const collapsed = () =>\n pushCollapsed(\n candidatesToPush,\n output,\n pusher,\n fanOutChangeType,\n mergeRelationships,\n addEmptyRelationships,\n isBranchRel,\n );\n if (witnessesLeft.length === 0 && witnessesEntered.length === 0) {\n return collapsed();\n }\n return andThen(pushAll(witnessesLeft, output, pusher), () =>\n andThen(collapsed(), () => pushAll(witnessesEntered, output, pusher)),\n );\n}\n\n/** Pushes `changes`, one after the other. */\nfunction pushAll(\n changes: readonly Change[],\n output: Output,\n pusher: InputBase,\n): Stream<'yield'> {\n let i = 0;\n return eachStream(() =>\n i === changes.length ? undefined : output.push(changes[i++], pusher),\n );\n}\n\nfunction pushCollapsed(\n candidatesToPush: Map<ChangeType, Change>,\n output: Output,\n pusher: InputBase,\n fanOutChangeType: ChangeType,\n mergeRelationships: (existing: Change, incoming: Change) => Change,\n addEmptyRelationships: (change: Change) => Change,\n isBranchRel: ((name: string) => boolean) | undefined,\n): Stream<'yield'> {\n const types = [...candidatesToPush.keys()];\n /**\n * Based on the received `fanOutChangeType` only certain output types are valid.\n *\n * - remove must result in all removes\n * - add must result in all adds\n * - edit must result in add or removes or edits\n * - child must result in a single add or single remove or many child changes\n * - Single add or remove because the relationship will be unique to one exist check within the fan-out,fan-in sub-graph\n * - Many child changes because other operators may preserve the child change\n */\n switch (fanOutChangeType) {\n case ChangeType.REMOVE:\n assert(\n types.length === 1 && types[0] === ChangeType.REMOVE,\n 'Fan-in:remove expected all removes',\n );\n return output.push(\n addEmptyRelationships(must(candidatesToPush.get(ChangeType.REMOVE))),\n pusher,\n );\n case ChangeType.ADD:\n assert(\n types.length === 1 && types[0] === ChangeType.ADD,\n 'Fan-in:add expected all adds',\n );\n return output.push(\n addEmptyRelationships(must(candidatesToPush.get(ChangeType.ADD))),\n pusher,\n );\n case ChangeType.EDIT: {\n assert(\n types.every(\n type =>\n type === ChangeType.ADD ||\n type === ChangeType.REMOVE ||\n type === ChangeType.EDIT,\n ),\n 'Fan-in:edit expected all adds, removes, or edits',\n );\n const addChange = candidatesToPush.get(ChangeType.ADD);\n const removeChange = candidatesToPush.get(ChangeType.REMOVE);\n let editChange = candidatesToPush.get(ChangeType.EDIT);\n\n // The row stays downstream (a branch kept the edit, or it moved from\n // one branch to another). Branches it left take their witnesses with\n // them; branches it entered bring theirs. An edit syncs one row, so\n // those must travel as child changes around it.\n if (isBranchRel && (editChange || (addChange && removeChange))) {\n const strip = (n: Node): Node => ({\n row: n.row,\n relationships: Object.fromEntries(\n Object.entries(n.relationships).filter(([k]) => !isBranchRel(k)),\n ),\n });\n const edit = editChange;\n return andThen(\n removeChange\n ? pushWitnessDeltas(\n removeChange[ChangeIndex.NODE],\n ChangeType.REMOVE,\n isBranchRel,\n output,\n pusher,\n )\n : emptyArray,\n () =>\n andThen(\n output.push(\n addEmptyRelationships(\n edit ??\n makeEditChange(\n strip(must(addChange)[ChangeIndex.NODE]),\n strip(must(removeChange)[ChangeIndex.NODE]),\n ),\n ),\n pusher,\n ),\n () =>\n addChange\n ? pushWitnessDeltas(\n addChange[ChangeIndex.NODE],\n ChangeType.ADD,\n isBranchRel,\n output,\n pusher,\n )\n : emptyArray,\n ),\n );\n }\n\n // If an `edit` is present, it supersedes `add` and `remove`\n // as it semantically represents both.\n if (editChange) {\n if (addChange) {\n editChange = mergeRelationships(editChange, addChange);\n }\n if (removeChange) {\n editChange = mergeRelationships(editChange, removeChange);\n }\n return output.push(addEmptyRelationships(editChange), pusher);\n }\n\n // If `edit` didn't make it through but both `add` and `remove` did,\n // convert back to an edit.\n //\n // When can this happen?\n //\n // EDIT old: a=1, new: a=2\n // |\n // FanOut\n // / \\\n // a=1 a=2\n // | |\n // remove add\n // \\ /\n // FanIn\n //\n // The left filter converts the edit into a remove.\n // The right filter converts the edit into an add.\n if (addChange && removeChange) {\n return output.push(\n addEmptyRelationships(\n makeEditChange(\n addChange[ChangeIndex.NODE],\n removeChange[ChangeIndex.NODE],\n ),\n ),\n pusher,\n );\n }\n\n return output.push(\n addEmptyRelationships(must(addChange ?? removeChange)),\n pusher,\n );\n }\n case ChangeType.CHILD: {\n assert(\n types.every(\n type =>\n type === ChangeType.ADD || // exists can change child to add or remove\n type === ChangeType.REMOVE || // exists can change child to add or remove\n type === ChangeType.CHILD, // other operators may preserve the child change\n ),\n 'Fan-in:child expected all adds, removes, or children',\n );\n assert(\n types.length <= 2,\n 'Fan-in:child expected at most 2 types on a child change from fan-out',\n );\n\n // If any branch preserved the original child change, that takes precedence over all other changes.\n const childChange = candidatesToPush.get(ChangeType.CHILD);\n if (childChange) {\n const left = candidatesToPush.get(ChangeType.REMOVE);\n const entered = candidatesToPush.get(ChangeType.ADD);\n if (!isBranchRel) {\n return output.push(childChange, pusher);\n }\n return andThen(\n left\n ? pushWitnessDeltas(\n left[ChangeIndex.NODE],\n ChangeType.REMOVE,\n isBranchRel,\n output,\n pusher,\n )\n : emptyArray,\n () =>\n andThen(output.push(childChange, pusher), () =>\n entered\n ? pushWitnessDeltas(\n entered[ChangeIndex.NODE],\n ChangeType.ADD,\n isBranchRel,\n output,\n pusher,\n )\n : emptyArray,\n ),\n );\n }\n\n const addChange = candidatesToPush.get(ChangeType.ADD);\n const removeChange = candidatesToPush.get(ChangeType.REMOVE);\n\n assert(\n addChange === undefined || removeChange === undefined,\n 'Fan-in:child expected either add or remove, not both',\n );\n\n return output.push(\n addEmptyRelationships(must(addChange ?? removeChange)),\n pusher,\n );\n }\n default:\n fanOutChangeType satisfies never;\n }\n return emptyArray;\n}\n\n/**\n * The row stays downstream, but it gained (`ADD`) or lost (`REMOVE`) every\n * witness in `node`'s branch relationships. One child change per witness.\n */\nexport function* pushWitnessDeltas(\n node: Node,\n type: ChangeType.ADD | ChangeType.REMOVE,\n isBranchRel: (name: string) => boolean,\n output: Output,\n pusher: InputBase,\n): Stream<'yield'> {\n for (const [relationshipName, children] of Object.entries(\n node.relationships,\n )) {\n if (!isBranchRel(relationshipName)) {\n continue;\n }\n for (const child of children()) {\n if (child === 'yield') {\n yield child;\n continue;\n }\n yield* output.push(\n makeChildChange(node, {\n relationshipName,\n change:\n type === ChangeType.ADD\n ? makeAddChange(child)\n : makeRemoveChange(child),\n }),\n pusher,\n );\n }\n }\n}\n\n/**\n * Puts relationships from `right` into `left` if they don't already exist in `left`.\n */\nexport function mergeRelationships(left: Change, right: Change): Change {\n // change types will always match\n // unless we have an edit on the left\n // then the right could be edit, add, or remove\n if (left[ChangeIndex.TYPE] === right[ChangeIndex.TYPE]) {\n switch (left[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n return makeAddChange({\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n });\n }\n case ChangeType.REMOVE: {\n return makeRemoveChange({\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n });\n }\n case ChangeType.EDIT: {\n assert(\n right[ChangeIndex.TYPE] === ChangeType.EDIT,\n () =>\n `mergeRelationships: when left.type is edit and types match, right.type must be edit, got ${right[ChangeIndex.TYPE]}`,\n );\n // merge edits into a single edit\n return makeEditChange(\n {\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n {\n row: left[ChangeIndex.OLD_NODE].row,\n relationships: {\n ...right[ChangeIndex.OLD_NODE].relationships,\n ...left[ChangeIndex.OLD_NODE].relationships,\n },\n },\n );\n }\n case ChangeType.CHILD: {\n // Multiple branches may preserve the same child change, each adding\n // different relationships. Merge the relationships, keeping the child\n // (which should be identical across branches).\n assert(\n right[ChangeIndex.TYPE] === ChangeType.CHILD,\n () =>\n `mergeRelationships: when left.type is child and types match, right.type must be child, got ${right[ChangeIndex.TYPE]}`,\n );\n return makeChildChange(\n {\n row: left[ChangeIndex.NODE].row,\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n left[ChangeIndex.CHILD_DATA],\n );\n }\n }\n }\n\n // left is always an edit here\n assert(\n left[ChangeIndex.TYPE] === ChangeType.EDIT,\n () =>\n `mergeRelationships: when types differ, left.type must be edit, got left.type=${left[ChangeIndex.TYPE]}, right.type=${right[ChangeIndex.TYPE]}`,\n );\n switch (right[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n return makeEditChange(\n {\n ...left[ChangeIndex.NODE],\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.NODE].relationships,\n },\n },\n left[ChangeIndex.OLD_NODE],\n );\n }\n case ChangeType.REMOVE: {\n return makeEditChange(left[ChangeIndex.NODE], {\n ...left[ChangeIndex.OLD_NODE],\n relationships: {\n ...right[ChangeIndex.NODE].relationships,\n ...left[ChangeIndex.OLD_NODE].relationships,\n },\n });\n }\n }\n\n unreachable();\n}\n\nexport function makeAddEmptyRelationships(\n schema: SourceSchema,\n): (change: Change) => Change {\n return (change: Change): Change => {\n if (Object.keys(schema.relationships).length === 0) {\n return change;\n }\n\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const relationships = {...change[ChangeIndex.NODE].relationships};\n mergeEmpty(relationships, Object.keys(schema.relationships));\n return makeAddChange({\n row: change[ChangeIndex.NODE].row,\n relationships,\n });\n }\n case ChangeType.REMOVE: {\n const relationships = {...change[ChangeIndex.NODE].relationships};\n mergeEmpty(relationships, Object.keys(schema.relationships));\n return makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships,\n });\n }\n case ChangeType.EDIT: {\n const nodeRelationships = {...change[ChangeIndex.NODE].relationships};\n const oldNodeRelationships = {\n ...change[ChangeIndex.OLD_NODE].relationships,\n };\n mergeEmpty(nodeRelationships, Object.keys(schema.relationships));\n mergeEmpty(oldNodeRelationships, Object.keys(schema.relationships));\n return makeEditChange(\n {row: change[ChangeIndex.NODE].row, relationships: nodeRelationships},\n {\n row: change[ChangeIndex.OLD_NODE].row,\n relationships: oldNodeRelationships,\n },\n );\n }\n case ChangeType.CHILD:\n return change; // children only have relationships along the path to the change\n }\n };\n}\n\n/**\n * For each relationship in `schema` that does not exist\n * in `relationships`, add it with an empty stream.\n *\n * This modifies the `relationships` object in place.\n */\nexport function mergeEmpty(\n relationships: Record<string, () => Stream<Node | 'yield'>>,\n relationshipNames: string[],\n) {\n for (const relName of relationshipNames) {\n if (relationships[relName] === undefined) {\n relationships[relName] = () => emptyArray;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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EhD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,MAAM,EAEZ,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,EAAE,GAAG,CAAC;IACT,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;;GAGG;AACH,qBAAa,IAAK,YAAW,QAAQ;;IAOnC,YAAY,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAOrC;IAED,SAAS,IAAI,YAAY,CAExB;IAEA,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAoBhD;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,OAAO,IAAI,IAAI,CAEd;IAOD,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAepC;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAE7C;CA8DF"}
@@ -1,5 +1,6 @@
1
1
  import { assert } from "../../../shared/src/asserts.js";
2
2
  import { throwOutput } from "./operator.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
3
4
  import { maybeSplitAndPushEditChange } from "./maybe-split-and-push-edit-change.js";
4
5
  //#region ../zql/src/ivm/skip.ts
5
6
  /**
@@ -52,16 +53,12 @@ var Skip = class {
52
53
  const cmp = this.#comparator(this.#bound.row, row);
53
54
  return cmp < 0 || cmp === 0 && !this.#bound.exclusive;
54
55
  }
55
- *push(change) {
56
- const shouldBePresent = (row) => this.#shouldBePresent(row);
57
- if (change[0] === 2) {
58
- yield* maybeSplitAndPushEditChange(change, shouldBePresent, this.#output, this);
59
- return;
60
- }
61
- if (shouldBePresent(change[1].row)) yield* this.#output.push(change, this);
56
+ push(change) {
57
+ if (change[0] === 2) return maybeSplitAndPushEditChange(change, (row) => this.#shouldBePresent(row), this.#output, this);
58
+ return this.#shouldBePresent(change[1].row) ? this.#output.push(change, this) : emptyArray;
62
59
  }
63
- *reconcile(_pusher) {
64
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
60
+ reconcile(_pusher) {
61
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
65
62
  }
66
63
  #getStart(req) {
67
64
  const boundStart = {
@@ -1 +1 @@
1
- {"version":3,"file":"skip.js","names":[],"sources":["../../../../../zql/src/ivm/skip.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n type AddChange,\n type Change,\n type ChildChange,\n type RemoveChange,\n} from './change.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {maybeSplitAndPushEditChange} from './maybe-split-and-push-edit-change.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Start,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport type Bound = {\n row: Row;\n exclusive: boolean;\n};\n\n/**\n * Skip sets the start position for the pipeline. No rows before the bound will\n * be output.\n */\nexport class Skip implements Operator {\n readonly #input: Input;\n readonly #bound: Bound;\n readonly #comparator: Comparator;\n\n #output: Output = throwOutput;\n\n constructor(input: Input, bound: Bound) {\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Skip requires sorted input');\n this.#input = input;\n this.#bound = bound;\n this.#comparator = input.getSchema().compareRows;\n input.setOutput(this);\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const start = this.#getStart(req);\n if (start === 'empty') {\n return;\n }\n const nodes = this.#input.fetch({...req, start});\n if (!req.reverse) {\n yield* nodes;\n return;\n }\n for (const node of nodes) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (!this.#shouldBePresent(node.row)) {\n return;\n }\n yield node;\n }\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n #shouldBePresent(row: Row): boolean {\n const cmp = this.#comparator(this.#bound.row, row);\n return cmp < 0 || (cmp === 0 && !this.#bound.exclusive);\n }\n\n *push(change: Change): Stream<'yield'> {\n const shouldBePresent = (row: Row) => this.#shouldBePresent(row);\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* maybeSplitAndPushEditChange(\n change,\n shouldBePresent,\n this.#output,\n this,\n );\n return;\n }\n\n change satisfies AddChange | RemoveChange | ChildChange;\n\n if (shouldBePresent(change[ChangeIndex.NODE].row)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n #getStart(req: FetchRequest): Start | undefined | 'empty' {\n const boundStart = {\n row: this.#bound.row,\n basis: this.#bound.exclusive ? 'after' : 'at',\n } as const;\n\n if (!req.start) {\n if (req.reverse) {\n return undefined;\n }\n return boundStart;\n }\n\n const cmp = this.#comparator(this.#bound.row, req.start.row);\n\n if (!req.reverse) {\n // The skip bound is after the requested bound. The requested bound cannot\n // be relevant because even if it was basis: 'after', the skip bound is\n // itself after the requested bound. Return the skip bound.\n if (cmp > 0) {\n return boundStart;\n }\n\n // The skip bound and requested bound are equal. If either is exclusive,\n // return that bound with exclusive. Otherwise, return the skip bound.\n if (cmp === 0) {\n if (this.#bound.exclusive || req.start.basis === 'after') {\n return {\n row: this.#bound.row,\n basis: 'after',\n };\n }\n return boundStart;\n }\n\n return req.start;\n }\n\n req.reverse satisfies true;\n\n // bound is after the start, but request is for reverse so results\n // must be empty\n if (cmp > 0) {\n return 'empty';\n }\n\n if (cmp === 0) {\n // if both are inclusive, the result can be the single row at bound\n // return it as start\n if (!this.#bound.exclusive && req.start.basis === 'at') {\n return boundStart;\n }\n // otherwise the results must be empty, one or both are exclusive\n // in opposite directions\n return 'empty';\n }\n\n // bound is before the start, return start\n return req.start;\n }\n}\n"],"mappings":";;;;;;;;AAiCA,IAAa,OAAb,MAAsC;CACpC;CACA;CACA;CAEA,UAAkB;CAElB,YAAY,OAAc,OAAc;EACtC,MAAM,EAAC,SAAQ,MAAM,UAAU;EAC/B,OAAO,SAAS,KAAA,GAAW,4BAA4B;EACvD,KAAK,SAAS;EACd,KAAK,SAAS;EACd,KAAK,cAAc,MAAM,UAAU,CAAC,CAAC;EACrC,MAAM,UAAU,IAAI;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,CAAC,MAAM,KAA2C;EAChD,MAAM,QAAQ,KAAK,UAAU,GAAG;EAChC,IAAI,UAAU,SACZ;EAEF,MAAM,QAAQ,KAAK,OAAO,MAAM;GAAC,GAAG;GAAK;EAAK,CAAC;EAC/C,IAAI,CAAC,IAAI,SAAS;GAChB,OAAO;GACP;EACF;EACA,KAAK,MAAM,QAAQ,OAAO;GACxB,IAAI,SAAS,SAAS;IACpB,MAAM;IACN;GACF;GACA,IAAI,CAAC,KAAK,iBAAiB,KAAK,GAAG,GACjC;GAEF,MAAM;EACR;CACF;CAEA,UAAU,QAAsB;EAC9B,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,iBAAiB,KAAmB;EAClC,MAAM,MAAM,KAAK,YAAY,KAAK,OAAO,KAAK,GAAG;EACjD,OAAO,MAAM,KAAM,QAAQ,KAAK,CAAC,KAAK,OAAO;CAC/C;CAEA,CAAC,KAAK,QAAiC;EACrC,MAAM,mBAAmB,QAAa,KAAK,iBAAiB,GAAG;EAC/D,IAAI,OAAO,OAAsB,GAAiB;GAChD,OAAO,4BACL,QACA,iBACA,KAAK,SACL,IACF;GACA;EACF;EAIA,IAAI,gBAAgB,OAAO,EAAiB,CAAC,GAAG,GAC9C,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CAEzC;CAEA,CAAC,UAAU,SAAqC;EAC9C,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;CAEA,UAAU,KAAgD;EACxD,MAAM,aAAa;GACjB,KAAK,KAAK,OAAO;GACjB,OAAO,KAAK,OAAO,YAAY,UAAU;EAC3C;EAEA,IAAI,CAAC,IAAI,OAAO;GACd,IAAI,IAAI,SACN;GAEF,OAAO;EACT;EAEA,MAAM,MAAM,KAAK,YAAY,KAAK,OAAO,KAAK,IAAI,MAAM,GAAG;EAE3D,IAAI,CAAC,IAAI,SAAS;GAIhB,IAAI,MAAM,GACR,OAAO;GAKT,IAAI,QAAQ,GAAG;IACb,IAAI,KAAK,OAAO,aAAa,IAAI,MAAM,UAAU,SAC/C,OAAO;KACL,KAAK,KAAK,OAAO;KACjB,OAAO;IACT;IAEF,OAAO;GACT;GAEA,OAAO,IAAI;EACb;EAEA,IAAI;EAIJ,IAAI,MAAM,GACR,OAAO;EAGT,IAAI,QAAQ,GAAG;GAGb,IAAI,CAAC,KAAK,OAAO,aAAa,IAAI,MAAM,UAAU,MAChD,OAAO;GAIT,OAAO;EACT;EAGA,OAAO,IAAI;CACb;AACF"}
1
+ {"version":3,"file":"skip.js","names":[],"sources":["../../../../../zql/src/ivm/skip.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 {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n type AddChange,\n type Change,\n type ChildChange,\n type RemoveChange,\n} from './change.ts';\nimport type {Comparator, Node} from './data.ts';\nimport {maybeSplitAndPushEditChange} from './maybe-split-and-push-edit-change.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Start,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport type Bound = {\n row: Row;\n exclusive: boolean;\n};\n\n/**\n * Skip sets the start position for the pipeline. No rows before the bound will\n * be output.\n */\nexport class Skip implements Operator {\n readonly #input: Input;\n readonly #bound: Bound;\n readonly #comparator: Comparator;\n\n #output: Output = throwOutput;\n\n constructor(input: Input, bound: Bound) {\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Skip requires sorted input');\n this.#input = input;\n this.#bound = bound;\n this.#comparator = input.getSchema().compareRows;\n input.setOutput(this);\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const start = this.#getStart(req);\n if (start === 'empty') {\n return;\n }\n const nodes = this.#input.fetch({...req, start});\n if (!req.reverse) {\n yield* nodes;\n return;\n }\n for (const node of nodes) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (!this.#shouldBePresent(node.row)) {\n return;\n }\n yield node;\n }\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n #shouldBePresent(row: Row): boolean {\n const cmp = this.#comparator(this.#bound.row, row);\n return cmp < 0 || (cmp === 0 && !this.#bound.exclusive);\n }\n\n push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n return maybeSplitAndPushEditChange(\n change,\n row => this.#shouldBePresent(row),\n this.#output,\n this,\n );\n }\n\n change satisfies AddChange | RemoveChange | ChildChange;\n\n return this.#shouldBePresent(change[ChangeIndex.NODE].row)\n ? this.#output.push(change, this)\n : emptyArray;\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n #getStart(req: FetchRequest): Start | undefined | 'empty' {\n const boundStart = {\n row: this.#bound.row,\n basis: this.#bound.exclusive ? 'after' : 'at',\n } as const;\n\n if (!req.start) {\n if (req.reverse) {\n return undefined;\n }\n return boundStart;\n }\n\n const cmp = this.#comparator(this.#bound.row, req.start.row);\n\n if (!req.reverse) {\n // The skip bound is after the requested bound. The requested bound cannot\n // be relevant because even if it was basis: 'after', the skip bound is\n // itself after the requested bound. Return the skip bound.\n if (cmp > 0) {\n return boundStart;\n }\n\n // The skip bound and requested bound are equal. If either is exclusive,\n // return that bound with exclusive. Otherwise, return the skip bound.\n if (cmp === 0) {\n if (this.#bound.exclusive || req.start.basis === 'after') {\n return {\n row: this.#bound.row,\n basis: 'after',\n };\n }\n return boundStart;\n }\n\n return req.start;\n }\n\n req.reverse satisfies true;\n\n // bound is after the start, but request is for reverse so results\n // must be empty\n if (cmp > 0) {\n return 'empty';\n }\n\n if (cmp === 0) {\n // if both are inclusive, the result can be the single row at bound\n // return it as start\n if (!this.#bound.exclusive && req.start.basis === 'at') {\n return boundStart;\n }\n // otherwise the results must be empty, one or both are exclusive\n // in opposite directions\n return 'empty';\n }\n\n // bound is before the start, return start\n return req.start;\n }\n}\n"],"mappings":";;;;;;;;;AAkCA,IAAa,OAAb,MAAsC;CACpC;CACA;CACA;CAEA,UAAkB;CAElB,YAAY,OAAc,OAAc;EACtC,MAAM,EAAC,SAAQ,MAAM,UAAU;EAC/B,OAAO,SAAS,KAAA,GAAW,4BAA4B;EACvD,KAAK,SAAS;EACd,KAAK,SAAS;EACd,KAAK,cAAc,MAAM,UAAU,CAAC,CAAC;EACrC,MAAM,UAAU,IAAI;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,CAAC,MAAM,KAA2C;EAChD,MAAM,QAAQ,KAAK,UAAU,GAAG;EAChC,IAAI,UAAU,SACZ;EAEF,MAAM,QAAQ,KAAK,OAAO,MAAM;GAAC,GAAG;GAAK;EAAK,CAAC;EAC/C,IAAI,CAAC,IAAI,SAAS;GAChB,OAAO;GACP;EACF;EAC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AK,OAAO,aAAa,IAAI,MAAM,UAAU,MAChD,OAAO;GAIT,OAAO;EACT;EAGA,OAAO,IAAI;CACb;AACF"}
@@ -1,3 +1,4 @@
1
+ import type { Node } from './data.ts';
1
2
  /**
2
3
  * streams are lazy forward-only iterables.
3
4
  * Once a stream reaches the end it can't be restarted.
@@ -8,4 +9,61 @@
8
9
  export type Stream<T> = Iterable<T>;
9
10
  export declare function take<T>(stream: Stream<T>, limit: number): Stream<T>;
10
11
  export declare function consume<T>(stream: Stream<T>): void;
12
+ /**
13
+ * `stream`, then the stream `rest` returns.
14
+ *
15
+ * A push through a chain of generators costs a generator per operator, though
16
+ * a push rarely yields. So an operator does its push right away and returns
17
+ * `emptyArray` when all of it is done. It returns a stream only when a push it
18
+ * makes downstream, or a fetch it reads, yields. That stream holds the yields
19
+ * and, behind them, the rest of the operator's work.
20
+ *
21
+ * So nothing may follow a downstream push directly. What follows it goes in
22
+ * `rest`:
23
+ *
24
+ * return andThen(output.push(a, this), () => output.push(b, this));
25
+ *
26
+ * `rest` runs right away when the first push is done, and otherwise once the
27
+ * stream of the first push has been read. Only `emptyArray` says that a push
28
+ * is done: any other stream is read first, an empty one too, which is correct
29
+ * and only slower.
30
+ *
31
+ * The stream a push returns must be read: by the time it is returned, the
32
+ * operator may have done part of its work and hold state that only the rest
33
+ * of the stream clears.
34
+ *
35
+ * This is for a fixed number of steps. A loop goes through `eachStream`.
36
+ */
37
+ export declare function andThen(stream: Stream<'yield'>, rest: () => Stream<'yield'>): Stream<'yield'>;
38
+ /**
39
+ * Runs `cleanup` once `stream` is done, however it ends: `try { yield* stream
40
+ * } finally { cleanup() }` without the generator when there is nothing to
41
+ * wait for. The caller runs `cleanup` itself if getting the stream throws.
42
+ */
43
+ export declare function andFinally(stream: Stream<'yield'>, cleanup: () => void): Stream<'yield'>;
44
+ /**
45
+ * The streams `next` returns, one after the other, until it returns
46
+ * `undefined`: a loop whose body makes one push per round, without a generator
47
+ * for as long as every push is done.
48
+ *
49
+ * Written with `andThen`, such a loop would nest one generator per round that
50
+ * is not done, and a push to several thousand parents through an output that
51
+ * returns a generator would overflow the stack. Here, the first stream that is
52
+ * not done hands the rest of the loop to a single generator.
53
+ *
54
+ * `open` is an iterator that `next` reads. It is closed if the loop stops
55
+ * before `next` returns `undefined`, because `next` or a stream threw or the
56
+ * consumer stopped reading, as a `for...of` over that iterator would have
57
+ * closed it.
58
+ */
59
+ export declare function eachStream(next: () => Stream<'yield'> | undefined, open?: Iterator<unknown>): Stream<'yield'>;
60
+ /** One 'yield'. Returned for a round of `eachStream` that was reached by one. */
61
+ export declare const yieldOnce: Stream<'yield'>;
62
+ export declare const doneResult: IteratorReturnResult<undefined>;
63
+ /** Yields `fn(node)` for each node of `stream`. */
64
+ export declare function mapNodes(stream: Stream<Node | 'yield'>, fn: (node: Node) => Node): Stream<Node | 'yield'>;
65
+ /** Yields the nodes of `stream`, calling `fn` on each as it goes by. */
66
+ export declare function tapNodes(stream: Stream<Node | 'yield'>, fn: (node: Node) => void): Stream<Node | 'yield'>;
67
+ /** Yields the nodes of `stream` up to the first for which `fn` is false. */
68
+ export declare function nodesWhile(stream: Stream<Node | 'yield'>, fn: (node: Node) => boolean): Stream<Node | 'yield'>;
11
69
  //# sourceMappingURL=stream.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/stream.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC;AAEpC,wBAAiB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAWpE;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAGlD"}
1
+ {"version":3,"file":"stream.d.ts","s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