@rocicorp/zero 0.17.2025030600 → 0.17.2025030701

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 (87) hide show
  1. package/out/advanced.js +1 -1
  2. package/out/{chunk-Q6D7EOS6.js → chunk-6XQK3UTJ.js} +52 -21
  3. package/out/chunk-6XQK3UTJ.js.map +7 -0
  4. package/out/{chunk-XXZWDMNW.js → chunk-YKI43S5O.js} +189 -221
  5. package/out/chunk-YKI43S5O.js.map +7 -0
  6. package/out/replicache/src/db/rebase.d.ts +3 -2
  7. package/out/replicache/src/db/rebase.d.ts.map +1 -1
  8. package/out/replicache/src/persist/refresh.d.ts.map +1 -1
  9. package/out/replicache/src/replicache-impl.d.ts +8 -3
  10. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  11. package/out/replicache/src/replicache-options.d.ts +51 -0
  12. package/out/replicache/src/replicache-options.d.ts.map +1 -1
  13. package/out/replicache/src/subscriptions.d.ts +2 -1
  14. package/out/replicache/src/subscriptions.d.ts.map +1 -1
  15. package/out/replicache/src/sync/patch.d.ts +1 -1
  16. package/out/replicache/src/sync/patch.d.ts.map +1 -1
  17. package/out/replicache/src/sync/pull.d.ts +0 -2
  18. package/out/replicache/src/sync/pull.d.ts.map +1 -1
  19. package/out/replicache/src/transactions.d.ts +4 -1
  20. package/out/replicache/src/transactions.d.ts.map +1 -1
  21. package/out/solid.js +25 -4
  22. package/out/solid.js.map +2 -2
  23. package/out/zero-advanced/src/mod.d.ts +3 -0
  24. package/out/zero-advanced/src/mod.d.ts.map +1 -1
  25. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  26. package/out/zero-cache/src/services/change-source/pg/change-source.js +9 -4
  27. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  28. package/out/zero-cache/src/services/change-source/protocol/current/data.d.ts +0 -11
  29. package/out/zero-cache/src/services/change-source/protocol/current/data.d.ts.map +1 -1
  30. package/out/zero-cache/src/services/change-source/protocol/current/data.js +12 -3
  31. package/out/zero-cache/src/services/change-source/protocol/current/data.js.map +1 -1
  32. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts +0 -15
  33. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts.map +1 -1
  34. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +0 -5
  35. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  36. package/out/zero-cache/src/services/replicator/change-processor.d.ts.map +1 -1
  37. package/out/zero-cache/src/services/replicator/change-processor.js +68 -33
  38. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  39. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts +2 -2
  40. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
  41. package/out/zero-cache/src/services/replicator/schema/change-log.js +3 -2
  42. package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
  43. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts.map +1 -1
  44. package/out/zero-cache/src/services/view-syncer/cvr-store.js +4 -6
  45. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  46. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  47. package/out/zero-cache/src/services/view-syncer/cvr.js +3 -2
  48. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  49. package/out/zero-cache/src/services/view-syncer/schema/cvr.d.ts.map +1 -1
  50. package/out/zero-cache/src/services/view-syncer/schema/cvr.js +0 -4
  51. package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
  52. package/out/zero-cache/src/services/view-syncer/schema/init.d.ts.map +1 -1
  53. package/out/zero-cache/src/services/view-syncer/schema/init.js +6 -0
  54. package/out/zero-cache/src/services/view-syncer/schema/init.js.map +1 -1
  55. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  56. package/out/zero-cache/src/services/view-syncer/view-syncer.js +43 -62
  57. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  58. package/out/zero-cache/src/types/lite.d.ts +4 -1
  59. package/out/zero-cache/src/types/lite.d.ts.map +1 -1
  60. package/out/zero-cache/src/types/lite.js +10 -5
  61. package/out/zero-cache/src/types/lite.js.map +1 -1
  62. package/out/zero-client/src/client/context.d.ts +1 -1
  63. package/out/zero-client/src/client/context.d.ts.map +1 -1
  64. package/out/zero-client/src/client/crud.d.ts +1 -1
  65. package/out/zero-client/src/client/crud.d.ts.map +1 -1
  66. package/out/zero-client/src/client/custom.d.ts +13 -5
  67. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  68. package/out/zero-client/src/client/ivm-branch.d.ts +48 -0
  69. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -0
  70. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  71. package/out/zero-solid/src/solid-view.d.ts +1 -2
  72. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  73. package/out/zero.js +2 -2
  74. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  75. package/out/zql/src/ivm/array-view.js +3 -2
  76. package/out/zql/src/ivm/array-view.js.map +1 -1
  77. package/out/zql/src/ivm/view-apply-change.d.ts +9 -1
  78. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  79. package/out/zql/src/ivm/view-apply-change.js +56 -14
  80. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  81. package/out/zql/src/ivm/view.d.ts +1 -1
  82. package/out/zql/src/ivm/view.d.ts.map +1 -1
  83. package/package.json +3 -3
  84. package/out/chunk-Q6D7EOS6.js.map +0 -7
  85. package/out/chunk-XXZWDMNW.js.map +0 -7
  86. package/out/zero-client/src/client/ivm-source-repo.d.ts +0 -43
  87. package/out/zero-client/src/client/ivm-source-repo.d.ts.map +0 -1
package/out/advanced.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import "./chunk-ULOTOBTC.js";
2
2
  import {
3
3
  applyChange
4
- } from "./chunk-Q6D7EOS6.js";
4
+ } from "./chunk-6XQK3UTJ.js";
5
5
  import "./chunk-424PT5DM.js";
6
6
  export {
7
7
  applyChange
@@ -47,11 +47,6 @@ function assertNotNull(v) {
47
47
  throw new Error("Expected non-null value");
48
48
  }
49
49
  }
50
- function assertUndefined(v, msg = "Expected undefined value") {
51
- if (v !== void 0) {
52
- throw new Error(msg);
53
- }
54
- }
55
50
  function unreachable(_) {
56
51
  throw new Error("Unreachable");
57
52
  }
@@ -123,7 +118,7 @@ function drainStreams(node) {
123
118
  }
124
119
 
125
120
  // ../zql/src/ivm/view-apply-change.ts
126
- function applyChange(parentEntry, change, schema, relationship, format) {
121
+ function applyChange(parentEntry, change, schema, relationship, format, refCountMap) {
127
122
  if (schema.isHidden) {
128
123
  switch (change.type) {
129
124
  case "add":
@@ -138,7 +133,8 @@ function applyChange(parentEntry, change, schema, relationship, format) {
138
133
  { type: change.type, node },
139
134
  childSchema,
140
135
  relationship2,
141
- format
136
+ format,
137
+ refCountMap
142
138
  );
143
139
  }
144
140
  }
@@ -154,7 +150,8 @@ function applyChange(parentEntry, change, schema, relationship, format) {
154
150
  change.child.change,
155
151
  childSchema,
156
152
  relationship,
157
- format
153
+ format,
154
+ refCountMap
158
155
  );
159
156
  return;
160
157
  }
@@ -169,16 +166,32 @@ function applyChange(parentEntry, change, schema, relationship, format) {
169
166
  ...change.node.row
170
167
  };
171
168
  if (singular) {
172
- assertUndefined(
173
- parentEntry[relationship],
174
- "single output already exists"
175
- );
176
- parentEntry[relationship] = newEntry;
169
+ const oldEntry = parentEntry[relationship];
170
+ if (oldEntry !== void 0) {
171
+ assert(
172
+ schema.compareRows(oldEntry, newEntry) === 0,
173
+ "single output already exists"
174
+ );
175
+ const rc = must(refCountMap.get(oldEntry));
176
+ refCountMap.delete(oldEntry);
177
+ parentEntry[relationship] = newEntry;
178
+ refCountMap.set(newEntry, rc + 1);
179
+ } else {
180
+ parentEntry[relationship] = newEntry;
181
+ refCountMap.set(newEntry, 1);
182
+ }
177
183
  } else {
178
184
  const view = getChildEntryList(parentEntry, relationship);
179
185
  const { pos, found } = binarySearch(view, newEntry, schema.compareRows);
180
- assert(!found, "node already exists");
181
- view.splice(pos, 0, newEntry);
186
+ let deleteCount = 0;
187
+ let rc = 1;
188
+ if (found) {
189
+ deleteCount = 1;
190
+ rc = must(refCountMap.get(view[pos])) + 1;
191
+ refCountMap.delete(view[pos]);
192
+ }
193
+ view.splice(pos, deleteCount, newEntry);
194
+ refCountMap.set(newEntry, rc);
182
195
  }
183
196
  for (const [relationship2, children] of Object.entries(
184
197
  change.node.relationships
@@ -196,7 +209,8 @@ function applyChange(parentEntry, change, schema, relationship, format) {
196
209
  { type: "add", node },
197
210
  childSchema,
198
211
  relationship2,
199
- childFormat
212
+ childFormat,
213
+ refCountMap
200
214
  );
201
215
  }
202
216
  }
@@ -204,8 +218,15 @@ function applyChange(parentEntry, change, schema, relationship, format) {
204
218
  }
205
219
  case "remove": {
206
220
  if (singular) {
207
- assertObject(parentEntry[relationship]);
208
- parentEntry[relationship] = void 0;
221
+ const oldEntry = parentEntry[relationship];
222
+ assert(oldEntry !== void 0, "node does not exist");
223
+ const rc = must(refCountMap.get(oldEntry));
224
+ if (rc === 1) {
225
+ refCountMap.delete(oldEntry);
226
+ parentEntry[relationship] = void 0;
227
+ } else {
228
+ refCountMap.set(oldEntry, rc - 1);
229
+ }
209
230
  } else {
210
231
  const view = getChildEntryList(parentEntry, relationship);
211
232
  const { pos, found } = binarySearch(
@@ -214,7 +235,13 @@ function applyChange(parentEntry, change, schema, relationship, format) {
214
235
  schema.compareRows
215
236
  );
216
237
  assert(found, "node does not exist");
217
- view.splice(pos, 1);
238
+ const rc = must(refCountMap.get(view[pos]));
239
+ if (rc === 1) {
240
+ refCountMap.delete(view[pos]);
241
+ view.splice(pos, 1);
242
+ } else {
243
+ refCountMap.set(view[pos], rc - 1);
244
+ }
218
245
  }
219
246
  drainStreams(change.node);
220
247
  break;
@@ -244,7 +271,8 @@ function applyChange(parentEntry, change, schema, relationship, format) {
244
271
  change.child.change,
245
272
  childSchema,
246
273
  change.child.relationshipName,
247
- childFormat
274
+ childFormat,
275
+ refCountMap
248
276
  );
249
277
  }
250
278
  break;
@@ -266,11 +294,14 @@ function applyChange(parentEntry, change, schema, relationship, format) {
266
294
  schema.compareRows
267
295
  );
268
296
  assert(found, "node does not exists");
297
+ const rc = must(refCountMap.get(view[pos]));
298
+ refCountMap.delete(view[pos]);
269
299
  view[pos] = makeEntryPreserveRelationships(
270
300
  change.node.row,
271
301
  view[pos],
272
302
  schema.relationships
273
303
  );
304
+ refCountMap.set(view[pos], rc);
274
305
  } else {
275
306
  const { pos, found } = binarySearch(
276
307
  view,
@@ -353,4 +384,4 @@ export {
353
384
  drainStreams,
354
385
  applyChange
355
386
  };
356
- //# sourceMappingURL=chunk-Q6D7EOS6.js.map
387
+ //# sourceMappingURL=chunk-6XQK3UTJ.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../shared/src/asserts.ts", "../../shared/src/must.ts", "../../zql/src/ivm/data.ts", "../../zql/src/ivm/view-apply-change.ts"],
4
+ "sourcesContent": ["export function assert(\n b: unknown,\n msg: string | (() => string) = 'Assertion failed',\n): asserts b {\n if (!b) {\n const msgStr = typeof msg === 'string' ? msg : msg();\n throw new Error(msgStr);\n }\n}\n\nexport function assertString(v: unknown): asserts v is string {\n assertType(v, 'string');\n}\n\nexport function assertNumber(v: unknown): asserts v is number {\n assertType(v, 'number');\n}\n\nexport function assertBoolean(v: unknown): asserts v is boolean {\n assertType(v, 'boolean');\n}\n\nfunction assertType(v: unknown, t: string) {\n if (typeof v !== t) {\n throwInvalidType(v, t);\n }\n}\n\nexport function assertObject(v: unknown): asserts v is Record<string, unknown> {\n if (v === null) {\n throwInvalidType(v, 'object');\n }\n assertType(v, 'object');\n}\n\nexport function assertArray(v: unknown): asserts v is unknown[] {\n if (!Array.isArray(v)) {\n throwInvalidType(v, 'array');\n }\n}\n\nexport function invalidType(v: unknown, t: string): string {\n let s = 'Invalid type: ';\n if (v === null || v === undefined) {\n s += v;\n } else {\n s += `${typeof v} \\`${v}\\``;\n }\n return s + `, expected ${t}`;\n}\n\nexport function throwInvalidType(v: unknown, t: string): never {\n throw new Error(invalidType(v, t));\n}\n\nexport function assertNotNull<T>(v: T | null): asserts v is T {\n if (v === null) {\n throw new Error('Expected non-null value');\n }\n}\n\nexport function assertUndefined<T>(\n v: T | undefined,\n msg = 'Expected undefined value',\n): asserts v is T {\n if (v !== undefined) {\n throw new Error(msg);\n }\n}\n\nexport function assertNotUndefined<T>(\n v: T | undefined,\n msg = 'Expected non undefined value',\n): asserts v is T {\n if (v === undefined) {\n throw new Error(msg);\n }\n}\n\nexport function assertInstanceof<T>(\n v: unknown,\n t: new (...args: unknown[]) => T,\n): asserts v is T {\n if (!(v instanceof t)) {\n throw new Error(`Expected instanceof ${t.name}`);\n }\n}\n\nexport function assertUint8Array(v: unknown): asserts v is Uint8Array {\n assertInstanceof(v, Uint8Array);\n}\n\nexport function unreachable(): never;\nexport function unreachable(v: never): never;\nexport function unreachable(_?: never): never {\n throw new Error('Unreachable');\n}\n\nexport function notImplemented(): never {\n throw new Error('Not implemented');\n}\n", "export function must<T>(v: T | undefined | null, msg?: string): T {\n // eslint-disable-next-line eqeqeq\n if (v == null) {\n throw new Error(msg ?? `Unexpected ${v} value`);\n }\n return v;\n}\n", "import {compareUTF8} from 'compare-utf8';\nimport {\n assertBoolean,\n assertNumber,\n assertString,\n} from '../../../shared/src/asserts.ts';\nimport type {Ordering} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * A row flowing through the pipeline, plus its relationships.\n * Relationships are generated lazily as read.\n */\nexport type Node = {\n row: Row;\n relationships: Record<string, () => Stream<Node>>;\n};\n\n/**\n * Compare two values. The values must be of the same type. This function\n * throws at runtime if the types differ.\n *\n * Note, this function considers `null === null` and\n * `undefined === undefined`. This is different than SQL. In join code,\n * null must be treated separately.\n *\n * See: https://github.com/rocicorp/mono/pull/2116/files#r1704811479\n *\n * @returns < 0 if a < b, 0 if a === b, > 0 if a > b\n */\nexport function compareValues(a: Value, b: Value): number {\n a = normalizeUndefined(a);\n b = normalizeUndefined(b);\n\n if (a === b) {\n return 0;\n }\n if (a === null) {\n return -1;\n }\n if (b === null) {\n return 1;\n }\n if (typeof a === 'boolean') {\n assertBoolean(b);\n return a ? 1 : -1;\n }\n if (typeof a === 'number') {\n assertNumber(b);\n return a - b;\n }\n if (typeof a === 'string') {\n assertString(b);\n // We compare all strings in Zero as UTF-8. This is the default on SQLite\n // and we need to match it. See:\n // https://blog.replicache.dev/blog/replicache-11-adventures-in-text-encoding.\n //\n // TODO: We could change this since SQLite supports UTF-16. Microbenchmark\n // to see if there's a big win.\n //\n // https://www.sqlite.org/c3ref/create_collation.html\n return compareUTF8(a, b);\n }\n throw new Error(`Unsupported type: ${a}`);\n}\n\nexport type NormalizedValue = Exclude<Value, undefined>;\n\n/**\n * We allow undefined to be passed for the convenience of developers, but we\n * treat it equivalently to null. It's better for perf to not create an copy\n * of input values, so we just normalize at use when necessary.\n */\nexport function normalizeUndefined(v: Value): NormalizedValue {\n return v ?? null;\n}\n\nexport type Comparator = (r1: Row, r2: Row) => number;\n\nexport function makeComparator(\n order: Ordering,\n reverse?: boolean | undefined,\n): Comparator {\n return (a, b) => {\n // Skip destructuring here since it is hot code.\n for (const ord of order) {\n const field = ord[0];\n const comp = compareValues(a[field], b[field]);\n if (comp !== 0) {\n const result = ord[1] === 'asc' ? comp : -comp;\n return reverse ? -result : result;\n }\n }\n return 0;\n };\n}\n\n/**\n * Determine if two values are equal. Note that unlike compareValues() above,\n * this function treats `null` as unequal to itself (and same for `undefined`).\n * This is required to make joins work correctly, but may not be the right\n * semantic for your application.\n */\nexport function valuesEqual(a: Value, b: Value): boolean {\n // eslint-disable-next-line eqeqeq\n if (a == null || b == null) {\n return false;\n }\n return a === b;\n}\n\nexport function drainStreams(node: Node) {\n for (const stream of Object.values(node.relationships)) {\n for (const node of stream()) {\n drainStreams(node);\n }\n }\n}\n", "import {\n assert,\n assertArray,\n assertObject,\n unreachable,\n} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {drainStreams, type Comparator, type Node} from './data.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Entry, EntryList, Format} from './view.ts';\n\n/**\n * `applyChange` does not consume the `relationships` of `ChildChange#node`,\n * `EditChange#node` and `EditChange#oldNode`. The `ViewChange` type\n * documents and enforces this via the type system.\n */\nexport type ViewChange =\n | AddViewChange\n | RemoveViewChange\n | ChildViewChange\n | EditViewChange;\n\nexport type RowOnlyNode = {row: Row};\n\nexport type AddViewChange = {\n type: 'add';\n node: Node;\n};\n\nexport type RemoveViewChange = {\n type: 'remove';\n node: Node;\n};\n\ntype ChildViewChange = {\n type: 'child';\n node: RowOnlyNode;\n child: {\n relationshipName: string;\n change: ViewChange;\n };\n};\n\ntype EditViewChange = {\n type: 'edit';\n node: RowOnlyNode;\n oldNode: RowOnlyNode;\n};\n\n/**\n * This is a subset of WeakMap but restricted to what we need.\n */\nexport interface RefCountMap {\n get(entry: Entry): number | undefined;\n set(entry: Entry, refCount: number): void;\n delete(entry: Entry): boolean;\n}\n\nexport function applyChange(\n parentEntry: Entry,\n change: ViewChange,\n schema: SourceSchema,\n relationship: string,\n format: Format,\n refCountMap: RefCountMap,\n) {\n if (schema.isHidden) {\n switch (change.type) {\n case 'add':\n case 'remove':\n for (const [relationship, children] of Object.entries(\n change.node.relationships,\n )) {\n const childSchema = must(schema.relationships[relationship]);\n for (const node of children()) {\n applyChange(\n parentEntry,\n {type: change.type, node},\n childSchema,\n relationship,\n format,\n refCountMap,\n );\n }\n }\n return;\n case 'edit':\n // If hidden at this level it means that the hidden row was changed. If\n // the row was changed in such a way that it would change the\n // relationships then the edit would have been split into remove and\n // add.\n return;\n case 'child': {\n const childSchema = must(\n schema.relationships[change.child.relationshipName],\n );\n applyChange(\n parentEntry,\n change.child.change,\n childSchema,\n relationship,\n format,\n refCountMap,\n );\n return;\n }\n default:\n unreachable(change);\n }\n }\n\n const {singular, relationships: childFormats} = format;\n switch (change.type) {\n case 'add': {\n // TODO: Only create a new entry if we need to mutate the existing one.\n const newEntry: Entry = {\n ...change.node.row,\n };\n if (singular) {\n const oldEntry = parentEntry[relationship] as Entry | undefined;\n if (oldEntry !== undefined) {\n assert(\n schema.compareRows(oldEntry, newEntry) === 0,\n 'single output already exists',\n );\n // adding same again.\n const rc = must(refCountMap.get(oldEntry));\n refCountMap.delete(oldEntry);\n // @ts-expect-error parentEntry is readonly\n parentEntry[relationship] = newEntry;\n refCountMap.set(newEntry, rc + 1);\n } else {\n // @ts-expect-error parentEntry is readonly\n parentEntry[relationship] = newEntry;\n refCountMap.set(newEntry, 1);\n }\n } else {\n const view = getChildEntryList(parentEntry, relationship);\n const {pos, found} = binarySearch(view, newEntry, schema.compareRows);\n\n let deleteCount = 0;\n let rc = 1;\n if (found) {\n deleteCount = 1;\n rc = must(refCountMap.get(view[pos])) + 1;\n refCountMap.delete(view[pos]);\n }\n\n // @ts-expect-error view is readonly\n view.splice(pos, deleteCount, newEntry);\n refCountMap.set(newEntry, rc);\n }\n for (const [relationship, children] of Object.entries(\n change.node.relationships,\n )) {\n // TODO: Is there a flag to make TypeScript complain that dictionary access might be undefined?\n const childSchema = must(schema.relationships[relationship]);\n const childFormat = childFormats[relationship];\n if (childFormat === undefined) {\n continue;\n }\n\n const newView = childFormat.singular ? undefined : ([] as EntryList);\n // @ts-expect-error newEntry is readonly\n newEntry[relationship] = newView;\n for (const node of children()) {\n applyChange(\n newEntry,\n {type: 'add', node},\n childSchema,\n relationship,\n childFormat,\n refCountMap,\n );\n }\n }\n break;\n }\n case 'remove': {\n if (singular) {\n const oldEntry = parentEntry[relationship] as Entry | undefined;\n assert(oldEntry !== undefined, 'node does not exist');\n const rc = must(refCountMap.get(oldEntry));\n if (rc === 1) {\n refCountMap.delete(oldEntry);\n // @ts-expect-error parentEntry is readonly\n parentEntry[relationship] = undefined;\n } else {\n refCountMap.set(oldEntry, rc - 1);\n }\n } else {\n const view = getChildEntryList(parentEntry, relationship);\n const {pos, found} = binarySearch(\n view,\n change.node.row,\n schema.compareRows,\n );\n assert(found, 'node does not exist');\n const rc = must(refCountMap.get(view[pos]));\n if (rc === 1) {\n refCountMap.delete(view[pos]);\n // @ts-expect-error view is readonly\n view.splice(pos, 1);\n } else {\n refCountMap.set(view[pos], rc - 1);\n }\n }\n // Needed to ensure cleanup of operator state is fully done.\n drainStreams(change.node);\n break;\n }\n case 'child': {\n let existing: Entry;\n if (singular) {\n assertObject(parentEntry[relationship]);\n existing = parentEntry[relationship];\n } else {\n const view = getChildEntryList(parentEntry, relationship);\n const {pos, found} = binarySearch(\n view,\n change.node.row,\n schema.compareRows,\n );\n assert(found, 'node does not exist');\n existing = view[pos];\n }\n\n const childSchema = must(\n schema.relationships[change.child.relationshipName],\n );\n const childFormat = format.relationships[change.child.relationshipName];\n if (childFormat !== undefined) {\n applyChange(\n existing,\n change.child.change,\n childSchema,\n change.child.relationshipName,\n childFormat,\n refCountMap,\n );\n }\n break;\n }\n case 'edit': {\n if (singular) {\n assertObject(parentEntry[relationship]);\n // @ts-expect-error parentEntry is readonly\n parentEntry[relationship] = {\n ...parentEntry[relationship],\n ...change.node.row,\n };\n } else {\n const view = parentEntry[relationship] as EntryList | undefined;\n assertArray(view);\n // If the order changed due to the edit, we need to remove and reinsert.\n if (schema.compareRows(change.oldNode.row, change.node.row) === 0) {\n const {pos, found} = binarySearch(\n view,\n change.oldNode.row,\n schema.compareRows,\n );\n assert(found, 'node does not exists');\n const rc = must(refCountMap.get(view[pos]));\n refCountMap.delete(view[pos]);\n view[pos] = makeEntryPreserveRelationships(\n change.node.row,\n view[pos],\n schema.relationships,\n );\n refCountMap.set(view[pos], rc);\n } else {\n // Remove\n const {pos, found} = binarySearch(\n view,\n change.oldNode.row,\n schema.compareRows,\n );\n assert(found, 'node does not exists');\n const oldEntry = view[pos];\n view.splice(pos, 1);\n\n // Insert\n {\n const {pos, found} = binarySearch(\n view,\n change.node.row,\n schema.compareRows,\n );\n assert(!found, 'node already exists');\n view.splice(\n pos,\n 0,\n makeEntryPreserveRelationships(\n change.node.row,\n oldEntry,\n schema.relationships,\n ),\n );\n }\n }\n }\n break;\n }\n default:\n unreachable(change);\n }\n}\n\n// TODO: Do not return an object. It puts unnecessary pressure on the GC.\nfunction binarySearch(view: EntryList, target: Entry, comparator: Comparator) {\n let low = 0;\n let high = view.length - 1;\n while (low <= high) {\n const mid = (low + high) >>> 1;\n const comparison = comparator(view[mid] as Row, target as Row);\n if (comparison < 0) {\n low = mid + 1;\n } else if (comparison > 0) {\n high = mid - 1;\n } else {\n return {pos: mid, found: true};\n }\n }\n return {pos: low, found: false};\n}\n\nfunction makeEntryPreserveRelationships(\n row: Row,\n entry: Entry,\n relationships: {[key: string]: SourceSchema},\n): Entry {\n const result: Entry = {...row};\n for (const relationship in relationships) {\n assert(!(relationship in row), 'Relationship already exists');\n // @ts-expect-error entry is readonly\n result[relationship] = entry[relationship];\n }\n return result;\n}\n\nfunction getChildEntryList(\n parentEntry: Entry,\n relationship: string,\n): EntryList {\n const view = parentEntry[relationship] as unknown;\n assertArray(view);\n return view as EntryList;\n}\n"],
5
+ "mappings": ";AAAO,SAAS,OACd,GACA,MAA+B,oBACpB;AACX,MAAI,CAAC,GAAG;AACN,UAAM,SAAS,OAAO,QAAQ,WAAW,MAAM,IAAI;AACnD,UAAM,IAAI,MAAM,MAAM;AAAA,EACxB;AACF;AAEO,SAAS,aAAa,GAAiC;AAC5D,aAAW,GAAG,QAAQ;AACxB;AAEO,SAAS,aAAa,GAAiC;AAC5D,aAAW,GAAG,QAAQ;AACxB;AAEO,SAAS,cAAc,GAAkC;AAC9D,aAAW,GAAG,SAAS;AACzB;AAEA,SAAS,WAAW,GAAY,GAAW;AACzC,MAAI,OAAO,MAA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