@rocicorp/zero 0.17.2025030300 → 0.17.2025030601

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 (168) hide show
  1. package/out/{chunk-BBNRRMOA.js → chunk-EENFDWT4.js} +226 -262
  2. package/out/chunk-EENFDWT4.js.map +7 -0
  3. package/out/react.js +19 -11
  4. package/out/react.js.map +2 -2
  5. package/out/replicache/src/replicache-impl.d.ts +1 -2
  6. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  7. package/out/replicache/src/subscriptions.d.ts +2 -1
  8. package/out/replicache/src/subscriptions.d.ts.map +1 -1
  9. package/out/replicache/src/sync/patch.d.ts +1 -1
  10. package/out/replicache/src/sync/patch.d.ts.map +1 -1
  11. package/out/replicache/src/sync/pull.d.ts +0 -2
  12. package/out/replicache/src/sync/pull.d.ts.map +1 -1
  13. package/out/solid.js +8 -3
  14. package/out/solid.js.map +2 -2
  15. package/out/zero-cache/src/auth/read-authorizer.d.ts.map +1 -1
  16. package/out/zero-cache/src/auth/read-authorizer.js +0 -1
  17. package/out/zero-cache/src/auth/read-authorizer.js.map +1 -1
  18. package/out/zero-cache/src/config/zero-config.d.ts +22 -5
  19. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  20. package/out/zero-cache/src/config/zero-config.js +30 -15
  21. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  22. package/out/zero-cache/src/db/migration-lite.d.ts.map +1 -1
  23. package/out/zero-cache/src/db/migration-lite.js +12 -6
  24. package/out/zero-cache/src/db/migration-lite.js.map +1 -1
  25. package/out/zero-cache/src/db/pg-to-lite.d.ts +4 -0
  26. package/out/zero-cache/src/db/pg-to-lite.d.ts.map +1 -1
  27. package/out/zero-cache/src/db/pg-to-lite.js +14 -4
  28. package/out/zero-cache/src/db/pg-to-lite.js.map +1 -1
  29. package/out/zero-cache/src/server/change-streamer.js +7 -7
  30. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  31. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  32. package/out/zero-cache/src/server/main.js +8 -2
  33. package/out/zero-cache/src/server/main.js.map +1 -1
  34. package/out/zero-cache/src/server/multi/config.d.ts +11 -7
  35. package/out/zero-cache/src/server/multi/config.d.ts.map +1 -1
  36. package/out/zero-cache/src/server/multi/config.js +1 -3
  37. package/out/zero-cache/src/server/multi/config.js.map +1 -1
  38. package/out/zero-cache/src/server/multi/run-worker.d.ts.map +1 -1
  39. package/out/zero-cache/src/server/multi/run-worker.js +1 -3
  40. package/out/zero-cache/src/server/multi/run-worker.js.map +1 -1
  41. package/out/zero-cache/src/server/replicator.js +1 -1
  42. package/out/zero-cache/src/server/replicator.js.map +1 -1
  43. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  44. package/out/zero-cache/src/server/syncer.js +10 -9
  45. package/out/zero-cache/src/server/syncer.js.map +1 -1
  46. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +1 -1
  47. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  48. package/out/zero-cache/src/services/change-source/pg/change-source.js +12 -3
  49. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  50. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +1 -0
  51. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  52. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +33 -8
  53. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  54. package/out/zero-cache/src/services/change-source/pg/sync-schema.d.ts.map +1 -1
  55. package/out/zero-cache/src/services/change-source/pg/sync-schema.js +9 -0
  56. package/out/zero-cache/src/services/change-source/pg/sync-schema.js.map +1 -1
  57. package/out/zero-cache/src/services/litestream/commands.d.ts +1 -1
  58. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  59. package/out/zero-cache/src/services/litestream/commands.js +3 -3
  60. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  61. package/out/zero-cache/src/services/mutagen/mutagen.js +1 -1
  62. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  63. package/out/zero-cache/src/services/replicator/schema/replication-state.d.ts +7 -0
  64. package/out/zero-cache/src/services/replicator/schema/replication-state.d.ts.map +1 -1
  65. package/out/zero-cache/src/services/replicator/schema/replication-state.js +29 -1
  66. package/out/zero-cache/src/services/replicator/schema/replication-state.js.map +1 -1
  67. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +1 -1
  68. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  69. package/out/zero-cache/src/services/view-syncer/client-handler.js +2 -1
  70. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  71. package/out/zero-cache/src/services/view-syncer/cvr-store.js +1 -1
  72. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  73. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +0 -1
  74. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  75. package/out/zero-cache/src/services/view-syncer/cvr.js +7 -16
  76. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  77. package/out/zero-cache/src/services/view-syncer/database-storage.d.ts +2 -0
  78. package/out/zero-cache/src/services/view-syncer/database-storage.d.ts.map +1 -1
  79. package/out/zero-cache/src/services/view-syncer/database-storage.js +8 -3
  80. package/out/zero-cache/src/services/view-syncer/database-storage.js.map +1 -1
  81. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +11 -3
  82. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  83. package/out/zero-cache/src/services/view-syncer/view-syncer.js +69 -61
  84. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  85. package/out/zero-cache/src/types/lite.d.ts +2 -2
  86. package/out/zero-cache/src/types/lite.d.ts.map +1 -1
  87. package/out/zero-cache/src/types/lite.js +9 -4
  88. package/out/zero-cache/src/types/lite.js.map +1 -1
  89. package/out/zero-cache/src/types/pg.d.ts +7 -3
  90. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  91. package/out/zero-cache/src/types/pg.js +11 -5
  92. package/out/zero-cache/src/types/pg.js.map +1 -1
  93. package/out/zero-cache/src/workers/connection.d.ts.map +1 -1
  94. package/out/zero-cache/src/workers/connection.js +39 -20
  95. package/out/zero-cache/src/workers/connection.js.map +1 -1
  96. package/out/zero-cache/src/workers/replicator.d.ts +2 -1
  97. package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
  98. package/out/zero-cache/src/workers/replicator.js +42 -17
  99. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  100. package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts +4 -4
  101. package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
  102. package/out/zero-cache/src/workers/syncer-ws-message-handler.js +5 -2
  103. package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
  104. package/out/zero-client/src/client/context.d.ts +1 -1
  105. package/out/zero-client/src/client/context.d.ts.map +1 -1
  106. package/out/zero-client/src/client/crud.d.ts +1 -1
  107. package/out/zero-client/src/client/crud.d.ts.map +1 -1
  108. package/out/zero-client/src/client/custom.d.ts +4 -14
  109. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  110. package/out/zero-client/src/client/ivm-branch.d.ts +48 -0
  111. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -0
  112. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  113. package/out/zero-client/src/mod.d.ts +1 -0
  114. package/out/zero-client/src/mod.d.ts.map +1 -1
  115. package/out/zero-pg/src/custom.d.ts +1 -2
  116. package/out/zero-pg/src/custom.d.ts.map +1 -1
  117. package/out/zero-pg/src/custom.js.map +1 -1
  118. package/out/zero-pg/src/mod.d.ts +1 -2
  119. package/out/zero-pg/src/mod.d.ts.map +1 -1
  120. package/out/zero-pg/src/query.d.ts +3 -4
  121. package/out/zero-pg/src/query.d.ts.map +1 -1
  122. package/out/zero-pg/src/query.js +5 -7
  123. package/out/zero-pg/src/query.js.map +1 -1
  124. package/out/zero-pg/src/web.d.ts +1 -1
  125. package/out/zero-pg/src/web.d.ts.map +1 -1
  126. package/out/zero-pg/src/web.js.map +1 -1
  127. package/out/zero-protocol/src/close-connection.d.ts +16 -0
  128. package/out/zero-protocol/src/close-connection.d.ts.map +1 -0
  129. package/out/zero-protocol/src/close-connection.js +17 -0
  130. package/out/zero-protocol/src/close-connection.js.map +1 -0
  131. package/out/zero-protocol/src/up.d.ts +1 -1
  132. package/out/zero-protocol/src/up.d.ts.map +1 -1
  133. package/out/zero-protocol/src/up.js +2 -1
  134. package/out/zero-protocol/src/up.js.map +1 -1
  135. package/out/zero-react/src/use-query.d.ts +11 -2
  136. package/out/zero-react/src/use-query.d.ts.map +1 -1
  137. package/out/zero-solid/src/use-query.d.ts +4 -1
  138. package/out/zero-solid/src/use-query.d.ts.map +1 -1
  139. package/out/zero.js +1 -1
  140. package/out/zql/src/mutate/custom.d.ts +36 -1
  141. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  142. package/out/zql/src/mutate/custom.js.map +1 -1
  143. package/out/zql/src/query/query-impl.d.ts +8 -8
  144. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  145. package/out/zql/src/query/query-impl.js +29 -25
  146. package/out/zql/src/query/query-impl.js.map +1 -1
  147. package/out/zql/src/query/query-internal.d.ts +2 -2
  148. package/out/zql/src/query/query-internal.d.ts.map +1 -1
  149. package/out/zql/src/query/query.d.ts +10 -2
  150. package/out/zql/src/query/query.d.ts.map +1 -1
  151. package/out/zql/src/query/query.js +1 -1
  152. package/out/zql/src/query/query.js.map +1 -1
  153. package/out/zql/src/query/static-query.d.ts +1 -1
  154. package/out/zql/src/query/static-query.d.ts.map +1 -1
  155. package/out/zql/src/query/static-query.js +2 -2
  156. package/out/zql/src/query/static-query.js.map +1 -1
  157. package/out/zqlite/src/db.d.ts +3 -1
  158. package/out/zqlite/src/db.d.ts.map +1 -1
  159. package/out/zqlite/src/db.js +47 -0
  160. package/out/zqlite/src/db.js.map +1 -1
  161. package/package.json +2 -2
  162. package/out/chunk-BBNRRMOA.js.map +0 -7
  163. package/out/zero-client/src/client/ivm-source-repo.d.ts +0 -43
  164. package/out/zero-client/src/client/ivm-source-repo.d.ts.map +0 -1
  165. package/out/zero-pg/src/db.d.ts +0 -27
  166. package/out/zero-pg/src/db.d.ts.map +0 -1
  167. package/out/zero-pg/src/db.js +0 -2
  168. package/out/zero-pg/src/db.js.map +0 -1
@@ -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);
@@ -4391,6 +4387,13 @@ var DiffsMap = class extends Map {
4391
4387
  return super.set(key, value);
4392
4388
  }
4393
4389
  };
4390
+ async function diff2(oldHash, newHash, read, diffConfig, formatVersion) {
4391
+ const [oldCommit, newCommit] = await Promise.all([
4392
+ commitFromHash(oldHash, read),
4393
+ commitFromHash(newHash, read)
4394
+ ]);
4395
+ return diffCommits(oldCommit, newCommit, read, diffConfig, formatVersion);
4396
+ }
4394
4397
  async function diffCommits(oldCommit, newCommit, read, diffConfig, formatVersion) {
4395
4398
  const diffsMap = new DiffsMap();
4396
4399
  if (!diffConfig.shouldComputeDiffs()) {
@@ -5053,29 +5056,10 @@ var CookieMismatch = 2;
5053
5056
 
5054
5057
  // ../replicache/src/sync/patch.ts
5055
5058
  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
5059
  for (const p of patch) {
5074
5060
  switch (p.op) {
5075
5061
  case "put": {
5076
- const existing = await dbWrite.get(p.key);
5077
5062
  const frozen = deepFreeze(p.value);
5078
- pushChangeOrAdd(p.key, existing, frozen);
5079
5063
  await dbWrite.put(lc, p.key, frozen);
5080
5064
  break;
5081
5065
  }
@@ -5097,7 +5081,6 @@ async function apply(lc, dbWrite, patch) {
5097
5081
  addToEntries(p.merge);
5098
5082
  }
5099
5083
  const frozen = deepFreeze(Object.fromEntries(entries));
5100
- pushChangeOrAdd(p.key, existing, frozen);
5101
5084
  await dbWrite.put(lc, p.key, frozen);
5102
5085
  break;
5103
5086
  }
@@ -5107,22 +5090,13 @@ async function apply(lc, dbWrite, patch) {
5107
5090
  continue;
5108
5091
  }
5109
5092
  await dbWrite.del(lc, p.key);
5110
- ret.push({
5111
- op: "del",
5112
- key: p.key,
5113
- oldValue: existing
5114
- });
5115
5093
  break;
5116
5094
  }
5117
5095
  case "clear":
5118
5096
  await dbWrite.clear();
5119
- ret.push({
5120
- op: "clear"
5121
- });
5122
5097
  break;
5123
5098
  }
5124
5099
  }
5125
- return ret;
5126
5100
  }
5127
5101
 
5128
5102
  // ../replicache/src/sync/pull-error.ts
@@ -5289,11 +5263,10 @@ function handlePullResponseV1(lc, store, expectedBaseCookie, response, clientID,
5289
5263
  clientID,
5290
5264
  formatVersion
5291
5265
  );
5292
- const diffs = await apply(lc, dbWrite, response.patch);
5266
+ await apply(lc, dbWrite, response.patch);
5293
5267
  return {
5294
5268
  type: Applied,
5295
- syncHead: await dbWrite.commit(SYNC_HEAD_NAME),
5296
- diffs
5269
+ syncHead: await dbWrite.commit(SYNC_HEAD_NAME)
5297
5270
  };
5298
5271
  });
5299
5272
  }
@@ -6774,8 +6747,8 @@ var SubscriptionImpl = class {
6774
6747
  return this.#body(tx);
6775
6748
  }
6776
6749
  matches(diffs) {
6777
- for (const [indexName, diff2] of diffs) {
6778
- if (diffMatchesSubscription(this.#keys, this.#scans, indexName, diff2)) {
6750
+ for (const [indexName, diff3] of diffs) {
6751
+ if (diffMatchesSubscription(this.#keys, this.#scans, indexName, diff3)) {
6779
6752
  return true;
6780
6753
  }
6781
6754
  }
@@ -6815,7 +6788,7 @@ var WatchSubscription = class {
6815
6788
  }
6816
6789
  invoke(tx, kind, diffs) {
6817
6790
  const invoke = async (indexName, prefix, compareKey, convertInternalDiff) => {
6818
- let diff2;
6791
+ let diff3;
6819
6792
  if (kind === InitialRun) {
6820
6793
  if (!this.#initialValuesInFirstDiff) {
6821
6794
  return void 0;
@@ -6829,17 +6802,17 @@ var WatchSubscription = class {
6829
6802
  newValue: entry[1]
6830
6803
  });
6831
6804
  }
6832
- diff2 = newDiff2;
6805
+ diff3 = newDiff2;
6833
6806
  } else {
6834
6807
  assert(diffs);
6835
6808
  const maybeDiff = diffs.get(indexName ?? "") ?? [];
6836
- diff2 = convertInternalDiff(maybeDiff);
6809
+ diff3 = convertInternalDiff(maybeDiff);
6837
6810
  }
6838
6811
  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]);
6812
+ const { length } = diff3;
6813
+ for (let i = diffBinarySearch(diff3, prefix, compareKey); i < length; i++) {
6814
+ if (compareKey(diff3[i]).startsWith(prefix)) {
6815
+ newDiff.push(diff3[i]);
6843
6816
  } else {
6844
6817
  break;
6845
6818
  }
@@ -6850,29 +6823,29 @@ var WatchSubscription = class {
6850
6823
  return invoke(
6851
6824
  this.#indexName,
6852
6825
  this.#prefix,
6853
- (diff2) => diff2.key[0],
6826
+ (diff3) => diff3.key[0],
6854
6827
  (internalDiff) => convertDiffValues(internalDiff, decodeIndexKey)
6855
6828
  );
6856
6829
  }
6857
6830
  return invoke(
6858
6831
  void 0,
6859
6832
  this.#prefix,
6860
- (diff2) => diff2.key,
6833
+ (diff3) => diff3.key,
6861
6834
  (internalDiff) => convertDiffValues(internalDiff, (k) => k)
6862
6835
  );
6863
6836
  }
6864
6837
  matches(diffs) {
6865
- const diff2 = diffs.get(this.#indexName ?? "");
6866
- if (diff2 === void 0) {
6838
+ const diff3 = diffs.get(this.#indexName ?? "");
6839
+ if (diff3 === void 0) {
6867
6840
  return false;
6868
6841
  }
6869
- return watcherMatchesDiff(diff2, this.#prefix, this.#indexName);
6842
+ return watcherMatchesDiff(diff3, this.#prefix, this.#indexName);
6870
6843
  }
6871
6844
  updateDeps(_keys, _scans) {
6872
6845
  }
6873
6846
  };
6874
- function convertDiffValues(diff2, convertKey) {
6875
- return diff2.map((op) => {
6847
+ function convertDiffValues(diff3, convertKey) {
6848
+ return diff3.map((op) => {
6876
6849
  const key = convertKey(op.key);
6877
6850
  switch (op.op) {
6878
6851
  case "add":
@@ -6991,23 +6964,23 @@ var SubscriptionsManagerImpl = class {
6991
6964
  return false;
6992
6965
  }
6993
6966
  };
6994
- function diffMatchesSubscription(keys, scans, indexName, diff2) {
6967
+ function diffMatchesSubscription(keys, scans, indexName, diff3) {
6995
6968
  if (indexName === "") {
6996
- for (const diffEntry of diff2) {
6969
+ for (const diffEntry of diff3) {
6997
6970
  if (keys.has(diffEntry.key)) {
6998
6971
  return true;
6999
6972
  }
7000
6973
  }
7001
6974
  }
7002
6975
  for (const scanInfo of scans) {
7003
- if (scanInfoMatchesDiff(scanInfo, indexName, diff2)) {
6976
+ if (scanInfoMatchesDiff(scanInfo, indexName, diff3)) {
7004
6977
  return true;
7005
6978
  }
7006
6979
  }
7007
6980
  return false;
7008
6981
  }
7009
- function scanInfoMatchesDiff(scanInfo, changeIndexName, diff2) {
7010
- for (const diffEntry of diff2) {
6982
+ function scanInfoMatchesDiff(scanInfo, changeIndexName, diff3) {
6983
+ for (const diffEntry of diff3) {
7011
6984
  if (scanInfoMatchesKey(scanInfo, changeIndexName, diffEntry.key)) {
7012
6985
  return true;
7013
6986
  }
@@ -7069,18 +7042,18 @@ function* subscriptionsForDiffs(subscriptions, diffs) {
7069
7042
  }
7070
7043
  }
7071
7044
  }
7072
- function watcherMatchesDiff(diff2, prefix, indexName) {
7045
+ function watcherMatchesDiff(diff3, prefix, indexName) {
7073
7046
  if (prefix === "") {
7074
7047
  return true;
7075
7048
  }
7076
7049
  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);
7050
+ const i = diffBinarySearch(diff3, prefix, compareKey);
7051
+ return i < diff3.length && compareKey(diff3[i]).startsWith(prefix);
7079
7052
  }
7080
- function diffBinarySearch(diff2, prefix, compareKey) {
7053
+ function diffBinarySearch(diff3, prefix, compareKey) {
7081
7054
  return binarySearch(
7082
- diff2.length,
7083
- (i) => compareUTF82(prefix, compareKey(diff2[i]))
7055
+ diff3.length,
7056
+ (i) => compareUTF82(prefix, compareKey(diff3[i]))
7084
7057
  );
7085
7058
  }
7086
7059
 
@@ -7832,7 +7805,7 @@ var ReplicacheImpl = class {
7832
7805
  *
7833
7806
  * @experimental This method is under development and its semantics will change.
7834
7807
  */
7835
- async poke(poke, pullApplied) {
7808
+ async poke(poke) {
7836
7809
  await this.#ready;
7837
7810
  const { clientID } = this;
7838
7811
  const requestID = newRequestID(clientID);
@@ -7856,7 +7829,6 @@ var ReplicacheImpl = class {
7856
7829
  );
7857
7830
  switch (result.type) {
7858
7831
  case Applied:
7859
- await pullApplied(this.memdag, result.syncHead, result.diffs);
7860
7832
  await this.maybeEndPull(result.syncHead, requestID);
7861
7833
  break;
7862
7834
  case CookieMismatch:
@@ -11053,13 +11025,16 @@ function unwrap(c) {
11053
11025
  return { type: c.type, conditions: flatten(c.type, c.conditions.map(unwrap)) };
11054
11026
  }
11055
11027
 
11028
+ // ../zql/src/query/query.ts
11029
+ var DEFAULT_TTL = 1e4;
11030
+
11056
11031
  // ../zql/src/query/query-impl.ts
11057
11032
  var astForTestingSymbol = Symbol();
11058
11033
  function newQuery(delegate, schema, table2) {
11059
11034
  return new QueryImpl(delegate, schema, table2);
11060
11035
  }
11061
- function newQueryWithDetails(delegate, schema, tableName, ast, ttl, format) {
11062
- return new QueryImpl(delegate, schema, tableName, ast, ttl, format);
11036
+ function newQueryWithDetails(delegate, schema, tableName, ast, format) {
11037
+ return new QueryImpl(delegate, schema, tableName, ast, format);
11063
11038
  }
11064
11039
  function staticParam(anchorClass, field) {
11065
11040
  return {
@@ -11074,14 +11049,12 @@ var AbstractQuery = class {
11074
11049
  #schema;
11075
11050
  #tableName;
11076
11051
  #ast;
11077
- #ttl;
11078
11052
  #format;
11079
11053
  #hash = "";
11080
- constructor(schema, tableName, ast = { table: tableName }, ttl, format) {
11054
+ constructor(schema, tableName, ast = { table: tableName }, format) {
11081
11055
  this.#schema = schema;
11082
11056
  this.#tableName = tableName;
11083
11057
  this.#ast = ast;
11084
- this.#ttl = ttl;
11085
11058
  this.#format = format ?? { singular: false, relationships: {} };
11086
11059
  }
11087
11060
  get format() {
@@ -11107,7 +11080,6 @@ var AbstractQuery = class {
11107
11080
  ...this.#ast,
11108
11081
  limit: 1
11109
11082
  },
11110
- this.#ttl,
11111
11083
  {
11112
11084
  ...this.#format,
11113
11085
  singular: true
@@ -11136,7 +11108,6 @@ var AbstractQuery = class {
11136
11108
  table: destSchema,
11137
11109
  alias: relationship
11138
11110
  },
11139
- this.#ttl,
11140
11111
  {
11141
11112
  relationships: {},
11142
11113
  singular: cardinality === "one"
@@ -11175,7 +11146,6 @@ var AbstractQuery = class {
11175
11146
  }
11176
11147
  ]
11177
11148
  },
11178
- this.#ttl,
11179
11149
  {
11180
11150
  ...this.#format,
11181
11151
  relationships: {
@@ -11198,7 +11168,6 @@ var AbstractQuery = class {
11198
11168
  table: destSchema,
11199
11169
  alias: relationship
11200
11170
  },
11201
- this.#ttl,
11202
11171
  {
11203
11172
  relationships: {},
11204
11173
  singular: secondRelation.cardinality === "one"
@@ -11247,7 +11216,6 @@ var AbstractQuery = class {
11247
11216
  }
11248
11217
  ]
11249
11218
  },
11250
- this.#ttl,
11251
11219
  {
11252
11220
  ...this.#format,
11253
11221
  relationships: {
@@ -11280,7 +11248,6 @@ var AbstractQuery = class {
11280
11248
  ...this.#ast,
11281
11249
  where: dnf(cond)
11282
11250
  },
11283
- this.#ttl,
11284
11251
  this.#format
11285
11252
  );
11286
11253
  }
@@ -11295,7 +11262,6 @@ var AbstractQuery = class {
11295
11262
  exclusive: !opts?.inclusive
11296
11263
  }
11297
11264
  },
11298
- this.#ttl,
11299
11265
  this.#format
11300
11266
  );
11301
11267
  }
@@ -11313,7 +11279,6 @@ var AbstractQuery = class {
11313
11279
  ...this.#ast,
11314
11280
  limit
11315
11281
  },
11316
- this.#ttl,
11317
11282
  this.#format
11318
11283
  );
11319
11284
  }
@@ -11325,7 +11290,6 @@ var AbstractQuery = class {
11325
11290
  ...this.#ast,
11326
11291
  orderBy: [...this.#ast.orderBy ?? [], [field, direction]]
11327
11292
  },
11328
- this.#ttl,
11329
11293
  this.#format
11330
11294
  );
11331
11295
  }
@@ -11344,7 +11308,6 @@ var AbstractQuery = class {
11344
11308
  table: destSchema,
11345
11309
  alias: `${SUBQ_PREFIX}${relationship}`
11346
11310
  },
11347
- this.#ttl,
11348
11311
  void 0
11349
11312
  )
11350
11313
  );
@@ -11381,7 +11344,6 @@ var AbstractQuery = class {
11381
11344
  table: destSchema,
11382
11345
  alias: `${SUBQ_PREFIX}${relationship}`
11383
11346
  },
11384
- this.#ttl,
11385
11347
  void 0
11386
11348
  )
11387
11349
  );
@@ -11459,29 +11421,27 @@ var AbstractQuery = class {
11459
11421
  var completedAstSymbol = Symbol();
11460
11422
  var QueryImpl = class extends AbstractQuery {
11461
11423
  #delegate;
11462
- constructor(delegate, schema, tableName, ast, ttl, format) {
11463
- super(schema, tableName, ast, ttl, format);
11424
+ constructor(delegate, schema, tableName, ast, format) {
11425
+ super(schema, tableName, ast, format);
11464
11426
  this.#delegate = delegate;
11465
11427
  }
11466
11428
  _system = "client";
11467
11429
  get [completedAstSymbol]() {
11468
11430
  return this._completeAst();
11469
11431
  }
11470
- _newQuery(schema, tableName, ast, ttl, format) {
11471
- return newQueryWithDetails(
11472
- this.#delegate,
11473
- schema,
11474
- tableName,
11475
- ast,
11476
- ttl,
11477
- format
11478
- );
11432
+ _newQuery(schema, tableName, ast, format) {
11433
+ return newQueryWithDetails(this.#delegate, schema, tableName, ast, format);
11479
11434
  }
11480
- materialize(factory) {
11435
+ materialize(factoryOrTTL, ttl = DEFAULT_TTL) {
11436
+ let factory;
11437
+ if (typeof factoryOrTTL === "function") {
11438
+ factory = factoryOrTTL;
11439
+ } else {
11440
+ ttl = factoryOrTTL ?? DEFAULT_TTL;
11441
+ }
11481
11442
  const ast = this._completeAst();
11482
11443
  const queryCompleteResolver = resolver7();
11483
11444
  let queryGot = false;
11484
- const ttl = void 0;
11485
11445
  const removeServerQuery = this.#delegate.addServerQuery(ast, ttl, (got) => {
11486
11446
  if (got) {
11487
11447
  queryGot = true;
@@ -11515,15 +11475,18 @@ var QueryImpl = class extends AbstractQuery {
11515
11475
  v2.destroy();
11516
11476
  return Promise.resolve(ret);
11517
11477
  }
11518
- preload() {
11478
+ preload(options) {
11519
11479
  const { resolve, promise: complete } = resolver7();
11520
11480
  const ast = this._completeAst();
11521
- const ttl = void 0;
11522
- const unsub = this.#delegate.addServerQuery(ast, ttl, (got) => {
11523
- if (got) {
11524
- resolve();
11481
+ const unsub = this.#delegate.addServerQuery(
11482
+ ast,
11483
+ options?.ttl ?? DEFAULT_TTL,
11484
+ (got) => {
11485
+ if (got) {
11486
+ resolve();
11487
+ }
11525
11488
  }
11526
- });
11489
+ );
11527
11490
  return {
11528
11491
  cleanup: unsub,
11529
11492
  complete
@@ -11569,8 +11532,8 @@ var StaticQuery = class _StaticQuery extends AbstractQuery {
11569
11532
  return new ExpressionBuilder(this._exists);
11570
11533
  }
11571
11534
  _system = "permissions";
11572
- _newQuery(schema, tableName, ast, ttl, format) {
11573
- return new _StaticQuery(schema, tableName, ast, ttl, format);
11535
+ _newQuery(schema, tableName, ast, format) {
11536
+ return new _StaticQuery(schema, tableName, ast, format);
11574
11537
  }
11575
11538
  get ast() {
11576
11539
  return this._completeAst();
@@ -13274,101 +13237,15 @@ function sourceNameFromKey(key) {
13274
13237
  return key.slice(ENTITIES_KEY_PREFIX.length, slash);
13275
13238
  }
13276
13239
 
13277
- // ../zero-client/src/client/ivm-source-repo.ts
13278
- var IVMSourceRepo = class {
13279
- #main;
13280
- #tables;
13281
- /**
13282
- * Sync is lazily created when the first response from the server is received.
13283
- */
13284
- #sync;
13285
- /**
13286
- * Rebase is created when the sync head is advanced and points to a fork
13287
- * of the sync head. This is used to rebase optimistic mutations.
13288
- */
13289
- #rebase;
13290
- constructor(tables) {
13291
- this.#main = new IVMSourceBranch(tables);
13292
- this.#tables = tables;
13293
- }
13294
- get main() {
13295
- return this.#main;
13296
- }
13297
- /**
13298
- * Used for reads in `zero.TransactionImpl`.
13299
- * Writes in `zero.TransactionImpl` also get applied to the rebase branch.
13300
- *
13301
- * The rebase branch is always forked off of the sync branch when a rebase begins.
13302
- */
13303
- get rebase() {
13304
- return must(this.#rebase, "rebase branch does not exist!");
13305
- }
13306
- advanceSyncHead = async (store, syncHeadHash, patches) => {
13307
- if (this.#sync === void 0) {
13308
- await withRead(store, async (dagRead) => {
13309
- const syncSources = new IVMSourceBranch(this.#tables);
13310
- const read = await readFromHash(
13311
- syncHeadHash,
13312
- dagRead,
13313
- Latest
13314
- );
13315
- for await (const entry of read.map.scan(ENTITIES_KEY_PREFIX)) {
13316
- if (!entry[0].startsWith(ENTITIES_KEY_PREFIX)) {
13317
- break;
13318
- }
13319
- const name = sourceNameFromKey(entry[0]);
13320
- const source = must(syncSources.getSource(name));
13321
- source.push({
13322
- type: "add",
13323
- row: entry[1]
13324
- });
13325
- }
13326
- this.#sync = syncSources;
13327
- });
13328
- } else {
13329
- for (const patch of patches) {
13330
- if (patch.op === "clear") {
13331
- this.#sync.clear();
13332
- continue;
13333
- }
13334
- const { key } = patch;
13335
- if (!key.startsWith(ENTITIES_KEY_PREFIX)) {
13336
- continue;
13337
- }
13338
- const name = sourceNameFromKey(key);
13339
- const source = must(this.#sync.getSource(name));
13340
- switch (patch.op) {
13341
- case "del":
13342
- source.push({
13343
- type: "remove",
13344
- row: patch.oldValue
13345
- });
13346
- break;
13347
- case "add":
13348
- source.push({
13349
- type: "add",
13350
- row: patch.newValue
13351
- });
13352
- break;
13353
- case "change":
13354
- source.push({
13355
- type: "edit",
13356
- row: patch.newValue,
13357
- oldRow: patch.oldValue
13358
- });
13359
- break;
13360
- }
13361
- }
13362
- }
13363
- this.#rebase = must(this.#sync).fork();
13364
- };
13365
- };
13240
+ // ../zero-client/src/client/ivm-branch.ts
13366
13241
  var IVMSourceBranch = class _IVMSourceBranch {
13367
13242
  #sources;
13368
13243
  #tables;
13369
- constructor(tables, sources = /* @__PURE__ */ new Map()) {
13244
+ hash;
13245
+ constructor(tables, hash2, sources = /* @__PURE__ */ new Map()) {
13370
13246
  this.#tables = tables;
13371
13247
  this.#sources = sources;
13248
+ this.hash = hash2;
13372
13249
  }
13373
13250
  getSource(name) {
13374
13251
  if (this.#sources.has(name)) {
@@ -13382,20 +13259,46 @@ var IVMSourceBranch = class _IVMSourceBranch {
13382
13259
  clear() {
13383
13260
  this.#sources.clear();
13384
13261
  }
13262
+ /**
13263
+ * Mutates the current branch, advancing it to the new head
13264
+ * by applying the given diffs.
13265
+ */
13266
+ advance(expectedHead, newHead, diffs) {
13267
+ assert(
13268
+ this.hash === expectedHead,
13269
+ () => `Expected head must match the main head. Got: ${this.hash}, expected: ${expectedHead}`
13270
+ );
13271
+ applyDiffs(diffs, this);
13272
+ this.hash = newHead;
13273
+ }
13274
+ /**
13275
+ * Fork the branch and patch it up to match the desired head.
13276
+ */
13277
+ async forkToHead(store, expectedHead, desiredHead) {
13278
+ const fork = this.fork();
13279
+ assert(
13280
+ expectedHead === fork.hash,
13281
+ () => `Expected head must match the main head. Got: ${expectedHead}, expected: ${fork.hash}`
13282
+ );
13283
+ if (fork.hash === desiredHead) {
13284
+ return fork;
13285
+ }
13286
+ await patchBranch(desiredHead, store, fork);
13287
+ fork.hash = desiredHead;
13288
+ return fork;
13289
+ }
13385
13290
  /**
13386
13291
  * Creates a new IVMSourceBranch that is a copy of the current one.
13387
13292
  * This is a cheap operation since the b-trees are shared until a write is performed
13388
13293
  * and then only the modified nodes are copied.
13389
13294
  *
13390
- * This is used when:
13391
- * 1. We need to rebase a change. We fork the `sync` branch and run the mutations against the fork.
13392
- * 2. We need to create `main` at startup.
13393
- * 3. We need to create a new `sync` head because we got a new server snapshot.
13394
- * The old `sync` head is forked and the new server snapshot is applied to the fork.
13295
+ * IVM branches are forked when we need to rebase mutations.
13296
+ * The mutations modify the fork rather than original branch.
13395
13297
  */
13396
13298
  fork() {
13397
13299
  return new _IVMSourceBranch(
13398
13300
  this.#tables,
13301
+ this.hash,
13399
13302
  new Map(
13400
13303
  wrapIterable(this.#sources.entries()).map(([name, source]) => [
13401
13304
  name,
@@ -13405,6 +13308,61 @@ var IVMSourceBranch = class _IVMSourceBranch {
13405
13308
  );
13406
13309
  }
13407
13310
  };
13311
+ async function patchBranch(desiredHead, store, fork) {
13312
+ const diffs = await computeDiffs(must(fork.hash), desiredHead, store);
13313
+ if (!diffs) {
13314
+ return;
13315
+ }
13316
+ applyDiffs(diffs, fork);
13317
+ }
13318
+ async function computeDiffs(startHash, endHash, store) {
13319
+ const readFn = (dagRead) => diff2(
13320
+ startHash,
13321
+ endHash,
13322
+ dagRead,
13323
+ {
13324
+ shouldComputeDiffs: () => true,
13325
+ shouldComputeDiffsForIndex(_name) {
13326
+ return false;
13327
+ }
13328
+ },
13329
+ Latest
13330
+ );
13331
+ const diffs = await withRead(store, readFn);
13332
+ return diffs.get("");
13333
+ }
13334
+ function applyDiffs(diffs, branch) {
13335
+ for (let i = diffBinarySearch(diffs, ENTITIES_KEY_PREFIX, (diff3) => diff3.key); i < diffs.length; i++) {
13336
+ const diff3 = diffs[i];
13337
+ const { key } = diff3;
13338
+ if (!key.startsWith(ENTITIES_KEY_PREFIX)) {
13339
+ break;
13340
+ }
13341
+ const name = sourceNameFromKey(key);
13342
+ const source = must(branch.getSource(name));
13343
+ switch (diff3.op) {
13344
+ case "del":
13345
+ source.push({
13346
+ type: "remove",
13347
+ row: diff3.oldValue
13348
+ });
13349
+ break;
13350
+ case "add":
13351
+ source.push({
13352
+ type: "add",
13353
+ row: diff3.newValue
13354
+ });
13355
+ break;
13356
+ case "change":
13357
+ source.push({
13358
+ type: "edit",
13359
+ row: diff3.newValue,
13360
+ oldRow: diff3.oldValue
13361
+ });
13362
+ break;
13363
+ }
13364
+ }
13365
+ }
13408
13366
 
13409
13367
  // ../zero-client/src/client/context.ts
13410
13368
  var ZeroContext = class {
@@ -13453,40 +13411,40 @@ var ZeroContext = class {
13453
13411
  processChanges(changes) {
13454
13412
  this.batchViewUpdates(() => {
13455
13413
  try {
13456
- for (const diff2 of changes) {
13457
- const { key } = diff2;
13414
+ for (const diff3 of changes) {
13415
+ const { key } = diff3;
13458
13416
  assert(key.startsWith(ENTITIES_KEY_PREFIX));
13459
13417
  const name = sourceNameFromKey(key);
13460
13418
  const source = this.getSource(name);
13461
13419
  if (!source) {
13462
13420
  continue;
13463
13421
  }
13464
- switch (diff2.op) {
13422
+ switch (diff3.op) {
13465
13423
  case "del":
13466
- assert(typeof diff2.oldValue === "object");
13424
+ assert(typeof diff3.oldValue === "object");
13467
13425
  source.push({
13468
13426
  type: "remove",
13469
- row: diff2.oldValue
13427
+ row: diff3.oldValue
13470
13428
  });
13471
13429
  break;
13472
13430
  case "add":
13473
- assert(typeof diff2.newValue === "object");
13431
+ assert(typeof diff3.newValue === "object");
13474
13432
  source.push({
13475
13433
  type: "add",
13476
- row: diff2.newValue
13434
+ row: diff3.newValue
13477
13435
  });
13478
13436
  break;
13479
13437
  case "change":
13480
- assert(typeof diff2.newValue === "object");
13481
- assert(typeof diff2.oldValue === "object");
13438
+ assert(typeof diff3.newValue === "object");
13439
+ assert(typeof diff3.oldValue === "object");
13482
13440
  source.push({
13483
13441
  type: "edit",
13484
- row: diff2.newValue,
13485
- oldRow: diff2.oldValue
13442
+ row: diff3.newValue,
13443
+ oldRow: diff3.oldValue
13486
13444
  });
13487
13445
  break;
13488
13446
  default:
13489
- unreachable(diff2);
13447
+ unreachable(diff3);
13490
13448
  }
13491
13449
  }
13492
13450
  } finally {
@@ -13722,23 +13680,13 @@ async function deleteImpl(tx, arg, schema, ivmBranch) {
13722
13680
 
13723
13681
  // ../zero-client/src/client/custom.ts
13724
13682
  var TransactionImpl = class {
13725
- constructor(repTx, schema, ivmSourceRepo) {
13683
+ constructor(repTx, schema) {
13726
13684
  must(repTx.reason === "initial" || repTx.reason === "rebase");
13727
13685
  this.clientID = repTx.clientID;
13728
13686
  this.mutationID = repTx.mutationID;
13729
13687
  this.reason = repTx.reason === "initial" ? "optimistic" : "rebase";
13730
- this.mutate = makeSchemaCRUD(
13731
- schema,
13732
- repTx,
13733
- // Mutators do not write to the main IVM sources during optimistic mutations
13734
- // so we pass undefined here.
13735
- // ExperimentalWatch handles updating main.
13736
- this.reason === "optimistic" ? void 0 : ivmSourceRepo.rebase
13737
- );
13738
- this.query = makeSchemaQuery(
13739
- schema,
13740
- this.reason === "optimistic" ? ivmSourceRepo.main : ivmSourceRepo.rebase
13741
- );
13688
+ this.mutate = makeSchemaCRUD(schema, repTx);
13689
+ this.query = {};
13742
13690
  }
13743
13691
  clientID;
13744
13692
  mutationID;
@@ -13747,33 +13695,20 @@ var TransactionImpl = class {
13747
13695
  mutate;
13748
13696
  query;
13749
13697
  };
13750
- function makeReplicacheMutator(mutator, schema, ivmSourceRepo) {
13698
+ function makeReplicacheMutator(mutator, schema) {
13751
13699
  return (repTx, args) => {
13752
- const tx = new TransactionImpl(repTx, schema, ivmSourceRepo);
13700
+ const tx = new TransactionImpl(repTx, schema);
13753
13701
  return mutator(tx, args);
13754
13702
  };
13755
13703
  }
13756
- function makeSchemaQuery(schema, ivmBranch) {
13757
- const rv = {};
13758
- const context = new ZeroContext(
13759
- ivmBranch,
13760
- () => () => {
13761
- },
13762
- (applyViewUpdates) => applyViewUpdates()
13763
- );
13764
- for (const name of Object.keys(schema.tables)) {
13765
- rv[name] = newQuery(context, schema, name);
13766
- }
13767
- return rv;
13768
- }
13769
- function makeSchemaCRUD(schema, tx, ivmBranch) {
13704
+ function makeSchemaCRUD(schema, tx) {
13770
13705
  const mutate = {};
13771
13706
  for (const [name] of Object.entries(schema.tables)) {
13772
- mutate[name] = makeTableCRUD(schema, name, tx, ivmBranch);
13707
+ mutate[name] = makeTableCRUD(schema, name, tx);
13773
13708
  }
13774
13709
  return mutate;
13775
13710
  }
13776
- function makeTableCRUD(schema, tableName, tx, ivmBranch) {
13711
+ function makeTableCRUD(schema, tableName, tx) {
13777
13712
  const table2 = must(schema.tables[tableName]);
13778
13713
  const { primaryKey } = table2;
13779
13714
  return {
@@ -13781,25 +13716,25 @@ function makeTableCRUD(schema, tableName, tx, ivmBranch) {
13781
13716
  tx,
13782
13717
  { op: "insert", tableName, primaryKey, value },
13783
13718
  schema,
13784
- ivmBranch
13719
+ void 0
13785
13720
  ),
13786
13721
  upsert: (value) => upsertImpl(
13787
13722
  tx,
13788
13723
  { op: "upsert", tableName, primaryKey, value },
13789
13724
  schema,
13790
- ivmBranch
13725
+ void 0
13791
13726
  ),
13792
13727
  update: (value) => updateImpl(
13793
13728
  tx,
13794
13729
  { op: "update", tableName, primaryKey, value },
13795
13730
  schema,
13796
- ivmBranch
13731
+ void 0
13797
13732
  ),
13798
13733
  delete: (id) => deleteImpl(
13799
13734
  tx,
13800
13735
  { op: "delete", tableName, primaryKey, value: id },
13801
13736
  schema,
13802
- ivmBranch
13737
+ void 0
13803
13738
  )
13804
13739
  };
13805
13740
  }
@@ -14103,7 +14038,7 @@ function makeMessage(message, context, logLevel) {
14103
14038
  }
14104
14039
 
14105
14040
  // ../zero-client/src/client/version.ts
14106
- var version2 = "0.17.2025030300";
14041
+ var version2 = "0.17.2025030601";
14107
14042
 
14108
14043
  // ../zero-client/src/client/log-options.ts
14109
14044
  var LevelFilterLogSink = class {
@@ -14416,8 +14351,8 @@ var QueryManager = class {
14416
14351
  this.#recentQueriesMaxSize = recentQueriesMaxSize;
14417
14352
  this.#send = send2;
14418
14353
  experimentalWatch(
14419
- (diff2) => {
14420
- for (const diffOp of diff2) {
14354
+ (diff3) => {
14355
+ for (const diffOp of diff3) {
14421
14356
  const queryHash = diffOp.key.substring(GOT_QUERIES_KEY_PREFIX.length);
14422
14357
  switch (diffOp.op) {
14423
14358
  case "add":
@@ -15056,6 +14991,7 @@ function onClientStateNotFoundServerReason(serverErrMsg) {
15056
14991
  return `Server could not find state needed to synchronize this client. ${serverErrMsg}`;
15057
14992
  }
15058
14993
  var ON_CLIENT_STATE_NOT_FOUND_REASON_CLIENT = "The local persistent state needed to synchronize this client has been garbage collected.";
14994
+ var CLOSE_CODE_NORMAL = 1e3;
15059
14995
  var Zero = class {
15060
14996
  version = version2;
15061
14997
  #rep;
@@ -15067,7 +15003,7 @@ var Zero = class {
15067
15003
  #enableAnalytics;
15068
15004
  #pokeHandler;
15069
15005
  #queryManager;
15070
- #ivmSources;
15006
+ #ivmMain;
15071
15007
  #clientToServer;
15072
15008
  #deleteClientsManager;
15073
15009
  /**
@@ -15199,14 +15135,14 @@ var Zero = class {
15199
15135
  const replicacheMutators = {
15200
15136
  [CRUD_MUTATION_NAME]: makeCRUDMutator(schema)
15201
15137
  };
15202
- this.#ivmSources = new IVMSourceRepo(schema.tables);
15138
+ this.#ivmMain = new IVMSourceBranch(schema.tables);
15203
15139
  for (const [namespace, mutatorsForNamespace] of Object.entries(
15204
15140
  options.mutators ?? {}
15205
15141
  )) {
15206
15142
  for (const [name, mutator] of Object.entries(
15207
15143
  mutatorsForNamespace
15208
15144
  )) {
15209
- replicacheMutators[customMutatorKey(namespace, name)] = makeReplicacheMutator(mutator, schema, this.#ivmSources);
15145
+ replicacheMutators[customMutatorKey(namespace, name)] = makeReplicacheMutator(mutator, schema);
15210
15146
  }
15211
15147
  }
15212
15148
  this.storageKey = storageKey ?? "";
@@ -15300,12 +15236,12 @@ var Zero = class {
15300
15236
  this.#lc
15301
15237
  );
15302
15238
  this.#zeroContext = new ZeroContext(
15303
- this.#ivmSources.main,
15239
+ this.#ivmMain,
15304
15240
  (ast, ttl, gotCallback) => this.#queryManager.add(ast, ttl, gotCallback),
15305
15241
  batchViewUpdates
15306
15242
  );
15307
15243
  rep.experimentalWatch(
15308
- (diff2) => this.#zeroContext.processChanges(diff2),
15244
+ (diff3) => this.#zeroContext.processChanges(diff3),
15309
15245
  {
15310
15246
  prefix: ENTITIES_KEY_PREFIX,
15311
15247
  initialValuesInFirstDiff: true
@@ -15322,7 +15258,7 @@ var Zero = class {
15322
15258
  });
15323
15259
  this.#metrics.tags.push(`version:${this.version}`);
15324
15260
  this.#pokeHandler = new PokeHandler(
15325
- (poke) => this.#rep.poke(poke, this.#ivmSources.advanceSyncHead),
15261
+ (poke) => this.#rep.poke(poke),
15326
15262
  () => this.#onPokeError(),
15327
15263
  rep.clientID,
15328
15264
  schema,
@@ -15449,16 +15385,39 @@ var Zero = class {
15449
15385
  */
15450
15386
  close() {
15451
15387
  const lc = this.#lc.withContext("close");
15388
+ lc.debug?.("Closing Zero instance. Stack:", new Error().stack);
15452
15389
  if (this.#connectionState !== Disconnected) {
15453
- this.#disconnect(lc, {
15454
- client: "ClientClosed"
15455
- });
15390
+ let closeReason = JSON.stringify([
15391
+ "closeConnection",
15392
+ []
15393
+ ]);
15394
+ if (closeReason.length > 123) {
15395
+ lc.warn?.("Close reason is too long. Removing it.");
15396
+ closeReason = "";
15397
+ }
15398
+ this.#disconnect(
15399
+ lc,
15400
+ {
15401
+ client: "ClientClosed"
15402
+ },
15403
+ CLOSE_CODE_NORMAL,
15404
+ closeReason
15405
+ );
15456
15406
  }
15457
15407
  lc.debug?.("Aborting closeAbortController due to close()");
15458
15408
  this.#closeAbortController.abort();
15459
15409
  this.#metrics.stop();
15460
15410
  return this.#rep.close();
15461
15411
  }
15412
+ #onPageHide = (e) => {
15413
+ if (e.persisted) {
15414
+ this.#lc.debug?.("Ignoring pagehide event because it was persisted");
15415
+ } else {
15416
+ this.#lc.debug?.("Closing client because we got a clean close");
15417
+ this.close().catch(() => {
15418
+ });
15419
+ }
15420
+ };
15462
15421
  #onMessage = (e) => {
15463
15422
  const lc = this.#lc;
15464
15423
  lc.debug?.("received message", e.data);
@@ -15716,6 +15675,7 @@ var Zero = class {
15716
15675
  ws.addEventListener("close", this.#onClose);
15717
15676
  this.#socket = ws;
15718
15677
  this.#socketResolver.resolve(ws);
15678
+ getBrowserGlobal("window")?.addEventListener("pagehide", this.#onPageHide);
15719
15679
  try {
15720
15680
  l.debug?.("Waiting for connection to be acknowledged");
15721
15681
  await this.#connectResolver.promise;
@@ -15727,7 +15687,7 @@ var Zero = class {
15727
15687
  );
15728
15688
  }
15729
15689
  }
15730
- #disconnect(l, reason) {
15690
+ #disconnect(l, reason, closeCode, closeReason) {
15731
15691
  if (this.#connectionState === Connecting) {
15732
15692
  this.#connectErrorCount++;
15733
15693
  }
@@ -15781,10 +15741,14 @@ var Zero = class {
15781
15741
  this.#socket?.removeEventListener("message", this.#onMessage);
15782
15742
  this.#socket?.removeEventListener("open", this.#onOpen);
15783
15743
  this.#socket?.removeEventListener("close", this.#onClose);
15784
- this.#socket?.close();
15744
+ this.#socket?.close(closeCode, closeReason);
15785
15745
  this.#socket = void 0;
15786
15746
  this.#lastMutationIDSent = NULL_LAST_MUTATION_ID_SENT;
15787
15747
  this.#pokeHandler.handleDisconnect();
15748
+ getBrowserGlobal("window")?.removeEventListener(
15749
+ "pagehide",
15750
+ this.#onPageHide
15751
+ );
15788
15752
  }
15789
15753
  #handlePokeStart(_lc, pokeMessage) {
15790
15754
  resetBackoff();
@@ -16278,4 +16242,4 @@ export {
16278
16242
  escapeLike,
16279
16243
  Zero
16280
16244
  };
16281
- //# sourceMappingURL=chunk-BBNRRMOA.js.map
16245
+ //# sourceMappingURL=chunk-EENFDWT4.js.map