@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
@@ -18,7 +18,7 @@ import {
18
18
  throwInvalidType,
19
19
  unreachable,
20
20
  valuesEqual
21
- } from "./chunk-Q6D7EOS6.js";
21
+ } from "./chunk-6XQK3UTJ.js";
22
22
  import {
23
23
  __export,
24
24
  __reExport
@@ -3661,7 +3661,7 @@ async function* scanForHash(expectedRootHash, getRootHash, hash2, fromKey, readN
3661
3661
  }
3662
3662
  }
3663
3663
  async function allEntriesAsDiff(map, op) {
3664
- const diff2 = [];
3664
+ const diff3 = [];
3665
3665
  const make = op === "add" ? (entry) => ({
3666
3666
  op: "add",
3667
3667
  key: entry[0],
@@ -3672,9 +3672,9 @@ async function allEntriesAsDiff(map, op) {
3672
3672
  oldValue: entry[1]
3673
3673
  });
3674
3674
  for await (const entry of map.entries()) {
3675
- diff2.push(make(entry));
3675
+ diff3.push(make(entry));
3676
3676
  }
3677
- return diff2;
3677
+ return diff3;
3678
3678
  }
3679
3679
 
3680
3680
  // ../replicache/src/db/index-operation-enum.ts
@@ -3843,10 +3843,6 @@ async function readFromHead(name, dagRead, formatVersion) {
3843
3843
  const commit = await commitFromHead(name, dagRead);
3844
3844
  return readFromCommit(commit, dagRead, formatVersion);
3845
3845
  }
3846
- async function readFromHash(hash2, dagRead, formatVersion) {
3847
- const commit = await commitFromHash(hash2, dagRead);
3848
- return readFromCommit(commit, dagRead, formatVersion);
3849
- }
3850
3846
  function readFromCommit(commit, dagRead, formatVersion) {
3851
3847
  const indexes = readIndexesForRead(commit, dagRead, formatVersion);
3852
3848
  const map = new BTreeRead(dagRead, formatVersion, commit.valueHash);
@@ -4166,13 +4162,16 @@ var SubscriptionTransactionWrapper = class {
4166
4162
  return this.#scans;
4167
4163
  }
4168
4164
  };
4165
+ var zeroData = Symbol();
4169
4166
  var WriteTransactionImpl = class extends ReadTransactionImpl {
4170
4167
  reason;
4171
4168
  mutationID;
4172
- constructor(clientID, mutationID, reason, dbWrite, lc, rpcName = "openWriteTransaction") {
4169
+ [zeroData];
4170
+ constructor(clientID, mutationID, reason, zData, dbWrite, lc, rpcName = "openWriteTransaction") {
4173
4171
  super(clientID, dbWrite, lc, rpcName);
4174
4172
  this.mutationID = mutationID;
4175
4173
  this.reason = reason;
4174
+ this[zeroData] = zData;
4176
4175
  }
4177
4176
  put(key, value) {
4178
4177
  return this.set(key, value);
@@ -4391,6 +4390,13 @@ var DiffsMap = class extends Map {
4391
4390
  return super.set(key, value);
4392
4391
  }
4393
4392
  };
4393
+ async function diff2(oldHash, newHash, read, diffConfig, formatVersion) {
4394
+ const [oldCommit, newCommit] = await Promise.all([
4395
+ commitFromHash(oldHash, read),
4396
+ commitFromHash(newHash, read)
4397
+ ]);
4398
+ return diffCommits(oldCommit, newCommit, read, diffConfig, formatVersion);
4399
+ }
4394
4400
  async function diffCommits(oldCommit, newCommit, read, diffConfig, formatVersion) {
4395
4401
  const diffsMap = new DiffsMap();
4396
4402
  if (!diffConfig.shouldComputeDiffs()) {
@@ -4714,7 +4720,7 @@ async function createIndexBTree(lc, dagWrite, valueMap, prefix, jsonPointer, all
4714
4720
  }
4715
4721
 
4716
4722
  // ../replicache/src/db/rebase.ts
4717
- async function rebaseMutation(mutation, dagWrite, basisHash, mutators, lc, mutationClientID, formatVersion) {
4723
+ async function rebaseMutation(mutation, dagWrite, basisHash, mutators, lc, mutationClientID, formatVersion, zeroData2) {
4718
4724
  const localMeta = mutation.meta;
4719
4725
  const name = localMeta.mutatorName;
4720
4726
  if (isLocalMetaDD31(localMeta)) {
@@ -4757,13 +4763,14 @@ async function rebaseMutation(mutation, dagWrite, basisHash, mutators, lc, mutat
4757
4763
  mutationClientID,
4758
4764
  await dbWrite.getMutationID(),
4759
4765
  "rebase",
4766
+ zeroData2,
4760
4767
  dbWrite,
4761
4768
  lc
4762
4769
  );
4763
4770
  await mutatorImpl(tx, args);
4764
4771
  return dbWrite;
4765
4772
  }
4766
- async function rebaseMutationAndPutCommit(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion) {
4773
+ async function rebaseMutationAndPutCommit(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData2) {
4767
4774
  const tx = await rebaseMutation(
4768
4775
  mutation,
4769
4776
  dagWrite,
@@ -4771,11 +4778,12 @@ async function rebaseMutationAndPutCommit(mutation, dagWrite, basis, mutators, l
4771
4778
  mutators,
4772
4779
  lc,
4773
4780
  mutationClientID,
4774
- formatVersion
4781
+ formatVersion,
4782
+ zeroData2
4775
4783
  );
4776
4784
  return tx.putCommit();
4777
4785
  }
4778
- async function rebaseMutationAndCommit(mutation, dagWrite, basis, headName, mutators, lc, mutationClientID, formatVersion) {
4786
+ async function rebaseMutationAndCommit(mutation, dagWrite, basis, headName, mutators, lc, mutationClientID, formatVersion, zeroData2) {
4779
4787
  const dbWrite = await rebaseMutation(
4780
4788
  mutation,
4781
4789
  dagWrite,
@@ -4783,7 +4791,8 @@ async function rebaseMutationAndCommit(mutation, dagWrite, basis, headName, muta
4783
4791
  mutators,
4784
4792
  lc,
4785
4793
  mutationClientID,
4786
- formatVersion
4794
+ formatVersion,
4795
+ zeroData2
4787
4796
  );
4788
4797
  return dbWrite.commit(headName);
4789
4798
  }
@@ -5053,29 +5062,10 @@ var CookieMismatch = 2;
5053
5062
 
5054
5063
  // ../replicache/src/sync/patch.ts
5055
5064
  async function apply(lc, dbWrite, patch) {
5056
- const ret = [];
5057
- function pushChangeOrAdd(key, oldValue, newValue) {
5058
- if (oldValue === void 0) {
5059
- ret.push({
5060
- op: "add",
5061
- key,
5062
- newValue
5063
- });
5064
- } else {
5065
- ret.push({
5066
- op: "change",
5067
- key,
5068
- oldValue,
5069
- newValue
5070
- });
5071
- }
5072
- }
5073
5065
  for (const p of patch) {
5074
5066
  switch (p.op) {
5075
5067
  case "put": {
5076
- const existing = await dbWrite.get(p.key);
5077
5068
  const frozen = deepFreeze(p.value);
5078
- pushChangeOrAdd(p.key, existing, frozen);
5079
5069
  await dbWrite.put(lc, p.key, frozen);
5080
5070
  break;
5081
5071
  }
@@ -5097,7 +5087,6 @@ async function apply(lc, dbWrite, patch) {
5097
5087
  addToEntries(p.merge);
5098
5088
  }
5099
5089
  const frozen = deepFreeze(Object.fromEntries(entries));
5100
- pushChangeOrAdd(p.key, existing, frozen);
5101
5090
  await dbWrite.put(lc, p.key, frozen);
5102
5091
  break;
5103
5092
  }
@@ -5107,22 +5096,13 @@ async function apply(lc, dbWrite, patch) {
5107
5096
  continue;
5108
5097
  }
5109
5098
  await dbWrite.del(lc, p.key);
5110
- ret.push({
5111
- op: "del",
5112
- key: p.key,
5113
- oldValue: existing
5114
- });
5115
5099
  break;
5116
5100
  }
5117
5101
  case "clear":
5118
5102
  await dbWrite.clear();
5119
- ret.push({
5120
- op: "clear"
5121
- });
5122
5103
  break;
5123
5104
  }
5124
5105
  }
5125
- return ret;
5126
5106
  }
5127
5107
 
5128
5108
  // ../replicache/src/sync/pull-error.ts
@@ -5289,11 +5269,10 @@ function handlePullResponseV1(lc, store, expectedBaseCookie, response, clientID,
5289
5269
  clientID,
5290
5270
  formatVersion
5291
5271
  );
5292
- const diffs = await apply(lc, dbWrite, response.patch);
5272
+ await apply(lc, dbWrite, response.patch);
5293
5273
  return {
5294
5274
  type: Applied,
5295
- syncHead: await dbWrite.commit(SYNC_HEAD_NAME),
5296
- diffs
5275
+ syncHead: await dbWrite.commit(SYNC_HEAD_NAME)
5297
5276
  };
5298
5277
  });
5299
5278
  }
@@ -6410,7 +6389,8 @@ async function rebase(mutations, basis, write, mutators, mutationIDs, lc, format
6410
6389
  mutators,
6411
6390
  lc,
6412
6391
  meta.clientID,
6413
- formatVersion
6392
+ formatVersion,
6393
+ void 0
6414
6394
  )).chunk.hash;
6415
6395
  }
6416
6396
  }
@@ -6575,7 +6555,8 @@ async function refresh(lc, memdag, perdag, clientID, mutators, diffConfig, close
6575
6555
  mutators,
6576
6556
  lc,
6577
6557
  newMemdagMutations[i].meta.clientID,
6578
- formatVersion
6558
+ formatVersion,
6559
+ void 0
6579
6560
  )).chunk.hash;
6580
6561
  }
6581
6562
  const newMemdagHeadCommit = await commitFromHash(
@@ -6774,8 +6755,8 @@ var SubscriptionImpl = class {
6774
6755
  return this.#body(tx);
6775
6756
  }
6776
6757
  matches(diffs) {
6777
- for (const [indexName, diff2] of diffs) {
6778
- if (diffMatchesSubscription(this.#keys, this.#scans, indexName, diff2)) {
6758
+ for (const [indexName, diff3] of diffs) {
6759
+ if (diffMatchesSubscription(this.#keys, this.#scans, indexName, diff3)) {
6779
6760
  return true;
6780
6761
  }
6781
6762
  }
@@ -6815,7 +6796,7 @@ var WatchSubscription = class {
6815
6796
  }
6816
6797
  invoke(tx, kind, diffs) {
6817
6798
  const invoke = async (indexName, prefix, compareKey, convertInternalDiff) => {
6818
- let diff2;
6799
+ let diff3;
6819
6800
  if (kind === InitialRun) {
6820
6801
  if (!this.#initialValuesInFirstDiff) {
6821
6802
  return void 0;
@@ -6829,17 +6810,17 @@ var WatchSubscription = class {
6829
6810
  newValue: entry[1]
6830
6811
  });
6831
6812
  }
6832
- diff2 = newDiff2;
6813
+ diff3 = newDiff2;
6833
6814
  } else {
6834
6815
  assert(diffs);
6835
6816
  const maybeDiff = diffs.get(indexName ?? "") ?? [];
6836
- diff2 = convertInternalDiff(maybeDiff);
6817
+ diff3 = convertInternalDiff(maybeDiff);
6837
6818
  }
6838
6819
  const newDiff = [];
6839
- const { length } = diff2;
6840
- for (let i = diffBinarySearch(diff2, prefix, compareKey); i < length; i++) {
6841
- if (compareKey(diff2[i]).startsWith(prefix)) {
6842
- newDiff.push(diff2[i]);
6820
+ const { length } = diff3;
6821
+ for (let i = diffBinarySearch(diff3, prefix, compareKey); i < length; i++) {
6822
+ if (compareKey(diff3[i]).startsWith(prefix)) {
6823
+ newDiff.push(diff3[i]);
6843
6824
  } else {
6844
6825
  break;
6845
6826
  }
@@ -6850,29 +6831,29 @@ var WatchSubscription = class {
6850
6831
  return invoke(
6851
6832
  this.#indexName,
6852
6833
  this.#prefix,
6853
- (diff2) => diff2.key[0],
6834
+ (diff3) => diff3.key[0],
6854
6835
  (internalDiff) => convertDiffValues(internalDiff, decodeIndexKey)
6855
6836
  );
6856
6837
  }
6857
6838
  return invoke(
6858
6839
  void 0,
6859
6840
  this.#prefix,
6860
- (diff2) => diff2.key,
6841
+ (diff3) => diff3.key,
6861
6842
  (internalDiff) => convertDiffValues(internalDiff, (k) => k)
6862
6843
  );
6863
6844
  }
6864
6845
  matches(diffs) {
6865
- const diff2 = diffs.get(this.#indexName ?? "");
6866
- if (diff2 === void 0) {
6846
+ const diff3 = diffs.get(this.#indexName ?? "");
6847
+ if (diff3 === void 0) {
6867
6848
  return false;
6868
6849
  }
6869
- return watcherMatchesDiff(diff2, this.#prefix, this.#indexName);
6850
+ return watcherMatchesDiff(diff3, this.#prefix, this.#indexName);
6870
6851
  }
6871
6852
  updateDeps(_keys, _scans) {
6872
6853
  }
6873
6854
  };
6874
- function convertDiffValues(diff2, convertKey) {
6875
- return diff2.map((op) => {
6855
+ function convertDiffValues(diff3, convertKey) {
6856
+ return diff3.map((op) => {
6876
6857
  const key = convertKey(op.key);
6877
6858
  switch (op.op) {
6878
6859
  case "add":
@@ -6991,23 +6972,23 @@ var SubscriptionsManagerImpl = class {
6991
6972
  return false;
6992
6973
  }
6993
6974
  };
6994
- function diffMatchesSubscription(keys, scans, indexName, diff2) {
6975
+ function diffMatchesSubscription(keys, scans, indexName, diff3) {
6995
6976
  if (indexName === "") {
6996
- for (const diffEntry of diff2) {
6977
+ for (const diffEntry of diff3) {
6997
6978
  if (keys.has(diffEntry.key)) {
6998
6979
  return true;
6999
6980
  }
7000
6981
  }
7001
6982
  }
7002
6983
  for (const scanInfo of scans) {
7003
- if (scanInfoMatchesDiff(scanInfo, indexName, diff2)) {
6984
+ if (scanInfoMatchesDiff(scanInfo, indexName, diff3)) {
7004
6985
  return true;
7005
6986
  }
7006
6987
  }
7007
6988
  return false;
7008
6989
  }
7009
- function scanInfoMatchesDiff(scanInfo, changeIndexName, diff2) {
7010
- for (const diffEntry of diff2) {
6990
+ function scanInfoMatchesDiff(scanInfo, changeIndexName, diff3) {
6991
+ for (const diffEntry of diff3) {
7011
6992
  if (scanInfoMatchesKey(scanInfo, changeIndexName, diffEntry.key)) {
7012
6993
  return true;
7013
6994
  }
@@ -7069,18 +7050,18 @@ function* subscriptionsForDiffs(subscriptions, diffs) {
7069
7050
  }
7070
7051
  }
7071
7052
  }
7072
- function watcherMatchesDiff(diff2, prefix, indexName) {
7053
+ function watcherMatchesDiff(diff3, prefix, indexName) {
7073
7054
  if (prefix === "") {
7074
7055
  return true;
7075
7056
  }
7076
7057
  const compareKey = indexName ? (diffOp) => decodeIndexKey(diffOp.key)[0] : (diffOp) => diffOp.key;
7077
- const i = diffBinarySearch(diff2, prefix, compareKey);
7078
- return i < diff2.length && compareKey(diff2[i]).startsWith(prefix);
7058
+ const i = diffBinarySearch(diff3, prefix, compareKey);
7059
+ return i < diff3.length && compareKey(diff3[i]).startsWith(prefix);
7079
7060
  }
7080
- function diffBinarySearch(diff2, prefix, compareKey) {
7061
+ function diffBinarySearch(diff3, prefix, compareKey) {
7081
7062
  return binarySearch(
7082
- diff2.length,
7083
- (i) => compareUTF82(prefix, compareKey(diff2[i]))
7063
+ diff3.length,
7064
+ (i) => compareUTF82(prefix, compareKey(diff3[i]))
7084
7065
  );
7085
7066
  }
7086
7067
 
@@ -7611,7 +7592,8 @@ var ReplicacheImpl = class {
7611
7592
  this.#mutatorRegistry,
7612
7593
  lc,
7613
7594
  isLocalMetaDD31(meta) ? meta.clientID : clientID,
7614
- Latest
7595
+ Latest,
7596
+ void 0
7615
7597
  )
7616
7598
  );
7617
7599
  }
@@ -7832,7 +7814,7 @@ var ReplicacheImpl = class {
7832
7814
  *
7833
7815
  * @experimental This method is under development and its semantics will change.
7834
7816
  */
7835
- async poke(poke, pullApplied) {
7817
+ async poke(poke) {
7836
7818
  await this.#ready;
7837
7819
  const { clientID } = this;
7838
7820
  const requestID = newRequestID(clientID);
@@ -7856,7 +7838,6 @@ var ReplicacheImpl = class {
7856
7838
  );
7857
7839
  switch (result.type) {
7858
7840
  case Applied:
7859
- await pullApplied(this.memdag, result.syncHead, result.diffs);
7860
7841
  await this.maybeEndPull(result.syncHead, requestID);
7861
7842
  break;
7862
7843
  case CookieMismatch:
@@ -8159,6 +8140,7 @@ var ReplicacheImpl = class {
8159
8140
  clientID,
8160
8141
  await dbWrite.getMutationID(),
8161
8142
  "initial",
8143
+ void 0,
8162
8144
  dbWrite,
8163
8145
  this.#lc
8164
8146
  );
@@ -10781,6 +10763,7 @@ var ArrayView = class {
10781
10763
  onDestroy;
10782
10764
  #dirty = false;
10783
10765
  #complete = false;
10766
+ #refCountMap = /* @__PURE__ */ new WeakMap();
10784
10767
  constructor(input, format = { singular: false, relationships: {} }, queryComplete = true) {
10785
10768
  this.#input = input;
10786
10769
  this.#schema = input.getSchema();
@@ -10830,14 +10813,22 @@ var ArrayView = class {
10830
10813
  { type: "add", node },
10831
10814
  this.#schema,
10832
10815
  "",
10833
- this.#format
10816
+ this.#format,
10817
+ this.#refCountMap
10834
10818
  );
10835
10819
  }
10836
10820
  this.flush();
10837
10821
  }
10838
10822
  push(change) {
10839
10823
  this.#dirty = true;
10840
- applyChange(this.#root, change, this.#schema, "", this.#format);
10824
+ applyChange(
10825
+ this.#root,
10826
+ change,
10827
+ this.#schema,
10828
+ "",
10829
+ this.#format,
10830
+ this.#refCountMap
10831
+ );
10841
10832
  }
10842
10833
  flush() {
10843
10834
  if (!this.#dirty) {
@@ -13265,101 +13256,15 @@ function sourceNameFromKey(key) {
13265
13256
  return key.slice(ENTITIES_KEY_PREFIX.length, slash);
13266
13257
  }
13267
13258
 
13268
- // ../zero-client/src/client/ivm-source-repo.ts
13269
- var IVMSourceRepo = class {
13270
- #main;
13271
- #tables;
13272
- /**
13273
- * Sync is lazily created when the first response from the server is received.
13274
- */
13275
- #sync;
13276
- /**
13277
- * Rebase is created when the sync head is advanced and points to a fork
13278
- * of the sync head. This is used to rebase optimistic mutations.
13279
- */
13280
- #rebase;
13281
- constructor(tables) {
13282
- this.#main = new IVMSourceBranch(tables);
13283
- this.#tables = tables;
13284
- }
13285
- get main() {
13286
- return this.#main;
13287
- }
13288
- /**
13289
- * Used for reads in `zero.TransactionImpl`.
13290
- * Writes in `zero.TransactionImpl` also get applied to the rebase branch.
13291
- *
13292
- * The rebase branch is always forked off of the sync branch when a rebase begins.
13293
- */
13294
- get rebase() {
13295
- return must(this.#rebase, "rebase branch does not exist!");
13296
- }
13297
- advanceSyncHead = async (store, syncHeadHash, patches) => {
13298
- if (this.#sync === void 0) {
13299
- await withRead(store, async (dagRead) => {
13300
- const syncSources = new IVMSourceBranch(this.#tables);
13301
- const read = await readFromHash(
13302
- syncHeadHash,
13303
- dagRead,
13304
- Latest
13305
- );
13306
- for await (const entry of read.map.scan(ENTITIES_KEY_PREFIX)) {
13307
- if (!entry[0].startsWith(ENTITIES_KEY_PREFIX)) {
13308
- break;
13309
- }
13310
- const name = sourceNameFromKey(entry[0]);
13311
- const source = must(syncSources.getSource(name));
13312
- source.push({
13313
- type: "add",
13314
- row: entry[1]
13315
- });
13316
- }
13317
- this.#sync = syncSources;
13318
- });
13319
- } else {
13320
- for (const patch of patches) {
13321
- if (patch.op === "clear") {
13322
- this.#sync.clear();
13323
- continue;
13324
- }
13325
- const { key } = patch;
13326
- if (!key.startsWith(ENTITIES_KEY_PREFIX)) {
13327
- continue;
13328
- }
13329
- const name = sourceNameFromKey(key);
13330
- const source = must(this.#sync.getSource(name));
13331
- switch (patch.op) {
13332
- case "del":
13333
- source.push({
13334
- type: "remove",
13335
- row: patch.oldValue
13336
- });
13337
- break;
13338
- case "add":
13339
- source.push({
13340
- type: "add",
13341
- row: patch.newValue
13342
- });
13343
- break;
13344
- case "change":
13345
- source.push({
13346
- type: "edit",
13347
- row: patch.newValue,
13348
- oldRow: patch.oldValue
13349
- });
13350
- break;
13351
- }
13352
- }
13353
- }
13354
- this.#rebase = must(this.#sync).fork();
13355
- };
13356
- };
13259
+ // ../zero-client/src/client/ivm-branch.ts
13357
13260
  var IVMSourceBranch = class _IVMSourceBranch {
13358
13261
  #sources;
13359
13262
  #tables;
13360
- constructor(tables, sources = /* @__PURE__ */ new Map()) {
13263
+ hash;
13264
+ constructor(tables, hash2, sources = /* @__PURE__ */ new Map()) {
13361
13265
  this.#tables = tables;
13362
13266
  this.#sources = sources;
13267
+ this.hash = hash2;
13363
13268
  }
13364
13269
  getSource(name) {
13365
13270
  if (this.#sources.has(name)) {
@@ -13373,20 +13278,46 @@ var IVMSourceBranch = class _IVMSourceBranch {
13373
13278
  clear() {
13374
13279
  this.#sources.clear();
13375
13280
  }
13281
+ /**
13282
+ * Mutates the current branch, advancing it to the new head
13283
+ * by applying the given diffs.
13284
+ */
13285
+ advance(expectedHead, newHead, diffs) {
13286
+ assert(
13287
+ this.hash === expectedHead,
13288
+ () => `Expected head must match the main head. Got: ${this.hash}, expected: ${expectedHead}`
13289
+ );
13290
+ applyDiffs(diffs, this);
13291
+ this.hash = newHead;
13292
+ }
13293
+ /**
13294
+ * Fork the branch and patch it up to match the desired head.
13295
+ */
13296
+ async forkToHead(store, expectedHead, desiredHead) {
13297
+ const fork = this.fork();
13298
+ assert(
13299
+ expectedHead === fork.hash,
13300
+ () => `Expected head must match the main head. Got: ${expectedHead}, expected: ${fork.hash}`
13301
+ );
13302
+ if (fork.hash === desiredHead) {
13303
+ return fork;
13304
+ }
13305
+ await patchBranch(desiredHead, store, fork);
13306
+ fork.hash = desiredHead;
13307
+ return fork;
13308
+ }
13376
13309
  /**
13377
13310
  * Creates a new IVMSourceBranch that is a copy of the current one.
13378
13311
  * This is a cheap operation since the b-trees are shared until a write is performed
13379
13312
  * and then only the modified nodes are copied.
13380
13313
  *
13381
- * This is used when:
13382
- * 1. We need to rebase a change. We fork the `sync` branch and run the mutations against the fork.
13383
- * 2. We need to create `main` at startup.
13384
- * 3. We need to create a new `sync` head because we got a new server snapshot.
13385
- * The old `sync` head is forked and the new server snapshot is applied to the fork.
13314
+ * IVM branches are forked when we need to rebase mutations.
13315
+ * The mutations modify the fork rather than original branch.
13386
13316
  */
13387
13317
  fork() {
13388
13318
  return new _IVMSourceBranch(
13389
13319
  this.#tables,
13320
+ this.hash,
13390
13321
  new Map(
13391
13322
  wrapIterable(this.#sources.entries()).map(([name, source]) => [
13392
13323
  name,
@@ -13396,6 +13327,61 @@ var IVMSourceBranch = class _IVMSourceBranch {
13396
13327
  );
13397
13328
  }
13398
13329
  };
13330
+ async function patchBranch(desiredHead, store, fork) {
13331
+ const diffs = await computeDiffs(must(fork.hash), desiredHead, store);
13332
+ if (!diffs) {
13333
+ return;
13334
+ }
13335
+ applyDiffs(diffs, fork);
13336
+ }
13337
+ async function computeDiffs(startHash, endHash, store) {
13338
+ const readFn = (dagRead) => diff2(
13339
+ startHash,
13340
+ endHash,
13341
+ dagRead,
13342
+ {
13343
+ shouldComputeDiffs: () => true,
13344
+ shouldComputeDiffsForIndex(_name) {
13345
+ return false;
13346
+ }
13347
+ },
13348
+ Latest
13349
+ );
13350
+ const diffs = await withRead(store, readFn);
13351
+ return diffs.get("");
13352
+ }
13353
+ function applyDiffs(diffs, branch) {
13354
+ for (let i = diffBinarySearch(diffs, ENTITIES_KEY_PREFIX, (diff3) => diff3.key); i < diffs.length; i++) {
13355
+ const diff3 = diffs[i];
13356
+ const { key } = diff3;
13357
+ if (!key.startsWith(ENTITIES_KEY_PREFIX)) {
13358
+ break;
13359
+ }
13360
+ const name = sourceNameFromKey(key);
13361
+ const source = must(branch.getSource(name));
13362
+ switch (diff3.op) {
13363
+ case "del":
13364
+ source.push({
13365
+ type: "remove",
13366
+ row: diff3.oldValue
13367
+ });
13368
+ break;
13369
+ case "add":
13370
+ source.push({
13371
+ type: "add",
13372
+ row: diff3.newValue
13373
+ });
13374
+ break;
13375
+ case "change":
13376
+ source.push({
13377
+ type: "edit",
13378
+ row: diff3.newValue,
13379
+ oldRow: diff3.oldValue
13380
+ });
13381
+ break;
13382
+ }
13383
+ }
13384
+ }
13399
13385
 
13400
13386
  // ../zero-client/src/client/context.ts
13401
13387
  var ZeroContext = class {
@@ -13444,40 +13430,40 @@ var ZeroContext = class {
13444
13430
  processChanges(changes) {
13445
13431
  this.batchViewUpdates(() => {
13446
13432
  try {
13447
- for (const diff2 of changes) {
13448
- const { key } = diff2;
13433
+ for (const diff3 of changes) {
13434
+ const { key } = diff3;
13449
13435
  assert(key.startsWith(ENTITIES_KEY_PREFIX));
13450
13436
  const name = sourceNameFromKey(key);
13451
13437
  const source = this.getSource(name);
13452
13438
  if (!source) {
13453
13439
  continue;
13454
13440
  }
13455
- switch (diff2.op) {
13441
+ switch (diff3.op) {
13456
13442
  case "del":
13457
- assert(typeof diff2.oldValue === "object");
13443
+ assert(typeof diff3.oldValue === "object");
13458
13444
  source.push({
13459
13445
  type: "remove",
13460
- row: diff2.oldValue
13446
+ row: diff3.oldValue
13461
13447
  });
13462
13448
  break;
13463
13449
  case "add":
13464
- assert(typeof diff2.newValue === "object");
13450
+ assert(typeof diff3.newValue === "object");
13465
13451
  source.push({
13466
13452
  type: "add",
13467
- row: diff2.newValue
13453
+ row: diff3.newValue
13468
13454
  });
13469
13455
  break;
13470
13456
  case "change":
13471
- assert(typeof diff2.newValue === "object");
13472
- assert(typeof diff2.oldValue === "object");
13457
+ assert(typeof diff3.newValue === "object");
13458
+ assert(typeof diff3.oldValue === "object");
13473
13459
  source.push({
13474
13460
  type: "edit",
13475
- row: diff2.newValue,
13476
- oldRow: diff2.oldValue
13461
+ row: diff3.newValue,
13462
+ oldRow: diff3.oldValue
13477
13463
  });
13478
13464
  break;
13479
13465
  default:
13480
- unreachable(diff2);
13466
+ unreachable(diff3);
13481
13467
  }
13482
13468
  }
13483
13469
  } finally {
@@ -13713,7 +13699,8 @@ async function deleteImpl(tx, arg, schema, ivmBranch) {
13713
13699
 
13714
13700
  // ../zero-client/src/client/custom.ts
13715
13701
  var TransactionImpl = class {
13716
- constructor(repTx, schema, ivmSourceRepo) {
13702
+ constructor(repTx, schema) {
13703
+ const castedRepTx = repTx;
13717
13704
  must(repTx.reason === "initial" || repTx.reason === "rebase");
13718
13705
  this.clientID = repTx.clientID;
13719
13706
  this.mutationID = repTx.mutationID;
@@ -13721,15 +13708,9 @@ var TransactionImpl = class {
13721
13708
  this.mutate = makeSchemaCRUD(
13722
13709
  schema,
13723
13710
  repTx,
13724
- // Mutators do not write to the main IVM sources during optimistic mutations
13725
- // so we pass undefined here.
13726
- // ExperimentalWatch handles updating main.
13727
- this.reason === "optimistic" ? void 0 : ivmSourceRepo.rebase
13728
- );
13729
- this.query = makeSchemaQuery(
13730
- schema,
13731
- this.reason === "optimistic" ? ivmSourceRepo.main : ivmSourceRepo.rebase
13711
+ castedRepTx[zeroData]
13732
13712
  );
13713
+ this.query = {};
13733
13714
  }
13734
13715
  clientID;
13735
13716
  mutationID;
@@ -13738,25 +13719,12 @@ var TransactionImpl = class {
13738
13719
  mutate;
13739
13720
  query;
13740
13721
  };
13741
- function makeReplicacheMutator(mutator, schema, ivmSourceRepo) {
13722
+ function makeReplicacheMutator(mutator, schema) {
13742
13723
  return (repTx, args) => {
13743
- const tx = new TransactionImpl(repTx, schema, ivmSourceRepo);
13724
+ const tx = new TransactionImpl(repTx, schema);
13744
13725
  return mutator(tx, args);
13745
13726
  };
13746
13727
  }
13747
- function makeSchemaQuery(schema, ivmBranch) {
13748
- const rv = {};
13749
- const context = new ZeroContext(
13750
- ivmBranch,
13751
- () => () => {
13752
- },
13753
- (applyViewUpdates) => applyViewUpdates()
13754
- );
13755
- for (const name of Object.keys(schema.tables)) {
13756
- rv[name] = newQuery(context, schema, name);
13757
- }
13758
- return rv;
13759
- }
13760
13728
  function makeSchemaCRUD(schema, tx, ivmBranch) {
13761
13729
  const mutate = {};
13762
13730
  for (const [name] of Object.entries(schema.tables)) {
@@ -14094,7 +14062,7 @@ function makeMessage(message, context, logLevel) {
14094
14062
  }
14095
14063
 
14096
14064
  // ../zero-client/src/client/version.ts
14097
- var version2 = "0.17.2025030600";
14065
+ var version2 = "0.17.2025030701";
14098
14066
 
14099
14067
  // ../zero-client/src/client/log-options.ts
14100
14068
  var LevelFilterLogSink = class {
@@ -14407,8 +14375,8 @@ var QueryManager = class {
14407
14375
  this.#recentQueriesMaxSize = recentQueriesMaxSize;
14408
14376
  this.#send = send2;
14409
14377
  experimentalWatch(
14410
- (diff2) => {
14411
- for (const diffOp of diff2) {
14378
+ (diff3) => {
14379
+ for (const diffOp of diff3) {
14412
14380
  const queryHash = diffOp.key.substring(GOT_QUERIES_KEY_PREFIX.length);
14413
14381
  switch (diffOp.op) {
14414
14382
  case "add":
@@ -15059,7 +15027,7 @@ var Zero = class {
15059
15027
  #enableAnalytics;
15060
15028
  #pokeHandler;
15061
15029
  #queryManager;
15062
- #ivmSources;
15030
+ #ivmMain;
15063
15031
  #clientToServer;
15064
15032
  #deleteClientsManager;
15065
15033
  /**
@@ -15191,14 +15159,14 @@ var Zero = class {
15191
15159
  const replicacheMutators = {
15192
15160
  [CRUD_MUTATION_NAME]: makeCRUDMutator(schema)
15193
15161
  };
15194
- this.#ivmSources = new IVMSourceRepo(schema.tables);
15162
+ this.#ivmMain = new IVMSourceBranch(schema.tables);
15195
15163
  for (const [namespace, mutatorsForNamespace] of Object.entries(
15196
15164
  options.mutators ?? {}
15197
15165
  )) {
15198
15166
  for (const [name, mutator] of Object.entries(
15199
15167
  mutatorsForNamespace
15200
15168
  )) {
15201
- replicacheMutators[customMutatorKey(namespace, name)] = makeReplicacheMutator(mutator, schema, this.#ivmSources);
15169
+ replicacheMutators[customMutatorKey(namespace, name)] = makeReplicacheMutator(mutator, schema);
15202
15170
  }
15203
15171
  }
15204
15172
  this.storageKey = storageKey ?? "";
@@ -15292,12 +15260,12 @@ var Zero = class {
15292
15260
  this.#lc
15293
15261
  );
15294
15262
  this.#zeroContext = new ZeroContext(
15295
- this.#ivmSources.main,
15263
+ this.#ivmMain,
15296
15264
  (ast, ttl, gotCallback) => this.#queryManager.add(ast, ttl, gotCallback),
15297
15265
  batchViewUpdates
15298
15266
  );
15299
15267
  rep.experimentalWatch(
15300
- (diff2) => this.#zeroContext.processChanges(diff2),
15268
+ (diff3) => this.#zeroContext.processChanges(diff3),
15301
15269
  {
15302
15270
  prefix: ENTITIES_KEY_PREFIX,
15303
15271
  initialValuesInFirstDiff: true
@@ -15314,7 +15282,7 @@ var Zero = class {
15314
15282
  });
15315
15283
  this.#metrics.tags.push(`version:${this.version}`);
15316
15284
  this.#pokeHandler = new PokeHandler(
15317
- (poke) => this.#rep.poke(poke, this.#ivmSources.advanceSyncHead),
15285
+ (poke) => this.#rep.poke(poke),
15318
15286
  () => this.#onPokeError(),
15319
15287
  rep.clientID,
15320
15288
  schema,
@@ -16298,4 +16266,4 @@ export {
16298
16266
  escapeLike,
16299
16267
  Zero
16300
16268
  };
16301
- //# sourceMappingURL=chunk-XXZWDMNW.js.map
16269
+ //# sourceMappingURL=chunk-YKI43S5O.js.map