@rpcbase/client 0.411.0 → 0.413.0

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.
@@ -7,6 +7,7 @@ var RtsSsrRuntimeContext = createContext(null);
7
7
  var hydrationDataStore = /* @__PURE__ */ new Map();
8
8
  var hydrationPageInfoStore = /* @__PURE__ */ new Map();
9
9
  var hydrationTotalCountStore = /* @__PURE__ */ new Map();
10
+ var hydrationCountStore = /* @__PURE__ */ new Map();
10
11
  var makeStoreKey = (modelName, queryKey) => `${modelName}.${queryKey}`;
11
12
  var normalizeStringOrNull = (value) => {
12
13
  if (typeof value !== "string") return null;
@@ -36,6 +37,7 @@ var parseHydrationData = (value) => {
36
37
  const raw = value;
37
38
  if (raw.v !== 1) return null;
38
39
  if (!Array.isArray(raw.queries)) return null;
40
+ const rawCounts = Array.isArray(raw.counts) ? raw.counts : [];
39
41
  const queries = [];
40
42
  for (const entry of raw.queries) {
41
43
  if (!entry || typeof entry !== "object") continue;
@@ -54,11 +56,26 @@ var parseHydrationData = (value) => {
54
56
  ...totalCount !== void 0 ? { totalCount } : {}
55
57
  });
56
58
  }
59
+ const counts = [];
60
+ for (const entry of rawCounts) {
61
+ if (!entry || typeof entry !== "object") continue;
62
+ const countEntry = entry;
63
+ const modelName = normalizeStringOrNull(countEntry.modelName);
64
+ const queryKey = normalizeStringOrNull(countEntry.queryKey);
65
+ const count = normalizeTotalCount$2(countEntry.count);
66
+ if (!modelName || !queryKey || count === void 0) continue;
67
+ counts.push({
68
+ modelName,
69
+ queryKey,
70
+ count
71
+ });
72
+ }
57
73
  return {
58
74
  v: 1,
59
75
  tenantId: normalizeStringOrNull(raw.tenantId),
60
76
  uid: normalizeStringOrNull(raw.uid),
61
- queries
77
+ queries,
78
+ counts
62
79
  };
63
80
  };
64
81
  var hydrateRtsFromWindow = () => {
@@ -71,11 +88,13 @@ var hydrateRtsFromWindow = () => {
71
88
  hydrationDataStore.clear();
72
89
  hydrationPageInfoStore.clear();
73
90
  hydrationTotalCountStore.clear();
91
+ hydrationCountStore.clear();
74
92
  for (const query of parsed.queries) {
75
93
  hydrationDataStore.set(makeStoreKey(query.modelName, query.queryKey), query.data);
76
94
  hydrationPageInfoStore.set(makeStoreKey(query.modelName, query.queryKey), query.pageInfo);
77
95
  hydrationTotalCountStore.set(makeStoreKey(query.modelName, query.queryKey), query.totalCount);
78
96
  }
97
+ for (const count of parsed.counts) hydrationCountStore.set(makeStoreKey(count.modelName, count.queryKey), count.count);
79
98
  };
80
99
  var peekHydratedRtsQueryData = (modelName, queryKey) => {
81
100
  return hydrationDataStore.get(makeStoreKey(modelName, queryKey));
@@ -86,16 +105,23 @@ var peekHydratedRtsQueryPageInfo = (modelName, queryKey) => {
86
105
  var peekHydratedRtsQueryTotalCount = (modelName, queryKey) => {
87
106
  return hydrationTotalCountStore.get(makeStoreKey(modelName, queryKey));
88
107
  };
108
+ var peekHydratedRtsCount = (modelName, queryKey) => {
109
+ return hydrationCountStore.get(makeStoreKey(modelName, queryKey));
110
+ };
89
111
  var consumeHydratedRtsQueryData = (modelName, queryKey) => {
90
112
  const key = makeStoreKey(modelName, queryKey);
91
113
  hydrationDataStore.delete(key);
92
114
  hydrationPageInfoStore.delete(key);
93
115
  hydrationTotalCountStore.delete(key);
94
116
  };
117
+ var consumeHydratedRtsCount = (modelName, queryKey) => {
118
+ hydrationCountStore.delete(makeStoreKey(modelName, queryKey));
119
+ };
95
120
  var clearHydratedRtsQueryData = () => {
96
121
  hydrationDataStore.clear();
97
122
  hydrationPageInfoStore.clear();
98
123
  hydrationTotalCountStore.clear();
124
+ hydrationCountStore.clear();
99
125
  };
100
126
  var RtsSsrRuntimeProvider = (t0) => {
101
127
  const $ = c(3);
@@ -969,6 +995,7 @@ var MAX_TXN_BUF = 2048;
969
995
  var SERVER_RECONNECT_DELAY_MIN_MS = 1e4;
970
996
  var SERVER_RECONNECT_DELAY_MAX_MS = 15e3;
971
997
  var RUN_NETWORK_QUERY_TIMEOUT_ERROR = "runNetworkQuery: request timed out";
998
+ var RUN_NETWORK_COUNT_TIMEOUT_ERROR = "runNetworkCount: request timed out";
972
999
  var socket = null;
973
1000
  var connectPromise = null;
974
1001
  var explicitDisconnect = false;
@@ -977,7 +1004,9 @@ var currentUid = null;
977
1004
  var connectOptions = {};
978
1005
  var localTxnBuf = [];
979
1006
  var queryCallbacks = /* @__PURE__ */ new Map();
1007
+ var countCallbacks = /* @__PURE__ */ new Map();
980
1008
  var subscriptions = /* @__PURE__ */ new Map();
1009
+ var countSubscriptions = /* @__PURE__ */ new Map();
981
1010
  var messageCallbacks = /* @__PURE__ */ new Map();
982
1011
  var reconnectTimer = null;
983
1012
  var reconnectAttempts = 0;
@@ -985,6 +1014,9 @@ var hasEstablishedConnection = false;
985
1014
  var pendingServerReconnectJitter = false;
986
1015
  var syncPromise = null;
987
1016
  var syncKey = null;
1017
+ var connectionStatus = "idle";
1018
+ var connectionError = null;
1019
+ var connectionStatusCallbacks = /* @__PURE__ */ new Set();
988
1020
  var ensureRealtimeRuntime = () => {
989
1021
  if (typeof WebSocket !== "function") throw new Error("RTS websocket client requires WebSocket support");
990
1022
  if (typeof globalThis.setTimeout !== "function" || typeof globalThis.clearTimeout !== "function") throw new Error("RTS websocket client requires timer support");
@@ -1004,6 +1036,11 @@ var setRuntimeTimeout = (handler, delayMs) => {
1004
1036
  var clearRuntimeTimeout = (timer) => {
1005
1037
  globalThis.clearTimeout(timer);
1006
1038
  };
1039
+ var setConnectionStatus = (status, error = null) => {
1040
+ connectionStatus = status;
1041
+ connectionError = error;
1042
+ for (const callback of connectionStatusCallbacks) callback(status, error);
1043
+ };
1007
1044
  var resolveWebSocketUrlFromCandidate = (candidateUrl, options) => {
1008
1045
  const url = new URL(candidateUrl);
1009
1046
  if (url.protocol === "http:") url.protocol = "ws:";
@@ -1068,6 +1105,17 @@ var resubscribeAll = ({ forceInitialQuery }) => {
1068
1105
  runInitialQuery
1069
1106
  });
1070
1107
  }
1108
+ for (const sub of countSubscriptions.values()) {
1109
+ const runInitialQuery = forceInitialQuery || sub.runInitialNetworkQuery;
1110
+ sendToServer({
1111
+ type: "register-count",
1112
+ modelName: sub.modelName,
1113
+ queryKey: sub.queryKey,
1114
+ query: sub.query,
1115
+ options: sub.options,
1116
+ runInitialQuery
1117
+ });
1118
+ }
1071
1119
  };
1072
1120
  var clearReconnectTimer = () => {
1073
1121
  if (reconnectTimer === null) return;
@@ -1158,6 +1206,23 @@ var handleQueryPayload = (payload) => {
1158
1206
  }
1159
1207
  updateDocs(modelName, docs, currentUid).catch(() => {});
1160
1208
  };
1209
+ var handleCountPayload = (payload) => {
1210
+ const { modelName, queryKey, count, error, txnId } = payload;
1211
+ const cbKey = `${modelName}.${queryKey}`;
1212
+ const callbacks = countCallbacks.get(cbKey);
1213
+ if (!callbacks || !callbacks.size) return;
1214
+ const context = {
1215
+ source: "network",
1216
+ isLocal: !!(txnId && localTxnBuf.includes(txnId)),
1217
+ txnId
1218
+ };
1219
+ if (error) {
1220
+ for (const cb of callbacks) cb(error, void 0, context);
1221
+ return;
1222
+ }
1223
+ const normalizedCount = normalizeTotalCount$1(count);
1224
+ for (const cb of callbacks) cb(null, normalizedCount, context);
1225
+ };
1161
1226
  var handleEvent = (payload) => {
1162
1227
  const callbacks = messageCallbacks.get(payload.event);
1163
1228
  if (!callbacks || !callbacks.size) return;
@@ -1176,6 +1241,10 @@ var handleMessage = (event) => {
1176
1241
  handleQueryPayload(message);
1177
1242
  return;
1178
1243
  }
1244
+ if (message.type === "count-payload") {
1245
+ handleCountPayload(message);
1246
+ return;
1247
+ }
1179
1248
  if (message.type === "event") handleEvent(message);
1180
1249
  };
1181
1250
  var addLocalTxn = (txnId) => {
@@ -1317,12 +1386,14 @@ var connectInternal = (tenantId, uid, options, { resetReconnectAttempts }) => {
1317
1386
  if (resetReconnectAttempts) reconnectAttempts = 0;
1318
1387
  let opened = false;
1319
1388
  let settled = false;
1389
+ setConnectionStatus("connecting");
1320
1390
  socket = new WebSocket(url);
1321
1391
  socket.addEventListener("open", () => {
1322
1392
  opened = true;
1323
1393
  settled = true;
1324
1394
  reconnectAttempts = 0;
1325
1395
  pendingServerReconnectJitter = false;
1396
+ setConnectionStatus("connected");
1326
1397
  resubscribeAll({ forceInitialQuery: hasEstablishedConnection });
1327
1398
  hasEstablishedConnection = true;
1328
1399
  resolve();
@@ -1331,9 +1402,14 @@ var connectInternal = (tenantId, uid, options, { resetReconnectAttempts }) => {
1331
1402
  socket.addEventListener("close", (event) => {
1332
1403
  if (!opened && !settled) {
1333
1404
  settled = true;
1334
- reject(/* @__PURE__ */ new Error(`RTS WebSocket closed before opening (code=${event.code})`));
1405
+ const error = /* @__PURE__ */ new Error(`RTS WebSocket closed before opening (code=${event.code})`);
1406
+ setConnectionStatus("error", error);
1407
+ reject(error);
1335
1408
  }
1336
- if (!explicitDisconnect) pendingServerReconnectJitter = opened;
1409
+ if (!explicitDisconnect) {
1410
+ pendingServerReconnectJitter = opened;
1411
+ setConnectionStatus("connecting", connectionError);
1412
+ } else setConnectionStatus("idle");
1337
1413
  socket = null;
1338
1414
  connectPromise = null;
1339
1415
  scheduleReconnect();
@@ -1341,7 +1417,9 @@ var connectInternal = (tenantId, uid, options, { resetReconnectAttempts }) => {
1341
1417
  socket.addEventListener("error", (err) => {
1342
1418
  if (settled) return;
1343
1419
  settled = true;
1344
- reject(err instanceof Error ? err : /* @__PURE__ */ new Error("RTS WebSocket error"));
1420
+ const error = err instanceof Error ? err : /* @__PURE__ */ new Error("RTS WebSocket error");
1421
+ setConnectionStatus("error", error);
1422
+ reject(error);
1345
1423
  });
1346
1424
  });
1347
1425
  return connectPromise;
@@ -1359,6 +1437,7 @@ var disconnect = () => {
1359
1437
  } catch {}
1360
1438
  socket = null;
1361
1439
  connectPromise = null;
1440
+ setConnectionStatus("idle");
1362
1441
  };
1363
1442
  var reconnect = (tenantId, uid, options = {}) => {
1364
1443
  explicitDisconnect = true;
@@ -1371,6 +1450,18 @@ var reconnect = (tenantId, uid, options = {}) => {
1371
1450
  connectPromise = null;
1372
1451
  return connect(tenantId, uid, options);
1373
1452
  };
1453
+ var getConnectionStatus = () => {
1454
+ return connectionStatus;
1455
+ };
1456
+ var getConnectionError = () => {
1457
+ return connectionError;
1458
+ };
1459
+ var onConnectionStatusChange = (callback) => {
1460
+ connectionStatusCallbacks.add(callback);
1461
+ return () => {
1462
+ connectionStatusCallbacks.delete(callback);
1463
+ };
1464
+ };
1374
1465
  var registerQuery = (modelName, query, optionsOrCallback, callbackMaybe, behavior) => {
1375
1466
  let options;
1376
1467
  let callback;
@@ -1452,6 +1543,53 @@ var registerQuery = (modelName, query, optionsOrCallback, callbackMaybe, behavio
1452
1543
  }
1453
1544
  };
1454
1545
  };
1546
+ var registerCount = (modelName, query, optionsOrCallback, callbackMaybe, behavior) => {
1547
+ let options;
1548
+ let callback;
1549
+ if (typeof optionsOrCallback === "function") {
1550
+ options = {};
1551
+ callback = optionsOrCallback;
1552
+ } else {
1553
+ options = optionsOrCallback ?? {};
1554
+ callback = callbackMaybe;
1555
+ }
1556
+ if (!callback) return void 0;
1557
+ if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("registerCount: modelName must be a non-empty string");
1558
+ const queryKey = computeRtsQueryKey(query, options);
1559
+ const cbKey = `${modelName}.${queryKey}`;
1560
+ const runInitialNetworkQuery = behavior?.runInitialNetworkQuery !== false;
1561
+ const set = countCallbacks.get(cbKey) ?? /* @__PURE__ */ new Set();
1562
+ set.add(callback);
1563
+ countCallbacks.set(cbKey, set);
1564
+ countSubscriptions.set(cbKey, {
1565
+ modelName,
1566
+ query,
1567
+ options,
1568
+ queryKey,
1569
+ runInitialNetworkQuery
1570
+ });
1571
+ sendToServer({
1572
+ type: "register-count",
1573
+ modelName,
1574
+ queryKey,
1575
+ query,
1576
+ options,
1577
+ runInitialQuery: runInitialNetworkQuery
1578
+ });
1579
+ return () => {
1580
+ const callbacks = countCallbacks.get(cbKey);
1581
+ callbacks?.delete(callback);
1582
+ if (callbacks && callbacks.size === 0) {
1583
+ countCallbacks.delete(cbKey);
1584
+ countSubscriptions.delete(cbKey);
1585
+ sendToServer({
1586
+ type: "remove-count",
1587
+ modelName,
1588
+ queryKey
1589
+ });
1590
+ }
1591
+ };
1592
+ };
1455
1593
  var makeRunQueryKey = () => `run-query.${Date.now().toString(36)}.${Math.random().toString(36).slice(2, 10)}`;
1456
1594
  var runNetworkQuery = async ({ modelName, query, options = {}, timeoutMs = 1e4 }) => {
1457
1595
  if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("runNetworkQuery: modelName must be a non-empty string");
@@ -1466,7 +1604,7 @@ var runNetworkQuery = async ({ modelName, query, options = {}, timeoutMs = 1e4 }
1466
1604
  const connectAttempt = connectInternal(currentTenantId, currentUid, connectOptions, { resetReconnectAttempts: false });
1467
1605
  if (hasTimeout) await new Promise((resolve, reject) => {
1468
1606
  const timeoutId = setRuntimeTimeout(() => {
1469
- reject(new Error(RUN_NETWORK_QUERY_TIMEOUT_ERROR));
1607
+ reject(new Error(RUN_NETWORK_COUNT_TIMEOUT_ERROR));
1470
1608
  }, timeoutMs);
1471
1609
  connectAttempt.then(() => {
1472
1610
  clearRuntimeTimeout(timeoutId);
@@ -1531,6 +1669,84 @@ var runNetworkQuery = async ({ modelName, query, options = {}, timeoutMs = 1e4 }
1531
1669
  });
1532
1670
  });
1533
1671
  };
1672
+ var runNetworkCount = async ({ modelName, query, options = {}, timeoutMs = 1e4 }) => {
1673
+ if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("runNetworkCount: modelName must be a non-empty string");
1674
+ const hasTimeout = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0;
1675
+ const timeoutStartedAt = hasTimeout ? Date.now() : 0;
1676
+ if (!socket || socket.readyState !== WebSocket.OPEN) {
1677
+ if (currentTenantId && currentUid) try {
1678
+ const connectAttempt = connectInternal(currentTenantId, currentUid, connectOptions, { resetReconnectAttempts: false });
1679
+ if (hasTimeout) await new Promise((resolve, reject) => {
1680
+ const timeoutId = setRuntimeTimeout(() => {
1681
+ reject(new Error(RUN_NETWORK_QUERY_TIMEOUT_ERROR));
1682
+ }, timeoutMs);
1683
+ connectAttempt.then(() => {
1684
+ clearRuntimeTimeout(timeoutId);
1685
+ resolve();
1686
+ }, (error) => {
1687
+ clearRuntimeTimeout(timeoutId);
1688
+ reject(error);
1689
+ });
1690
+ });
1691
+ else await connectAttempt;
1692
+ } catch {}
1693
+ }
1694
+ if (!socket || socket.readyState !== WebSocket.OPEN) {
1695
+ if (hasTimeout && Date.now() - timeoutStartedAt >= timeoutMs) throw new Error(RUN_NETWORK_COUNT_TIMEOUT_ERROR);
1696
+ throw new Error("runNetworkCount: RTS socket is not connected");
1697
+ }
1698
+ const remainingTimeoutMs = hasTimeout ? Math.max(0, timeoutMs - (Date.now() - timeoutStartedAt)) : null;
1699
+ if (remainingTimeoutMs !== null && remainingTimeoutMs <= 0) throw new Error(RUN_NETWORK_COUNT_TIMEOUT_ERROR);
1700
+ const resolvedOptions = options.key ? options : {
1701
+ ...options,
1702
+ key: makeRunQueryKey()
1703
+ };
1704
+ const queryKey = computeRtsQueryKey(query, resolvedOptions);
1705
+ const cbKey = `${modelName}.${queryKey}`;
1706
+ return await new Promise((resolve, reject) => {
1707
+ let settled = false;
1708
+ let timeoutId = null;
1709
+ const cleanup = () => {
1710
+ const callbacks = countCallbacks.get(cbKey);
1711
+ callbacks?.delete(callback);
1712
+ if (callbacks && callbacks.size === 0) countCallbacks.delete(cbKey);
1713
+ if (timeoutId !== null) clearRuntimeTimeout(timeoutId);
1714
+ };
1715
+ const settle = (next) => {
1716
+ if (settled) return;
1717
+ settled = true;
1718
+ cleanup();
1719
+ next();
1720
+ };
1721
+ const callback = (error, count, context) => {
1722
+ if (error) {
1723
+ settle(() => reject(typeof error === "string" ? new Error(error) : error));
1724
+ return;
1725
+ }
1726
+ if (count === void 0) {
1727
+ settle(() => reject(/* @__PURE__ */ new Error("runNetworkCount: invalid count payload")));
1728
+ return;
1729
+ }
1730
+ settle(() => resolve({
1731
+ count,
1732
+ context
1733
+ }));
1734
+ };
1735
+ const set = countCallbacks.get(cbKey) ?? /* @__PURE__ */ new Set();
1736
+ set.add(callback);
1737
+ countCallbacks.set(cbKey, set);
1738
+ if (remainingTimeoutMs !== null) timeoutId = setRuntimeTimeout(() => {
1739
+ settle(() => reject(new Error(RUN_NETWORK_COUNT_TIMEOUT_ERROR)));
1740
+ }, remainingTimeoutMs);
1741
+ sendToServer({
1742
+ type: "run-count",
1743
+ modelName,
1744
+ queryKey,
1745
+ query,
1746
+ options: resolvedOptions
1747
+ });
1748
+ });
1749
+ };
1534
1750
  var sendMessage = (event, payload) => {
1535
1751
  sendToServer({
1536
1752
  type: "event",
@@ -1549,6 +1765,205 @@ var onMessage = (event, callback) => {
1549
1765
  };
1550
1766
  };
1551
1767
  //#endregion
1768
+ //#region src/rts/useCountQuery.ts
1769
+ var normalizeCount = (value) => {
1770
+ if (typeof value !== "number") return void 0;
1771
+ if (!Number.isFinite(value) || value < 0) return void 0;
1772
+ return Math.floor(value);
1773
+ };
1774
+ var useCountQuery = (modelName, t0, t1) => {
1775
+ const $ = c(53);
1776
+ const query = t0 === void 0 ? {} : t0;
1777
+ let t2;
1778
+ if ($[0] !== t1) {
1779
+ t2 = t1 === void 0 ? {} : t1;
1780
+ $[0] = t1;
1781
+ $[1] = t2;
1782
+ } else t2 = $[1];
1783
+ const options = t2;
1784
+ if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("useCountQuery: modelName must be a non-empty string");
1785
+ const id = useId();
1786
+ const enabled = options.enabled ?? true;
1787
+ const ssrEnabled = options.ssr !== false;
1788
+ const refreshOnMount = options.refreshOnMount === true;
1789
+ const key = options.key ?? id;
1790
+ const queryJson = JSON.stringify(query);
1791
+ let t3;
1792
+ if ($[2] !== key) {
1793
+ t3 = { key };
1794
+ $[2] = key;
1795
+ $[3] = t3;
1796
+ } else t3 = $[3];
1797
+ const runtimeOptions = t3;
1798
+ const queryKey = computeRtsQueryKey(query, runtimeOptions);
1799
+ const ssrRuntime = useRtsSsrRuntime();
1800
+ if (enabled && ssrEnabled && ssrRuntime) ssrRuntime.registerCount({
1801
+ modelName,
1802
+ query,
1803
+ options: runtimeOptions,
1804
+ queryKey
1805
+ });
1806
+ let t4;
1807
+ if ($[4] !== enabled || $[5] !== modelName || $[6] !== queryKey || $[7] !== ssrEnabled || $[8] !== ssrRuntime) {
1808
+ t4 = enabled && ssrEnabled ? ssrRuntime ? ssrRuntime.getCount(modelName, queryKey) : peekHydratedRtsCount(modelName, queryKey) : void 0;
1809
+ $[4] = enabled;
1810
+ $[5] = modelName;
1811
+ $[6] = queryKey;
1812
+ $[7] = ssrEnabled;
1813
+ $[8] = ssrRuntime;
1814
+ $[9] = t4;
1815
+ } else t4 = $[9];
1816
+ const seedCountRaw = t4;
1817
+ let t5;
1818
+ if ($[10] !== seedCountRaw) {
1819
+ t5 = normalizeCount(seedCountRaw);
1820
+ $[10] = seedCountRaw;
1821
+ $[11] = t5;
1822
+ } else t5 = $[11];
1823
+ const seedCount = t5;
1824
+ const seedCountStr = seedCount !== void 0 ? String(seedCount) : "";
1825
+ const hasSeedCount = seedCount !== void 0;
1826
+ let t6;
1827
+ if ($[12] !== seedCount) {
1828
+ t6 = () => seedCount;
1829
+ $[12] = seedCount;
1830
+ $[13] = t6;
1831
+ } else t6 = $[13];
1832
+ const [count, setCount] = useState(t6);
1833
+ let t7;
1834
+ if ($[14] !== hasSeedCount) {
1835
+ t7 = () => hasSeedCount ? "cache" : void 0;
1836
+ $[14] = hasSeedCount;
1837
+ $[15] = t7;
1838
+ } else t7 = $[15];
1839
+ const [source, setSource] = useState(t7);
1840
+ const [error, setError] = useState(void 0);
1841
+ const [loading, setLoading] = useState(enabled && !hasSeedCount);
1842
+ const lastCountRef = useRef(seedCount);
1843
+ let t8;
1844
+ if ($[16] !== enabled || $[17] !== hasSeedCount || $[18] !== modelName || $[19] !== queryKey || $[20] !== seedCount || $[21] !== ssrEnabled || $[22] !== ssrRuntime) {
1845
+ t8 = () => {
1846
+ if (!ssrRuntime && enabled && ssrEnabled && hasSeedCount) consumeHydratedRtsCount(modelName, queryKey);
1847
+ lastCountRef.current = seedCount;
1848
+ setError(void 0);
1849
+ if (!enabled) {
1850
+ setCount(void 0);
1851
+ setSource(void 0);
1852
+ setLoading(false);
1853
+ return;
1854
+ }
1855
+ if (hasSeedCount) {
1856
+ setCount(seedCount);
1857
+ setSource("cache");
1858
+ setLoading(false);
1859
+ return;
1860
+ }
1861
+ setCount(void 0);
1862
+ setSource(void 0);
1863
+ setLoading(true);
1864
+ };
1865
+ $[16] = enabled;
1866
+ $[17] = hasSeedCount;
1867
+ $[18] = modelName;
1868
+ $[19] = queryKey;
1869
+ $[20] = seedCount;
1870
+ $[21] = ssrEnabled;
1871
+ $[22] = ssrRuntime;
1872
+ $[23] = t8;
1873
+ } else t8 = $[23];
1874
+ let t9;
1875
+ if ($[24] !== enabled || $[25] !== hasSeedCount || $[26] !== modelName || $[27] !== queryKey || $[28] !== seedCount || $[29] !== seedCountStr || $[30] !== ssrEnabled || $[31] !== ssrRuntime) {
1876
+ t9 = [
1877
+ enabled,
1878
+ ssrEnabled,
1879
+ ssrRuntime,
1880
+ modelName,
1881
+ queryKey,
1882
+ hasSeedCount,
1883
+ seedCount,
1884
+ seedCountStr
1885
+ ];
1886
+ $[24] = enabled;
1887
+ $[25] = hasSeedCount;
1888
+ $[26] = modelName;
1889
+ $[27] = queryKey;
1890
+ $[28] = seedCount;
1891
+ $[29] = seedCountStr;
1892
+ $[30] = ssrEnabled;
1893
+ $[31] = ssrRuntime;
1894
+ $[32] = t9;
1895
+ } else t9 = $[32];
1896
+ useEffect(t8, t9);
1897
+ let t10;
1898
+ if ($[33] !== enabled || $[34] !== hasSeedCount || $[35] !== modelName || $[36] !== query || $[37] !== refreshOnMount || $[38] !== runtimeOptions) {
1899
+ t10 = () => {
1900
+ if (!enabled) return;
1901
+ const unsubscribe = registerCount(modelName, query, runtimeOptions, (err, nextCount, context) => {
1902
+ setLoading(false);
1903
+ if (err) {
1904
+ setError(err);
1905
+ return;
1906
+ }
1907
+ if (nextCount === void 0) return;
1908
+ setError(void 0);
1909
+ if (lastCountRef.current === nextCount) {
1910
+ setSource(context.source);
1911
+ return;
1912
+ }
1913
+ lastCountRef.current = nextCount;
1914
+ setCount(nextCount);
1915
+ setSource(context.source);
1916
+ }, { runInitialNetworkQuery: refreshOnMount || !hasSeedCount });
1917
+ return () => {
1918
+ unsubscribe?.();
1919
+ };
1920
+ };
1921
+ $[33] = enabled;
1922
+ $[34] = hasSeedCount;
1923
+ $[35] = modelName;
1924
+ $[36] = query;
1925
+ $[37] = refreshOnMount;
1926
+ $[38] = runtimeOptions;
1927
+ $[39] = t10;
1928
+ } else t10 = $[39];
1929
+ let t11;
1930
+ if ($[40] !== enabled || $[41] !== hasSeedCount || $[42] !== modelName || $[43] !== queryJson || $[44] !== queryKey || $[45] !== refreshOnMount || $[46] !== runtimeOptions) {
1931
+ t11 = [
1932
+ enabled,
1933
+ modelName,
1934
+ queryJson,
1935
+ queryKey,
1936
+ runtimeOptions,
1937
+ hasSeedCount,
1938
+ refreshOnMount
1939
+ ];
1940
+ $[40] = enabled;
1941
+ $[41] = hasSeedCount;
1942
+ $[42] = modelName;
1943
+ $[43] = queryJson;
1944
+ $[44] = queryKey;
1945
+ $[45] = refreshOnMount;
1946
+ $[46] = runtimeOptions;
1947
+ $[47] = t11;
1948
+ } else t11 = $[47];
1949
+ useEffect(t10, t11);
1950
+ let t12;
1951
+ if ($[48] !== count || $[49] !== error || $[50] !== loading || $[51] !== source) {
1952
+ t12 = {
1953
+ count,
1954
+ source,
1955
+ error,
1956
+ loading
1957
+ };
1958
+ $[48] = count;
1959
+ $[49] = error;
1960
+ $[50] = loading;
1961
+ $[51] = source;
1962
+ $[52] = t12;
1963
+ } else t12 = $[52];
1964
+ return t12;
1965
+ };
1966
+ //#endregion
1552
1967
  //#region src/rts/useQuery.ts
1553
1968
  var normalizePageInfo = (value) => {
1554
1969
  if (!value || typeof value !== "object") return void 0;
@@ -1986,6 +2401,6 @@ var useQuery = (modelName, query = {}, options = {}) => {
1986
2401
  ]);
1987
2402
  };
1988
2403
  //#endregion
1989
- export { peekHydratedRtsQueryData as C, hydrateRtsFromWindow as S, peekHydratedRtsQueryTotalCount as T, runQuery as _, onMessage as a, STATIC_RPCBASE_RTS_HYDRATION_DATA_KEY as b, runNetworkQuery as c, configureRtsPouchStore as d, deleteDocs as f, resetRtsPouchStore as g, getCollection as h, disconnect as i, sendMessage as l, destroyCollection as m, addLocalTxn as n, reconnect as o, destroyAllCollections as p, connect as r, registerQuery as s, useQuery as t, syncRtsChanges as u, updateDocs as v, peekHydratedRtsQueryPageInfo as w, clearHydratedRtsQueryData as x, RtsSsrRuntimeProvider as y };
2404
+ export { peekHydratedRtsCount as A, runQuery as C, clearHydratedRtsQueryData as D, STATIC_RPCBASE_RTS_HYDRATION_DATA_KEY as E, peekHydratedRtsQueryPageInfo as M, peekHydratedRtsQueryTotalCount as N, consumeHydratedRtsCount as O, resetRtsPouchStore as S, RtsSsrRuntimeProvider as T, configureRtsPouchStore as _, disconnect as a, destroyCollection as b, onConnectionStatusChange as c, registerCount as d, registerQuery as f, syncRtsChanges as g, sendMessage as h, connect as i, peekHydratedRtsQueryData as j, hydrateRtsFromWindow as k, onMessage as l, runNetworkQuery as m, useCountQuery as n, getConnectionError as o, runNetworkCount as p, addLocalTxn as r, getConnectionStatus as s, useQuery as t, reconnect as u, deleteDocs as v, updateDocs as w, getCollection as x, destroyAllCollections as y };
1990
2405
 
1991
- //# sourceMappingURL=rts-C9dMrcRZ.js.map
2406
+ //# sourceMappingURL=rts-rSMRh4Xw.js.map