@rpcbase/client 0.415.0 → 0.417.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.
@@ -1,2406 +0,0 @@
1
- import { c } from "react/compiler-runtime";
2
- import { createContext, useContext, useEffect, useId, useMemo, useRef, useState } from "react";
3
- import { jsx } from "react/jsx-runtime";
4
- //#region src/rts/ssrHydration.tsx
5
- var STATIC_RPCBASE_RTS_HYDRATION_DATA_KEY = "__staticRpcbaseRtsHydrationData";
6
- var RtsSsrRuntimeContext = createContext(null);
7
- var hydrationDataStore = /* @__PURE__ */ new Map();
8
- var hydrationPageInfoStore = /* @__PURE__ */ new Map();
9
- var hydrationTotalCountStore = /* @__PURE__ */ new Map();
10
- var hydrationCountStore = /* @__PURE__ */ new Map();
11
- var makeStoreKey = (modelName, queryKey) => `${modelName}.${queryKey}`;
12
- var normalizeStringOrNull = (value) => {
13
- if (typeof value !== "string") return null;
14
- const normalized = value.trim();
15
- return normalized ? normalized : null;
16
- };
17
- var normalizePageInfo$2 = (value) => {
18
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
19
- const raw = value;
20
- if (typeof raw.hasNextPage !== "boolean" || typeof raw.hasPrevPage !== "boolean") return void 0;
21
- const nextCursor = typeof raw.nextCursor === "string" && raw.nextCursor ? raw.nextCursor : void 0;
22
- const prevCursor = typeof raw.prevCursor === "string" && raw.prevCursor ? raw.prevCursor : void 0;
23
- return {
24
- hasNextPage: raw.hasNextPage,
25
- hasPrevPage: raw.hasPrevPage,
26
- ...nextCursor ? { nextCursor } : {},
27
- ...prevCursor ? { prevCursor } : {}
28
- };
29
- };
30
- var normalizeTotalCount$2 = (value) => {
31
- if (typeof value !== "number") return void 0;
32
- if (!Number.isFinite(value) || value < 0) return void 0;
33
- return Math.floor(value);
34
- };
35
- var parseHydrationData = (value) => {
36
- if (!value || typeof value !== "object") return null;
37
- const raw = value;
38
- if (raw.v !== 1) return null;
39
- if (!Array.isArray(raw.queries)) return null;
40
- const rawCounts = Array.isArray(raw.counts) ? raw.counts : [];
41
- const queries = [];
42
- for (const entry of raw.queries) {
43
- if (!entry || typeof entry !== "object") continue;
44
- const query = entry;
45
- const modelName = normalizeStringOrNull(query.modelName);
46
- const queryKey = normalizeStringOrNull(query.queryKey);
47
- if (!modelName || !queryKey) continue;
48
- if (!Array.isArray(query.data)) continue;
49
- const pageInfo = normalizePageInfo$2(query.pageInfo);
50
- const totalCount = normalizeTotalCount$2(query.totalCount);
51
- queries.push({
52
- modelName,
53
- queryKey,
54
- data: query.data,
55
- ...pageInfo ? { pageInfo } : {},
56
- ...totalCount !== void 0 ? { totalCount } : {}
57
- });
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
- }
73
- return {
74
- v: 1,
75
- tenantId: normalizeStringOrNull(raw.tenantId),
76
- uid: normalizeStringOrNull(raw.uid),
77
- queries,
78
- counts
79
- };
80
- };
81
- var hydrateRtsFromWindow = () => {
82
- if (typeof window === "undefined") return;
83
- const browserWindow = window;
84
- const raw = browserWindow[STATIC_RPCBASE_RTS_HYDRATION_DATA_KEY];
85
- delete browserWindow[STATIC_RPCBASE_RTS_HYDRATION_DATA_KEY];
86
- const parsed = parseHydrationData(raw);
87
- if (!parsed) return;
88
- hydrationDataStore.clear();
89
- hydrationPageInfoStore.clear();
90
- hydrationTotalCountStore.clear();
91
- hydrationCountStore.clear();
92
- for (const query of parsed.queries) {
93
- hydrationDataStore.set(makeStoreKey(query.modelName, query.queryKey), query.data);
94
- hydrationPageInfoStore.set(makeStoreKey(query.modelName, query.queryKey), query.pageInfo);
95
- hydrationTotalCountStore.set(makeStoreKey(query.modelName, query.queryKey), query.totalCount);
96
- }
97
- for (const count of parsed.counts) hydrationCountStore.set(makeStoreKey(count.modelName, count.queryKey), count.count);
98
- };
99
- var peekHydratedRtsQueryData = (modelName, queryKey) => {
100
- return hydrationDataStore.get(makeStoreKey(modelName, queryKey));
101
- };
102
- var peekHydratedRtsQueryPageInfo = (modelName, queryKey) => {
103
- return hydrationPageInfoStore.get(makeStoreKey(modelName, queryKey));
104
- };
105
- var peekHydratedRtsQueryTotalCount = (modelName, queryKey) => {
106
- return hydrationTotalCountStore.get(makeStoreKey(modelName, queryKey));
107
- };
108
- var peekHydratedRtsCount = (modelName, queryKey) => {
109
- return hydrationCountStore.get(makeStoreKey(modelName, queryKey));
110
- };
111
- var consumeHydratedRtsQueryData = (modelName, queryKey) => {
112
- const key = makeStoreKey(modelName, queryKey);
113
- hydrationDataStore.delete(key);
114
- hydrationPageInfoStore.delete(key);
115
- hydrationTotalCountStore.delete(key);
116
- };
117
- var consumeHydratedRtsCount = (modelName, queryKey) => {
118
- hydrationCountStore.delete(makeStoreKey(modelName, queryKey));
119
- };
120
- var clearHydratedRtsQueryData = () => {
121
- hydrationDataStore.clear();
122
- hydrationPageInfoStore.clear();
123
- hydrationTotalCountStore.clear();
124
- hydrationCountStore.clear();
125
- };
126
- var RtsSsrRuntimeProvider = (t0) => {
127
- const $ = c(3);
128
- const { value, children } = t0;
129
- let t1;
130
- if ($[0] !== children || $[1] !== value) {
131
- t1 = /* @__PURE__ */ jsx(RtsSsrRuntimeContext.Provider, {
132
- value,
133
- children
134
- });
135
- $[0] = children;
136
- $[1] = value;
137
- $[2] = t1;
138
- } else t1 = $[2];
139
- return t1;
140
- };
141
- var useRtsSsrRuntime = () => {
142
- return useContext(RtsSsrRuntimeContext);
143
- };
144
- //#endregion
145
- //#region src/rts/runtimeStorage.ts
146
- var memoryStore = /* @__PURE__ */ new Map();
147
- var reactNativeStorage = null;
148
- var MMKV_STORAGE_ID = "rpcbase-rts";
149
- var memoryStorage = {
150
- getItem: (key) => memoryStore.get(key) ?? null,
151
- setItem: (key, value) => {
152
- memoryStore.set(key, String(value));
153
- },
154
- removeItem: (key) => {
155
- memoryStore.delete(key);
156
- }
157
- };
158
- var asRuntimeStorage = (value) => {
159
- if (!value || typeof value !== "object") return null;
160
- const candidate = value;
161
- if (typeof candidate.getItem !== "function" || typeof candidate.setItem !== "function" || typeof candidate.removeItem !== "function") return null;
162
- return {
163
- getItem: candidate.getItem.bind(value),
164
- setItem: candidate.setItem.bind(value),
165
- removeItem: candidate.removeItem.bind(value)
166
- };
167
- };
168
- var isReactNativeRuntime = () => {
169
- if (typeof navigator === "undefined") return false;
170
- return navigator.product === "ReactNative";
171
- };
172
- var getRuntimeRequire = () => {
173
- const globalRequire = globalThis.require;
174
- if (typeof globalRequire === "function") return globalRequire;
175
- try {
176
- return (0, eval)("require");
177
- } catch {
178
- return null;
179
- }
180
- };
181
- var getReactNativeStorage = () => {
182
- if (reactNativeStorage) return reactNativeStorage;
183
- const runtimeRequire = getRuntimeRequire();
184
- if (!runtimeRequire) throw new Error("RTS storage: react-native-mmkv is required in React Native runtime");
185
- let mmkvModule;
186
- try {
187
- mmkvModule = runtimeRequire("react-native-mmkv");
188
- } catch (error) {
189
- const runtimeError = error instanceof Error ? ` ${error.message}` : "";
190
- throw new Error(`RTS storage: react-native-mmkv is required in React Native runtime.${runtimeError}`);
191
- }
192
- const MMKV = mmkvModule.MMKV;
193
- if (typeof MMKV !== "function") throw new Error("RTS storage: invalid react-native-mmkv module shape");
194
- const mmkv = new MMKV({ id: MMKV_STORAGE_ID });
195
- reactNativeStorage = {
196
- getItem: (key) => {
197
- const value = mmkv.getString(key);
198
- return typeof value === "string" ? value : null;
199
- },
200
- setItem: (key, value) => {
201
- mmkv.set(key, String(value));
202
- },
203
- removeItem: (key) => {
204
- mmkv.delete(key);
205
- }
206
- };
207
- return reactNativeStorage;
208
- };
209
- var getRuntimeStorage = () => {
210
- if (isReactNativeRuntime()) return getReactNativeStorage();
211
- const direct = asRuntimeStorage(globalThis.localStorage);
212
- if (direct) return direct;
213
- const windowStorage = asRuntimeStorage(globalThis.window?.localStorage);
214
- if (windowStorage) return windowStorage;
215
- return memoryStorage;
216
- };
217
- //#endregion
218
- //#region src/rts/pouchStore.ts
219
- var UNDERSCORE_PREFIX = "$_";
220
- var DEFAULT_FIND_LIMIT = 4096;
221
- var INDEXED_DB_ADAPTER = "indexeddb";
222
- var REACT_NATIVE_SQLITE_ADAPTER = "react-native-sqlite";
223
- var storeConfig = null;
224
- var pouchDbPromise = null;
225
- var lastAppliedPrefix = null;
226
- var activePouchAdapter = INDEXED_DB_ADAPTER;
227
- var collections = /* @__PURE__ */ new Map();
228
- var dbNamesByPrefix = /* @__PURE__ */ new Map();
229
- var unwrapDefault = (mod) => {
230
- if (!mod || typeof mod !== "object") return mod;
231
- return mod.default ?? mod;
232
- };
233
- var computeBasePrefix = ({ tenantId, appName }) => {
234
- let prefix = "rb/";
235
- if (appName) prefix += `${appName}/`;
236
- prefix += `${tenantId}/`;
237
- return prefix;
238
- };
239
- var getDbNamesKey = (prefix) => `rb:rts:pouchDbs:${prefix}`;
240
- var getPrefixOverrideKey = ({ tenantId, appName }) => `rb:rts:pouchPrefix:${appName ?? ""}:${tenantId}`;
241
- var readPrefixOverride = ({ tenantId, appName }) => {
242
- const storage = getRuntimeStorage();
243
- try {
244
- const value = storage.getItem(getPrefixOverrideKey({
245
- tenantId,
246
- appName
247
- }));
248
- if (!value) return null;
249
- if (!value.endsWith("/")) return `${value}/`;
250
- return value;
251
- } catch {
252
- return null;
253
- }
254
- };
255
- var getPrefix = () => {
256
- if (!storeConfig) throw new Error("RTS PouchDB store is not configured");
257
- if (storeConfig.prefix) return storeConfig.prefix;
258
- const basePrefix = computeBasePrefix({
259
- tenantId: storeConfig.tenantId,
260
- appName: storeConfig.appName
261
- });
262
- return readPrefixOverride({
263
- tenantId: storeConfig.tenantId,
264
- appName: storeConfig.appName
265
- }) ?? basePrefix;
266
- };
267
- var loadDbNames = (prefix) => {
268
- const existing = dbNamesByPrefix.get(prefix);
269
- if (existing) return existing;
270
- const names = /* @__PURE__ */ new Set();
271
- const storage = getRuntimeStorage();
272
- try {
273
- const raw = storage.getItem(getDbNamesKey(prefix));
274
- if (raw) {
275
- const parsed = JSON.parse(raw);
276
- if (Array.isArray(parsed)) {
277
- for (const value of parsed) if (typeof value === "string" && value) names.add(value);
278
- }
279
- }
280
- } catch {
281
- return names;
282
- }
283
- dbNamesByPrefix.set(prefix, names);
284
- return names;
285
- };
286
- var persistDbNames = (prefix, names) => {
287
- const storage = getRuntimeStorage();
288
- try {
289
- if (!names.size) {
290
- storage.removeItem(getDbNamesKey(prefix));
291
- dbNamesByPrefix.delete(prefix);
292
- return;
293
- }
294
- storage.setItem(getDbNamesKey(prefix), JSON.stringify(Array.from(names)));
295
- dbNamesByPrefix.set(prefix, names);
296
- } catch {
297
- return;
298
- }
299
- };
300
- var registerDbName = (prefix, dbName) => {
301
- if (!prefix || !dbName) return;
302
- const names = loadDbNames(prefix);
303
- if (names.has(dbName)) return;
304
- names.add(dbName);
305
- persistDbNames(prefix, names);
306
- };
307
- var unregisterDbName = (prefix, dbName) => {
308
- if (!prefix || !dbName) return;
309
- const names = loadDbNames(prefix);
310
- if (!names.delete(dbName)) return;
311
- persistDbNames(prefix, names);
312
- };
313
- var getPouchDb = async () => {
314
- if (!pouchDbPromise) pouchDbPromise = (async () => {
315
- const [core, findPlugin] = await Promise.all([import("pouchdb-core"), import("pouchdb-find")]);
316
- const PouchDB = unwrapDefault(core);
317
- if (isReactNativeRuntime()) {
318
- const moduleName = "pouchdb-adapter-react-native-sqlite";
319
- let sqliteAdapterModule;
320
- try {
321
- sqliteAdapterModule = await import(
322
- /* @vite-ignore */
323
- moduleName
324
- );
325
- } catch (error) {
326
- const runtimeError = error instanceof Error ? ` ${error.message}` : "";
327
- throw new Error(`RTS PouchDB: missing react-native sqlite adapter. Install \`pouchdb-adapter-react-native-sqlite\` in the app.${runtimeError}`);
328
- }
329
- PouchDB.plugin(unwrapDefault(sqliteAdapterModule));
330
- activePouchAdapter = REACT_NATIVE_SQLITE_ADAPTER;
331
- } else {
332
- const indexedDbAdapter = await import("pouchdb-adapter-indexeddb");
333
- PouchDB.plugin(unwrapDefault(indexedDbAdapter));
334
- activePouchAdapter = INDEXED_DB_ADAPTER;
335
- }
336
- PouchDB.plugin(unwrapDefault(findPlugin));
337
- return PouchDB;
338
- })();
339
- return pouchDbPromise;
340
- };
341
- var applyPrefix = (PouchDB) => {
342
- const prefix = getPrefix();
343
- if (prefix === lastAppliedPrefix) return;
344
- PouchDB.prefix = prefix;
345
- lastAppliedPrefix = prefix;
346
- };
347
- var configureRtsPouchStore = (config) => {
348
- storeConfig = config;
349
- lastAppliedPrefix = null;
350
- collections.clear();
351
- };
352
- var getCollection = async (modelName, options) => {
353
- const PouchDB = await getPouchDb();
354
- applyPrefix(PouchDB);
355
- const prefix = getPrefix();
356
- const dbName = `${options.uid}/${modelName}`;
357
- const dbKey = `${prefix}${dbName}`;
358
- const existing = collections.get(dbKey);
359
- if (existing) return existing;
360
- registerDbName(prefix, dbName);
361
- const db = new PouchDB(dbName, {
362
- adapter: activePouchAdapter,
363
- revs_limit: 1
364
- });
365
- collections.set(dbKey, db);
366
- return db;
367
- };
368
- var replaceQueryKeys = (value, replaceKey) => {
369
- if (typeof value !== "object" || value === null) return value;
370
- if (Array.isArray(value)) return value.map((item) => replaceQueryKeys(item, replaceKey));
371
- const obj = value;
372
- const next = Object.create(Object.getPrototypeOf(obj));
373
- for (const key of Object.keys(obj)) {
374
- if (/^\$/.test(key) || /\.\d+$/.test(key)) throw new Error(`replaceQueryKeys: Unexpected key format: ${key}`);
375
- const newKey = replaceKey(key);
376
- next[newKey] = replaceQueryKeys(obj[key], replaceKey);
377
- }
378
- return next;
379
- };
380
- var getKeys = (obj, parentKey = "") => {
381
- const keys = [];
382
- for (const key of Object.keys(obj)) {
383
- const nextKey = parentKey ? `${parentKey}.${key}` : key;
384
- const value = obj[key];
385
- if (typeof value === "object" && value !== null && !Array.isArray(value)) keys.push(...getKeys(value, nextKey));
386
- else keys.push(nextKey);
387
- }
388
- return keys;
389
- };
390
- var satisfiesProjection = (doc, projection) => {
391
- const docKeys = new Set(getKeys(doc));
392
- const projectionKeys = new Set(Object.keys(projection).filter((key) => projection[key] === 1));
393
- if (!projectionKeys.has("_id")) docKeys.delete("_id");
394
- if (projectionKeys.size > docKeys.size) return false;
395
- for (const key of projectionKeys) if (!docKeys.has(key)) return false;
396
- return true;
397
- };
398
- var getValueAtPath = (doc, path) => {
399
- const parts = path.split(".").map((part) => part.trim()).filter(Boolean);
400
- if (!parts.length) return void 0;
401
- let current = doc;
402
- for (const part of parts) {
403
- if (!current || typeof current !== "object" || Array.isArray(current)) return void 0;
404
- current = current[part];
405
- }
406
- return current;
407
- };
408
- var setValueAtPath = (doc, path, value) => {
409
- const parts = path.split(".").map((part) => part.trim()).filter(Boolean);
410
- if (!parts.length) return;
411
- let current = doc;
412
- for (let i = 0; i < parts.length - 1; i += 1) {
413
- const key = parts[i];
414
- const existing = current[key];
415
- if (!existing || typeof existing !== "object" || Array.isArray(existing)) current[key] = {};
416
- current = current[key];
417
- }
418
- current[parts[parts.length - 1]] = value;
419
- };
420
- var unsetValueAtPath = (doc, path) => {
421
- const parts = path.split(".").map((part) => part.trim()).filter(Boolean);
422
- if (!parts.length) return;
423
- let current = doc;
424
- for (let i = 0; i < parts.length - 1; i += 1) {
425
- const key = parts[i];
426
- const next = current[key];
427
- if (!next || typeof next !== "object" || Array.isArray(next)) return;
428
- current = next;
429
- }
430
- delete current[parts[parts.length - 1]];
431
- };
432
- var cloneDoc = (doc) => {
433
- try {
434
- return structuredClone(doc);
435
- } catch {
436
- return JSON.parse(JSON.stringify(doc));
437
- }
438
- };
439
- var toProjectionSpec = (projection) => {
440
- const spec = {};
441
- for (const [key, value] of Object.entries(projection)) {
442
- const path = key.trim();
443
- if (!path) continue;
444
- if (value === 1 || value === 0) spec[path] = value;
445
- }
446
- return spec;
447
- };
448
- var applyProjection = (doc, projection) => {
449
- const spec = toProjectionSpec(projection);
450
- const includeKeys = Object.keys(spec).filter((key) => spec[key] === 1);
451
- const excludeKeys = Object.keys(spec).filter((key) => spec[key] === 0);
452
- if (includeKeys.length > 0) {
453
- const projected = {};
454
- for (const key of includeKeys) {
455
- const value = getValueAtPath(doc, key);
456
- if (value !== void 0) setValueAtPath(projected, key, value);
457
- }
458
- if (spec._id !== 0 && Object.hasOwn(doc, "_id")) projected._id = doc._id;
459
- return projected;
460
- }
461
- const projected = cloneDoc(doc);
462
- for (const key of excludeKeys) unsetValueAtPath(projected, key);
463
- return projected;
464
- };
465
- var compareSort = (a, b, sort) => {
466
- for (const key of Object.keys(sort)) {
467
- const dir = sort[key];
468
- const aVal = getValueAtPath(a, key);
469
- const bVal = getValueAtPath(b, key);
470
- if (typeof aVal === "number" && typeof bVal === "number") {
471
- if (aVal < bVal) return -1 * dir;
472
- if (aVal > bVal) return 1 * dir;
473
- continue;
474
- }
475
- if (typeof aVal === "string" && typeof bVal === "string") {
476
- if (aVal < bVal) return -1 * dir;
477
- if (aVal > bVal) return 1 * dir;
478
- continue;
479
- }
480
- }
481
- return 0;
482
- };
483
- var extractDocId = (value) => {
484
- if (typeof value === "string") return value;
485
- if (!value || typeof value !== "object" || Array.isArray(value)) return "";
486
- const id = value._id;
487
- return typeof id === "string" ? id : "";
488
- };
489
- var matchValue = (docValue, matchValueRaw) => {
490
- if (Array.isArray(matchValueRaw)) {
491
- if (!Array.isArray(docValue)) return false;
492
- if (docValue.length !== matchValueRaw.length) return false;
493
- for (let i = 0; i < matchValueRaw.length; i += 1) {
494
- const matched = matchValue(docValue[i], matchValueRaw[i]);
495
- if (matched === null) return null;
496
- if (!matched) return false;
497
- }
498
- return true;
499
- }
500
- if (matchValueRaw && typeof matchValueRaw === "object") {
501
- const matchObj = matchValueRaw;
502
- if (Object.keys(matchObj).some((key) => key.startsWith("$"))) return null;
503
- if (!docValue || typeof docValue !== "object" || Array.isArray(docValue)) return false;
504
- const docObj = docValue;
505
- for (const key of Object.keys(matchObj)) {
506
- const matched = matchValue(docObj[key], matchObj[key]);
507
- if (matched === null) return null;
508
- if (!matched) return false;
509
- }
510
- return true;
511
- }
512
- return Object.is(docValue, matchValueRaw);
513
- };
514
- var matchesSimpleQuery = (doc, query) => {
515
- for (const [key, expected] of Object.entries(query)) {
516
- if (key.startsWith("$")) return null;
517
- const matched = matchValue(getValueAtPath(doc, key), expected);
518
- if (matched === null) return null;
519
- if (!matched) return false;
520
- }
521
- return true;
522
- };
523
- var remapDocFromStorage = (doc) => {
524
- const next = {};
525
- for (const [key, value] of Object.entries(doc)) {
526
- const newKey = key.startsWith(UNDERSCORE_PREFIX) ? key.replace(/^\$_/, "") : key;
527
- next[newKey] = value;
528
- }
529
- return next;
530
- };
531
- var runQueryInternal = async ({ modelName, query = {}, options, strictProjection = false }) => {
532
- const collection = await getCollection(modelName, { uid: options.uid });
533
- const replacedQuery = replaceQueryKeys(query, (key) => key.startsWith("_") && key !== "_id" ? `${UNDERSCORE_PREFIX}${key}` : key);
534
- const limit = typeof options.limit === "number" ? Math.abs(options.limit) : DEFAULT_FIND_LIMIT;
535
- const { docs } = await collection.find({
536
- selector: replacedQuery,
537
- limit
538
- });
539
- const mappedDocs = docs.map(({ _rev: _revIgnored, ...rest }) => remapDocFromStorage(rest));
540
- let filteredDocs = mappedDocs;
541
- if (options.projection) {
542
- if (strictProjection && mappedDocs.some((entry) => !satisfiesProjection(entry, options.projection))) return {
543
- data: [],
544
- context: { source: "cache" },
545
- projectionMismatch: true
546
- };
547
- filteredDocs = filteredDocs.filter((entry) => satisfiesProjection(entry, options.projection));
548
- }
549
- let result = filteredDocs;
550
- if (options.sort) result = result.sort((a, b) => compareSort(a, b, options.sort));
551
- return {
552
- data: result,
553
- context: { source: "cache" },
554
- projectionMismatch: false
555
- };
556
- };
557
- var runQuery = async ({ modelName, query = {}, options }) => {
558
- const result = await runQueryInternal({
559
- modelName,
560
- query,
561
- options
562
- });
563
- return {
564
- data: result.data,
565
- context: result.context
566
- };
567
- };
568
- var addWriteDoc = (writes, modelName, doc) => {
569
- const docsById = writes.get(modelName) ?? /* @__PURE__ */ new Map();
570
- docsById.set(doc._id, doc);
571
- writes.set(modelName, docsById);
572
- };
573
- var sanitizePopulatedDoc = (doc, populate, writes) => {
574
- const next = cloneDoc(doc);
575
- for (const entry of populate) {
576
- const value = getValueAtPath(next, entry.path);
577
- if (value === void 0) continue;
578
- if (Array.isArray(value)) {
579
- const ids = [];
580
- for (const candidate of value) {
581
- const id = extractDocId(candidate);
582
- if (!id) continue;
583
- ids.push(id);
584
- if (!entry.model) continue;
585
- if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) continue;
586
- const candidateDoc = candidate;
587
- const nested = entry.populate?.length ? sanitizePopulatedDoc(candidateDoc, entry.populate, writes) : cloneDoc(candidateDoc);
588
- addWriteDoc(writes, entry.model, nested);
589
- }
590
- setValueAtPath(next, entry.path, ids);
591
- continue;
592
- }
593
- const id = extractDocId(value);
594
- setValueAtPath(next, entry.path, id || null);
595
- if (!id) continue;
596
- if (!entry.model) continue;
597
- if (!value || typeof value !== "object" || Array.isArray(value)) continue;
598
- const valueDoc = value;
599
- const nested = entry.populate?.length ? sanitizePopulatedDoc(valueDoc, entry.populate, writes) : cloneDoc(valueDoc);
600
- addWriteDoc(writes, entry.model, nested);
601
- }
602
- return next;
603
- };
604
- var updatePopulatedDocs = async ({ modelName, data, uid, populate }) => {
605
- if (!data.length) return;
606
- const writes = /* @__PURE__ */ new Map();
607
- await updateDocs(modelName, data.map((doc) => sanitizePopulatedDoc(doc, populate, writes)), uid);
608
- for (const [targetModelName, docsById] of writes.entries()) {
609
- if (!docsById.size) continue;
610
- await updateDocs(targetModelName, Array.from(docsById.values()), uid);
611
- }
612
- };
613
- var loadProjectedDocsByIds = async ({ modelName, ids, uid, projection }) => {
614
- const uniqueIds = Array.from(new Set(ids.filter(Boolean)));
615
- if (!uniqueIds.length) return {
616
- rawDocsById: /* @__PURE__ */ new Map(),
617
- projectedDocsById: /* @__PURE__ */ new Map(),
618
- projectionMismatch: false
619
- };
620
- const { docs } = await (await getCollection(modelName, { uid })).find({
621
- selector: { _id: { $in: uniqueIds } },
622
- limit: uniqueIds.length
623
- });
624
- const rawDocsById = /* @__PURE__ */ new Map();
625
- const projectedDocsById = /* @__PURE__ */ new Map();
626
- let projectionMismatch = false;
627
- for (const rawDoc of docs) {
628
- const id = typeof rawDoc._id === "string" ? rawDoc._id : "";
629
- if (!id) continue;
630
- const { _rev: _revIgnored, ...rest } = rawDoc;
631
- const remapped = remapDocFromStorage(rest);
632
- if (!satisfiesProjection(remapped, projection)) {
633
- projectionMismatch = true;
634
- continue;
635
- }
636
- rawDocsById.set(id, remapped);
637
- projectedDocsById.set(id, applyProjection(remapped, projection));
638
- }
639
- if (projectedDocsById.size < uniqueIds.length) projectionMismatch = true;
640
- return {
641
- rawDocsById,
642
- projectedDocsById,
643
- projectionMismatch
644
- };
645
- };
646
- var hydratePopulateEntries = async ({ docs, populate, uid }) => {
647
- const currentDocs = docs;
648
- for (const entry of populate) {
649
- if (!entry.model) return {
650
- hit: false,
651
- data: []
652
- };
653
- const descriptors = currentDocs.map((doc) => {
654
- const rawValue = getValueAtPath(doc, entry.path);
655
- const isArray = Array.isArray(rawValue);
656
- const ids = (isArray ? rawValue : [rawValue]).map((candidate) => extractDocId(candidate)).filter(Boolean);
657
- return {
658
- doc,
659
- hasValue: rawValue !== void 0,
660
- isArray,
661
- ids
662
- };
663
- });
664
- const allIds = descriptors.flatMap((descriptor) => descriptor.ids);
665
- const loaded = await loadProjectedDocsByIds({
666
- modelName: entry.model,
667
- ids: allIds,
668
- uid,
669
- projection: entry.select
670
- });
671
- if (loaded.projectionMismatch) return {
672
- hit: false,
673
- data: []
674
- };
675
- let populatedDocsById = loaded.projectedDocsById;
676
- if (entry.match) {
677
- const filtered = /* @__PURE__ */ new Map();
678
- for (const [id, candidate] of loaded.rawDocsById.entries()) {
679
- const matched = matchesSimpleQuery(candidate, entry.match);
680
- if (matched === null) return {
681
- hit: false,
682
- data: []
683
- };
684
- if (!matched) continue;
685
- const projected = populatedDocsById.get(id);
686
- if (projected) filtered.set(id, projected);
687
- }
688
- populatedDocsById = filtered;
689
- }
690
- if (entry.populate?.length) {
691
- const nestedResult = await hydratePopulateEntries({
692
- docs: Array.from(populatedDocsById.values()).map((candidate) => cloneDoc(candidate)),
693
- populate: entry.populate,
694
- uid
695
- });
696
- if (!nestedResult.hit) return {
697
- hit: false,
698
- data: []
699
- };
700
- populatedDocsById = nestedResult.data.reduce((acc, candidate) => {
701
- const id = extractDocId(candidate);
702
- if (id) acc.set(id, candidate);
703
- return acc;
704
- }, /* @__PURE__ */ new Map());
705
- }
706
- for (const descriptor of descriptors) {
707
- if (!descriptor.hasValue) continue;
708
- if (!descriptor.ids.length) {
709
- setValueAtPath(descriptor.doc, entry.path, descriptor.isArray ? [] : null);
710
- continue;
711
- }
712
- if (descriptor.isArray) {
713
- let values = descriptor.ids.map((id) => populatedDocsById.get(id)).filter((candidate) => Boolean(candidate));
714
- if (entry.options?.sort) values = values.sort((a, b) => compareSort(a, b, entry.options.sort));
715
- if (typeof entry.options?.limit === "number" && Number.isFinite(entry.options.limit)) values = values.slice(0, Math.max(0, Math.floor(Math.abs(entry.options.limit))));
716
- setValueAtPath(descriptor.doc, entry.path, values);
717
- continue;
718
- }
719
- const value = populatedDocsById.get(descriptor.ids[0]);
720
- setValueAtPath(descriptor.doc, entry.path, value ?? null);
721
- }
722
- }
723
- return {
724
- hit: true,
725
- data: currentDocs
726
- };
727
- };
728
- var runPopulatedQuery = async ({ modelName, query = {}, options }) => {
729
- const rootResult = await runQueryInternal({
730
- modelName,
731
- query,
732
- options: {
733
- uid: options.uid,
734
- projection: options.projection,
735
- sort: options.sort,
736
- limit: options.limit
737
- },
738
- strictProjection: true
739
- });
740
- if (rootResult.projectionMismatch) return {
741
- hit: false,
742
- data: [],
743
- context: rootResult.context
744
- };
745
- const hydrated = await hydratePopulateEntries({
746
- docs: rootResult.data.map((doc) => applyProjection(doc, options.projection)).map((doc) => cloneDoc(doc)),
747
- populate: options.populate,
748
- uid: options.uid
749
- });
750
- if (!hydrated.hit) return {
751
- hit: false,
752
- data: [],
753
- context: rootResult.context
754
- };
755
- return {
756
- hit: true,
757
- data: hydrated.data,
758
- context: rootResult.context
759
- };
760
- };
761
- var updateDocs = async (modelName, data, uid) => {
762
- const collection = await getCollection(modelName, { uid });
763
- const allIds = data.map((doc) => doc._id).filter(Boolean);
764
- if (!allIds.length) return;
765
- const { docs: currentDocs } = await collection.find({
766
- selector: { _id: { $in: allIds } },
767
- fields: ["_id", "_rev"],
768
- limit: allIds.length
769
- });
770
- const currentDocsById = currentDocs.reduce((acc, doc) => {
771
- const id = String(doc._id ?? "");
772
- if (id) acc[id] = doc;
773
- return acc;
774
- }, {});
775
- const newDocs = data.map((mongoDoc) => {
776
- const currentDoc = currentDocsById[mongoDoc._id] ?? { _id: mongoDoc._id };
777
- const nextDoc = Object.entries(mongoDoc).reduce((acc, [key, value]) => {
778
- const newKey = key !== "_id" && key.startsWith("_") ? `${UNDERSCORE_PREFIX}${key}` : key;
779
- acc[newKey] = value;
780
- return acc;
781
- }, { ...currentDoc });
782
- const rev = currentDoc._rev;
783
- if (typeof rev === "string" && rev) nextDoc._rev = rev;
784
- else delete nextDoc._rev;
785
- return nextDoc;
786
- });
787
- await collection.bulkDocs(newDocs);
788
- };
789
- var deleteDocs = async (modelName, ids, uid) => {
790
- const collection = await getCollection(modelName, { uid });
791
- const allIds = ids.map((id) => String(id ?? "")).filter(Boolean);
792
- if (!allIds.length) return;
793
- const { docs: currentDocs } = await collection.find({
794
- selector: { _id: { $in: allIds } },
795
- fields: ["_id", "_rev"],
796
- limit: allIds.length
797
- });
798
- const deletions = currentDocs.map((doc) => ({
799
- _id: String(doc?._id ?? ""),
800
- _rev: doc?._rev,
801
- _deleted: true
802
- })).filter((doc) => doc._id && typeof doc._rev === "string" && doc._rev);
803
- if (!deletions.length) return;
804
- await collection.bulkDocs(deletions);
805
- };
806
- var destroyCollection = async (modelName, uid) => {
807
- const collection = await getCollection(modelName, { uid });
808
- const prefix = getPrefix();
809
- const dbName = `${uid}/${modelName}`;
810
- collections.delete(`${prefix}${dbName}`);
811
- unregisterDbName(prefix, dbName);
812
- await collection.destroy();
813
- };
814
- var resetRtsPouchStore = ({ tenantId, appName }) => {
815
- const basePrefix = computeBasePrefix({
816
- tenantId,
817
- appName
818
- });
819
- const oldPrefix = readPrefixOverride({
820
- tenantId,
821
- appName
822
- }) ?? basePrefix;
823
- const dbNames = Array.from(loadDbNames(oldPrefix));
824
- const openDbs = Array.from(collections.entries()).filter(([key]) => key.startsWith(oldPrefix)).map(([, db]) => db);
825
- (async () => {
826
- const remaining = new Set(dbNames);
827
- await Promise.all(openDbs.map((db) => db.destroy().catch(() => {})));
828
- if (remaining.size) {
829
- const PouchDB = await getPouchDb();
830
- const PouchDBForPrefix = PouchDB.defaults?.({}) ?? PouchDB;
831
- PouchDBForPrefix.prefix = oldPrefix;
832
- await Promise.all(Array.from(remaining).map(async (name) => {
833
- await new PouchDBForPrefix(name, {
834
- adapter: activePouchAdapter,
835
- revs_limit: 1
836
- }).destroy().then(() => {
837
- remaining.delete(name);
838
- }).catch(() => {});
839
- }));
840
- }
841
- if (remaining.size) persistDbNames(oldPrefix, remaining);
842
- else persistDbNames(oldPrefix, /* @__PURE__ */ new Set());
843
- })();
844
- const newPrefix = `${basePrefix}reset-${Date.now().toString(16)}/`;
845
- const storage = getRuntimeStorage();
846
- try {
847
- storage.setItem(getPrefixOverrideKey({
848
- tenantId,
849
- appName
850
- }), newPrefix);
851
- } catch {
852
- return newPrefix;
853
- }
854
- lastAppliedPrefix = null;
855
- collections.clear();
856
- return newPrefix;
857
- };
858
- var destroyAllCollections = async () => {
859
- const dbs = Array.from(collections.values());
860
- await Promise.all(dbs.map((db) => db.destroy()));
861
- collections.clear();
862
- };
863
- //#endregion
864
- //#region src/rts/populateCache.ts
865
- var EXCLUDE_PROJECTION_ERROR = "must be include-only (value 1); exclusion projection is not supported";
866
- var sortProjectionSpec = (projection) => {
867
- const sorted = {};
868
- for (const key of Object.keys(projection).sort()) sorted[key] = projection[key];
869
- return sorted;
870
- };
871
- var normalizeProjectionSpec = (value, source, label) => {
872
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
873
- const raw = value;
874
- const normalized = {};
875
- let hasExclude = false;
876
- for (const [key, rawValue] of Object.entries(raw)) {
877
- const path = key.trim();
878
- if (!path) continue;
879
- if (rawValue === 1 || rawValue === true) {
880
- normalized[path] = 1;
881
- continue;
882
- }
883
- if (rawValue === 0 || rawValue === false) {
884
- hasExclude = true;
885
- continue;
886
- }
887
- if (typeof rawValue === "number" && Number.isFinite(rawValue)) {
888
- if (rawValue === 1) normalized[path] = 1;
889
- if (rawValue === 0) hasExclude = true;
890
- }
891
- }
892
- if (hasExclude) throw new Error(`${source}: ${label} ${EXCLUDE_PROJECTION_ERROR}`);
893
- return Object.keys(normalized).length > 0 ? sortProjectionSpec(normalized) : void 0;
894
- };
895
- var normalizeSelectString = (value, source) => {
896
- const tokens = value.split(/\s+/).map((token) => token.trim()).filter(Boolean);
897
- if (!tokens.length) return void 0;
898
- const normalized = {};
899
- for (const token of tokens) {
900
- let path = token;
901
- if (token.startsWith("-")) throw new Error(`${source}: populate select ${EXCLUDE_PROJECTION_ERROR}`);
902
- else if (token.startsWith("+")) path = token.slice(1);
903
- path = path.trim();
904
- if (!path) continue;
905
- normalized[path] = 1;
906
- }
907
- return Object.keys(normalized).length > 0 ? sortProjectionSpec(normalized) : void 0;
908
- };
909
- var normalizePopulateSelect = (value, source) => {
910
- if (typeof value === "string") return normalizeSelectString(value, source);
911
- return normalizeProjectionSpec(value, source, "populate select");
912
- };
913
- var normalizePopulateOptions = (value) => {
914
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
915
- const raw = value;
916
- const normalized = {};
917
- if (raw.sort && typeof raw.sort === "object" && !Array.isArray(raw.sort)) {
918
- const sortRaw = raw.sort;
919
- const sort = {};
920
- for (const [key, rawDirection] of Object.entries(sortRaw)) {
921
- const path = key.trim();
922
- if (!path) continue;
923
- if (rawDirection === 1 || rawDirection === "asc") {
924
- sort[path] = 1;
925
- continue;
926
- }
927
- if (rawDirection === -1 || rawDirection === "desc") sort[path] = -1;
928
- }
929
- if (Object.keys(sort).length > 0) normalized.sort = sort;
930
- }
931
- if (typeof raw.limit === "number" && Number.isFinite(raw.limit)) normalized.limit = Math.max(0, Math.floor(Math.abs(raw.limit)));
932
- return Object.keys(normalized).length > 0 ? normalized : void 0;
933
- };
934
- var normalizeString = (value) => {
935
- if (typeof value !== "string") return void 0;
936
- return value.trim() || void 0;
937
- };
938
- var normalizeObject = (value) => {
939
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
940
- return value;
941
- };
942
- var normalizePopulateObject = (value, source) => {
943
- const path = normalizeString(value.path);
944
- if (!path) throw new Error(`${source}: populate entries must define a non-empty path`);
945
- const select = normalizePopulateSelect(value.select, source);
946
- if (!select) throw new Error(`${source}: populate entries must define a select projection`);
947
- const nested = value.populate !== void 0 ? normalizePopulateOption(value.populate, source) : void 0;
948
- return {
949
- path,
950
- select,
951
- ...normalizeString(value.model) ? { model: normalizeString(value.model) } : {},
952
- ...normalizeObject(value.match) ? { match: normalizeObject(value.match) } : {},
953
- ...normalizePopulateOptions(value.options) ? { options: normalizePopulateOptions(value.options) } : {},
954
- ...nested && nested.length > 0 ? { populate: nested } : {}
955
- };
956
- };
957
- var normalizePopulateOption = (value, source) => {
958
- if (typeof value === "string") throw new Error(`${source}: populate string syntax is not supported; use object entries with select`);
959
- if (Array.isArray(value)) {
960
- if (value.length === 0) throw new Error(`${source}: populate must contain at least one entry`);
961
- return value.map((entry) => {
962
- if (typeof entry === "string") throw new Error(`${source}: populate string syntax is not supported; use object entries with select`);
963
- return normalizePopulateObject(entry, source);
964
- });
965
- }
966
- return [normalizePopulateObject(value, source)];
967
- };
968
- var preparePopulateCacheOptions = (options, source) => {
969
- if (!options.populate) return void 0;
970
- const rootProjection = normalizeProjectionSpec(options.projection, source, "projection");
971
- if (!rootProjection) throw new Error(`${source}: projection is required when populate is used`);
972
- const populate = normalizePopulateOption(options.populate, source);
973
- if (!populate.length) throw new Error(`${source}: populate must contain at least one entry`);
974
- return {
975
- rootProjection,
976
- populate
977
- };
978
- };
979
- //#endregion
980
- //#region src/rts/queryKey.ts
981
- var computeRtsQueryKey = (query, options) => {
982
- const key = options.key ?? "";
983
- const projection = options.projection ? JSON.stringify(options.projection) : "";
984
- const sort = options.sort ? JSON.stringify(options.sort) : "";
985
- const limit = typeof options.limit === "number" ? String(options.limit) : "";
986
- const populate = options.populate ? JSON.stringify(options.populate) : "";
987
- const pagination = options.pagination ? JSON.stringify(options.pagination) : "";
988
- return `${key}${JSON.stringify(query)}${projection}${sort}${limit}${populate}${pagination}`;
989
- };
990
- //#endregion
991
- //#region src/rts/wsClient.ts
992
- var TENANT_ID_QUERY_PARAM = "rb-tenant-id";
993
- var RTS_CHANGES_PATH = "/api/rb/rts/changes";
994
- var MAX_TXN_BUF = 2048;
995
- var SERVER_RECONNECT_DELAY_MIN_MS = 1e4;
996
- var SERVER_RECONNECT_DELAY_MAX_MS = 15e3;
997
- var RUN_NETWORK_QUERY_TIMEOUT_ERROR = "runNetworkQuery: request timed out";
998
- var RUN_NETWORK_COUNT_TIMEOUT_ERROR = "runNetworkCount: request timed out";
999
- var socket = null;
1000
- var connectPromise = null;
1001
- var explicitDisconnect = false;
1002
- var currentTenantId = null;
1003
- var currentUid = null;
1004
- var connectOptions = {};
1005
- var localTxnBuf = [];
1006
- var queryCallbacks = /* @__PURE__ */ new Map();
1007
- var countCallbacks = /* @__PURE__ */ new Map();
1008
- var subscriptions = /* @__PURE__ */ new Map();
1009
- var countSubscriptions = /* @__PURE__ */ new Map();
1010
- var messageCallbacks = /* @__PURE__ */ new Map();
1011
- var reconnectTimer = null;
1012
- var reconnectAttempts = 0;
1013
- var hasEstablishedConnection = false;
1014
- var pendingServerReconnectJitter = false;
1015
- var syncPromise = null;
1016
- var syncKey = null;
1017
- var connectionStatus = "idle";
1018
- var connectionError = null;
1019
- var connectionStatusCallbacks = /* @__PURE__ */ new Set();
1020
- var ensureRealtimeRuntime = () => {
1021
- if (typeof WebSocket !== "function") throw new Error("RTS websocket client requires WebSocket support");
1022
- if (typeof globalThis.setTimeout !== "function" || typeof globalThis.clearTimeout !== "function") throw new Error("RTS websocket client requires timer support");
1023
- };
1024
- var ensureSyncRuntime = () => {
1025
- if (typeof fetch !== "function") throw new Error("syncRtsChanges requires fetch support");
1026
- };
1027
- var getRuntimeLocationHref = () => {
1028
- if (typeof window !== "undefined" && typeof window.location?.href === "string" && window.location.href) return window.location.href;
1029
- const location = globalThis.location;
1030
- if (typeof location?.href === "string" && location.href) return location.href;
1031
- return null;
1032
- };
1033
- var setRuntimeTimeout = (handler, delayMs) => {
1034
- return globalThis.setTimeout(handler, delayMs);
1035
- };
1036
- var clearRuntimeTimeout = (timer) => {
1037
- globalThis.clearTimeout(timer);
1038
- };
1039
- var setConnectionStatus = (status, error = null) => {
1040
- connectionStatus = status;
1041
- connectionError = error;
1042
- for (const callback of connectionStatusCallbacks) callback(status, error);
1043
- };
1044
- var resolveWebSocketUrlFromCandidate = (candidateUrl, options) => {
1045
- const url = new URL(candidateUrl);
1046
- if (url.protocol === "http:") url.protocol = "ws:";
1047
- else if (url.protocol === "https:") url.protocol = "wss:";
1048
- if (!url.pathname || url.pathname === "/") url.pathname = options.path ?? "/rts";
1049
- return url;
1050
- };
1051
- var resolveApiOriginUrl = ({ url }) => {
1052
- if (url) {
1053
- const base = new URL(url);
1054
- if (base.protocol === "ws:") base.protocol = "http:";
1055
- else if (base.protocol === "wss:") base.protocol = "https:";
1056
- base.pathname = "/";
1057
- base.search = "";
1058
- base.hash = "";
1059
- return base;
1060
- }
1061
- const locationHref = getRuntimeLocationHref();
1062
- if (!locationHref) throw new Error("syncRtsChanges: options.url is required when location.href is unavailable");
1063
- const base = new URL(locationHref);
1064
- base.pathname = "/";
1065
- base.search = "";
1066
- base.hash = "";
1067
- return base;
1068
- };
1069
- var buildSyncChangesUrl = (tenantId, options) => {
1070
- const base = resolveApiOriginUrl(options);
1071
- const url = new URL(RTS_CHANGES_PATH, base);
1072
- url.searchParams.set(TENANT_ID_QUERY_PARAM, tenantId);
1073
- return url.toString();
1074
- };
1075
- var buildSocketUrl = (tenantId, _uid, options) => {
1076
- if (options.url) {
1077
- const url = resolveWebSocketUrlFromCandidate(options.url, options);
1078
- url.searchParams.set(TENANT_ID_QUERY_PARAM, tenantId);
1079
- return url.toString();
1080
- }
1081
- const locationHref = getRuntimeLocationHref();
1082
- if (!locationHref) throw new Error("connect: options.url is required when location.href is unavailable");
1083
- const base = new URL(locationHref);
1084
- base.protocol = base.protocol === "https:" ? "wss:" : "ws:";
1085
- base.pathname = options.path ?? "/rts";
1086
- base.search = "";
1087
- base.hash = "";
1088
- base.searchParams.set(TENANT_ID_QUERY_PARAM, tenantId);
1089
- return base.toString();
1090
- };
1091
- var sendToServer = (message) => {
1092
- if (!socket) return;
1093
- if (socket.readyState !== WebSocket.OPEN) return;
1094
- socket.send(JSON.stringify(message));
1095
- };
1096
- var resubscribeAll = ({ forceInitialQuery }) => {
1097
- for (const sub of subscriptions.values()) {
1098
- const runInitialQuery = forceInitialQuery || sub.runInitialNetworkQuery;
1099
- sendToServer({
1100
- type: "register-query",
1101
- modelName: sub.modelName,
1102
- queryKey: sub.queryKey,
1103
- query: sub.query,
1104
- options: sub.options,
1105
- runInitialQuery
1106
- });
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
- }
1119
- };
1120
- var clearReconnectTimer = () => {
1121
- if (reconnectTimer === null) return;
1122
- clearRuntimeTimeout(reconnectTimer);
1123
- reconnectTimer = null;
1124
- };
1125
- var scheduleReconnect = () => {
1126
- clearReconnectTimer();
1127
- if (explicitDisconnect) {
1128
- pendingServerReconnectJitter = false;
1129
- return;
1130
- }
1131
- if (!currentTenantId || !currentUid) return;
1132
- const cfg = connectOptions.reconnect ?? {};
1133
- const maxAttempts = cfg.attempts ?? 128;
1134
- const delayMs = cfg.delayMs ?? 400;
1135
- const delayMaxMs = cfg.delayMaxMs ?? 1e4;
1136
- if (reconnectAttempts >= maxAttempts) return;
1137
- let delay = Math.min(delayMaxMs, delayMs * Math.pow(2, reconnectAttempts));
1138
- if (pendingServerReconnectJitter) {
1139
- const span = SERVER_RECONNECT_DELAY_MAX_MS - SERVER_RECONNECT_DELAY_MIN_MS;
1140
- delay = SERVER_RECONNECT_DELAY_MIN_MS + Math.floor(Math.random() * (span + 1));
1141
- pendingServerReconnectJitter = false;
1142
- }
1143
- reconnectAttempts += 1;
1144
- reconnectTimer = setRuntimeTimeout(() => {
1145
- connectInternal(currentTenantId, currentUid, connectOptions, { resetReconnectAttempts: false });
1146
- }, delay);
1147
- };
1148
- var isDocWithId = (doc) => {
1149
- if (!doc || typeof doc !== "object") return false;
1150
- return typeof doc._id === "string";
1151
- };
1152
- var normalizePageInfo$1 = (value) => {
1153
- if (!value || typeof value !== "object") return void 0;
1154
- if (Array.isArray(value)) return void 0;
1155
- const raw = value;
1156
- if (typeof raw.hasNextPage !== "boolean" || typeof raw.hasPrevPage !== "boolean") return void 0;
1157
- const nextCursor = typeof raw.nextCursor === "string" && raw.nextCursor ? raw.nextCursor : void 0;
1158
- const prevCursor = typeof raw.prevCursor === "string" && raw.prevCursor ? raw.prevCursor : void 0;
1159
- return {
1160
- hasNextPage: raw.hasNextPage,
1161
- hasPrevPage: raw.hasPrevPage,
1162
- ...nextCursor ? { nextCursor } : {},
1163
- ...prevCursor ? { prevCursor } : {}
1164
- };
1165
- };
1166
- var normalizeTotalCount$1 = (value) => {
1167
- if (typeof value !== "number") return void 0;
1168
- if (!Number.isFinite(value) || value < 0) return void 0;
1169
- return Math.floor(value);
1170
- };
1171
- var handleQueryPayload = (payload) => {
1172
- const { modelName, queryKey, data, error, txnId } = payload;
1173
- const cbKey = `${modelName}.${queryKey}`;
1174
- const callbacks = queryCallbacks.get(cbKey);
1175
- if (!callbacks || !callbacks.size) return;
1176
- const subscription = subscriptions.get(cbKey);
1177
- const pageInfo = normalizePageInfo$1(payload.pageInfo);
1178
- const totalCount = normalizeTotalCount$1(payload.totalCount);
1179
- const populateCache = subscription?.populateCache;
1180
- const hasPopulate = Boolean(populateCache);
1181
- const hasPagination = Boolean(subscription?.options?.pagination || pageInfo || totalCount !== void 0);
1182
- const context = {
1183
- source: "network",
1184
- isLocal: !!(txnId && localTxnBuf.includes(txnId)),
1185
- txnId,
1186
- ...pageInfo ? { pageInfo } : {},
1187
- ...totalCount !== void 0 ? { totalCount } : {}
1188
- };
1189
- if (error) {
1190
- for (const cb of callbacks) cb(error, void 0, context);
1191
- return;
1192
- }
1193
- for (const cb of callbacks) cb(null, data, context);
1194
- if (!currentUid) return;
1195
- const docs = Array.isArray(data) ? data.filter(isDocWithId) : [];
1196
- if (hasPagination) return;
1197
- if (!docs.length) return;
1198
- if (hasPopulate && populateCache) {
1199
- updatePopulatedDocs({
1200
- modelName,
1201
- data: docs,
1202
- uid: currentUid,
1203
- populate: populateCache.populate
1204
- }).catch(() => {});
1205
- return;
1206
- }
1207
- updateDocs(modelName, docs, currentUid).catch(() => {});
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
- };
1226
- var handleEvent = (payload) => {
1227
- const callbacks = messageCallbacks.get(payload.event);
1228
- if (!callbacks || !callbacks.size) return;
1229
- for (const cb of callbacks) cb(payload.payload);
1230
- };
1231
- var handleMessage = (event) => {
1232
- let parsed;
1233
- try {
1234
- parsed = JSON.parse(typeof event.data === "string" ? event.data : String(event.data));
1235
- } catch {
1236
- return;
1237
- }
1238
- if (!parsed || typeof parsed !== "object") return;
1239
- const message = parsed;
1240
- if (message.type === "query-payload") {
1241
- handleQueryPayload(message);
1242
- return;
1243
- }
1244
- if (message.type === "count-payload") {
1245
- handleCountPayload(message);
1246
- return;
1247
- }
1248
- if (message.type === "event") handleEvent(message);
1249
- };
1250
- var addLocalTxn = (txnId) => {
1251
- if (!txnId) return;
1252
- localTxnBuf.push(txnId);
1253
- if (localTxnBuf.length > MAX_TXN_BUF) localTxnBuf.shift();
1254
- };
1255
- var getSyncStorageKey = ({ tenantId, uid, appName }) => `rb:rts:changesSeq:${appName ?? ""}:${tenantId}:${uid}`;
1256
- var readStoredSeq = (key) => {
1257
- const storage = getRuntimeStorage();
1258
- try {
1259
- const raw = storage.getItem(key);
1260
- const num = raw ? Number(raw) : 0;
1261
- return Number.isFinite(num) && num >= 0 ? Math.floor(num) : 0;
1262
- } catch {
1263
- return 0;
1264
- }
1265
- };
1266
- var writeStoredSeq = (key, value) => {
1267
- const storage = getRuntimeStorage();
1268
- try {
1269
- storage.setItem(key, String(Math.max(0, Math.floor(value))));
1270
- } catch {
1271
- return;
1272
- }
1273
- };
1274
- var applyChangeBatch = async (changes, uid) => {
1275
- const resetModels = /* @__PURE__ */ new Set();
1276
- const deletesByModel = /* @__PURE__ */ new Map();
1277
- for (const change of changes) {
1278
- const modelName = typeof change.modelName === "string" ? change.modelName : "";
1279
- if (!modelName) continue;
1280
- if (change.op === "reset_model") {
1281
- resetModels.add(modelName);
1282
- continue;
1283
- }
1284
- if (change.op === "delete") {
1285
- const docId = typeof change.docId === "string" ? change.docId : "";
1286
- if (!docId) continue;
1287
- const existing = deletesByModel.get(modelName) ?? [];
1288
- existing.push(docId);
1289
- deletesByModel.set(modelName, existing);
1290
- }
1291
- }
1292
- for (const modelName of resetModels) await destroyCollection(modelName, uid).catch(() => {});
1293
- for (const [modelName, ids] of deletesByModel.entries()) {
1294
- if (resetModels.has(modelName)) continue;
1295
- await deleteDocs(modelName, ids, uid).catch(() => {});
1296
- }
1297
- };
1298
- var syncRtsChanges = async (tenantId, uid, options = {}) => {
1299
- ensureSyncRuntime();
1300
- if (!tenantId || !uid) return;
1301
- const storageKey = getSyncStorageKey({
1302
- tenantId,
1303
- uid,
1304
- appName: options.appName
1305
- });
1306
- let sinceSeq = readStoredSeq(storageKey);
1307
- const syncUrl = buildSyncChangesUrl(tenantId, { url: options.url });
1308
- for (let i = 0; i < 32; i += 1) {
1309
- const response = await fetch(syncUrl, {
1310
- method: "POST",
1311
- credentials: "include",
1312
- headers: { "Content-Type": "application/json" },
1313
- body: JSON.stringify({
1314
- sinceSeq,
1315
- limit: 2e3
1316
- })
1317
- });
1318
- if (!response.ok) return;
1319
- const payload = await response.json().catch(() => null);
1320
- if (!payload || typeof payload !== "object") return;
1321
- const payloadObj = payload;
1322
- if (payloadObj.ok !== true) return;
1323
- const latestSeq = Number(payloadObj.latestSeq ?? 0);
1324
- if (Boolean(payloadObj.needsFullResync)) {
1325
- resetRtsPouchStore({
1326
- tenantId,
1327
- appName: options.appName
1328
- });
1329
- writeStoredSeq(storageKey, latestSeq);
1330
- return;
1331
- }
1332
- const changesRaw = payloadObj.changes;
1333
- const normalized = (Array.isArray(changesRaw) ? changesRaw : []).map((c) => {
1334
- if (!c || typeof c !== "object") return null;
1335
- const obj = c;
1336
- const seq = Number(obj.seq ?? 0);
1337
- const modelName = typeof obj.modelName === "string" ? obj.modelName : String(obj.modelName ?? "");
1338
- const op = obj.op === "reset_model" ? "reset_model" : "delete";
1339
- const docId = typeof obj.docId === "string" && obj.docId ? obj.docId : obj.docId ? String(obj.docId) : void 0;
1340
- return {
1341
- seq,
1342
- modelName,
1343
- op,
1344
- ...docId ? { docId } : {}
1345
- };
1346
- }).filter((c) => c !== null).filter((c) => Number.isFinite(c.seq) && c.seq > 0 && c.modelName && (c.op === "reset_model" || !!c.docId));
1347
- if (!normalized.length) {
1348
- writeStoredSeq(storageKey, latestSeq);
1349
- return;
1350
- }
1351
- await applyChangeBatch(normalized, uid);
1352
- sinceSeq = normalized.reduce((max, c) => c.seq > max ? c.seq : max, sinceSeq);
1353
- writeStoredSeq(storageKey, sinceSeq);
1354
- if (latestSeq > 0 && sinceSeq >= latestSeq) return;
1355
- }
1356
- };
1357
- var ensureSynced = (tenantId, uid, options) => {
1358
- if (options.syncChanges === false) return;
1359
- const key = `${options.appName ?? ""}:${tenantId}:${uid}`;
1360
- if (syncPromise && syncKey === key) return;
1361
- syncKey = key;
1362
- syncPromise = syncRtsChanges(tenantId, uid, {
1363
- appName: options.appName,
1364
- url: options.url
1365
- }).catch(() => {}).finally(() => {
1366
- if (syncKey === key) syncPromise = null;
1367
- });
1368
- };
1369
- var connectInternal = (tenantId, uid, options, { resetReconnectAttempts }) => {
1370
- ensureRealtimeRuntime();
1371
- if (!tenantId) return Promise.resolve();
1372
- if (!uid) throw new Error("Missing uid");
1373
- currentTenantId = tenantId;
1374
- currentUid = uid;
1375
- connectOptions = options;
1376
- if (options.configureStore !== false) configureRtsPouchStore({
1377
- tenantId,
1378
- appName: options.appName
1379
- });
1380
- ensureSynced(tenantId, uid, options);
1381
- if (socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING)) return connectPromise ?? Promise.resolve();
1382
- explicitDisconnect = false;
1383
- clearReconnectTimer();
1384
- const url = buildSocketUrl(tenantId, uid, options);
1385
- connectPromise = new Promise((resolve, reject) => {
1386
- if (resetReconnectAttempts) reconnectAttempts = 0;
1387
- let opened = false;
1388
- let settled = false;
1389
- setConnectionStatus("connecting");
1390
- socket = new WebSocket(url);
1391
- socket.addEventListener("open", () => {
1392
- opened = true;
1393
- settled = true;
1394
- reconnectAttempts = 0;
1395
- pendingServerReconnectJitter = false;
1396
- setConnectionStatus("connected");
1397
- resubscribeAll({ forceInitialQuery: hasEstablishedConnection });
1398
- hasEstablishedConnection = true;
1399
- resolve();
1400
- });
1401
- socket.addEventListener("message", handleMessage);
1402
- socket.addEventListener("close", (event) => {
1403
- if (!opened && !settled) {
1404
- settled = true;
1405
- const error = /* @__PURE__ */ new Error(`RTS WebSocket closed before opening (code=${event.code})`);
1406
- setConnectionStatus("error", error);
1407
- reject(error);
1408
- }
1409
- if (!explicitDisconnect) {
1410
- pendingServerReconnectJitter = opened;
1411
- setConnectionStatus("connecting", connectionError);
1412
- } else setConnectionStatus("idle");
1413
- socket = null;
1414
- connectPromise = null;
1415
- scheduleReconnect();
1416
- });
1417
- socket.addEventListener("error", (err) => {
1418
- if (settled) return;
1419
- settled = true;
1420
- const error = err instanceof Error ? err : /* @__PURE__ */ new Error("RTS WebSocket error");
1421
- setConnectionStatus("error", error);
1422
- reject(error);
1423
- });
1424
- });
1425
- return connectPromise;
1426
- };
1427
- var connect = (tenantId, uid, options = {}) => {
1428
- return connectInternal(tenantId, uid, options, { resetReconnectAttempts: true });
1429
- };
1430
- var disconnect = () => {
1431
- explicitDisconnect = true;
1432
- clearReconnectTimer();
1433
- hasEstablishedConnection = false;
1434
- pendingServerReconnectJitter = false;
1435
- if (socket) try {
1436
- socket.close();
1437
- } catch {}
1438
- socket = null;
1439
- connectPromise = null;
1440
- setConnectionStatus("idle");
1441
- };
1442
- var reconnect = (tenantId, uid, options = {}) => {
1443
- explicitDisconnect = true;
1444
- clearReconnectTimer();
1445
- pendingServerReconnectJitter = false;
1446
- if (socket) try {
1447
- socket.close();
1448
- } catch {}
1449
- socket = null;
1450
- connectPromise = null;
1451
- return connect(tenantId, uid, options);
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
- };
1465
- var registerQuery = (modelName, query, optionsOrCallback, callbackMaybe, behavior) => {
1466
- let options;
1467
- let callback;
1468
- if (typeof optionsOrCallback === "function") {
1469
- options = {};
1470
- callback = optionsOrCallback;
1471
- } else {
1472
- options = optionsOrCallback ?? {};
1473
- callback = callbackMaybe;
1474
- }
1475
- if (!callback) return void 0;
1476
- if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("registerQuery: modelName must be a non-empty string");
1477
- const queryKey = computeRtsQueryKey(query, options);
1478
- const cbKey = `${modelName}.${queryKey}`;
1479
- const runInitialNetworkQuery = behavior?.runInitialNetworkQuery !== false;
1480
- const runInitialLocalQuery = behavior?.runInitialLocalQuery !== false;
1481
- const populateCache = preparePopulateCacheOptions({
1482
- projection: options.projection,
1483
- populate: options.populate
1484
- }, "registerQuery");
1485
- const hasPopulate = Boolean(populateCache);
1486
- const hasPagination = Boolean(options.pagination);
1487
- const set = queryCallbacks.get(cbKey) ?? /* @__PURE__ */ new Set();
1488
- set.add(callback);
1489
- queryCallbacks.set(cbKey, set);
1490
- subscriptions.set(cbKey, {
1491
- modelName,
1492
- query,
1493
- options,
1494
- queryKey,
1495
- runInitialNetworkQuery,
1496
- ...populateCache ? { populateCache } : {}
1497
- });
1498
- if (currentUid && runInitialLocalQuery && !hasPagination) if (hasPopulate && populateCache) runPopulatedQuery({
1499
- modelName,
1500
- query,
1501
- options: {
1502
- uid: currentUid,
1503
- projection: populateCache.rootProjection,
1504
- sort: options.sort,
1505
- limit: options.limit,
1506
- populate: populateCache.populate
1507
- }
1508
- }).then(({ hit, data, context }) => {
1509
- if (!hit) return;
1510
- callback?.(null, data, context);
1511
- }).catch(() => {});
1512
- else runQuery({
1513
- modelName,
1514
- query,
1515
- options: {
1516
- uid: currentUid,
1517
- projection: options.projection,
1518
- sort: options.sort,
1519
- limit: options.limit
1520
- }
1521
- }).then(({ data, context }) => {
1522
- callback?.(null, data, context);
1523
- }).catch(() => {});
1524
- sendToServer({
1525
- type: "register-query",
1526
- modelName,
1527
- queryKey,
1528
- query,
1529
- options,
1530
- runInitialQuery: runInitialNetworkQuery
1531
- });
1532
- return () => {
1533
- sendToServer({
1534
- type: "remove-query",
1535
- modelName,
1536
- queryKey
1537
- });
1538
- const callbacks = queryCallbacks.get(cbKey);
1539
- callbacks?.delete(callback);
1540
- if (callbacks && callbacks.size === 0) {
1541
- queryCallbacks.delete(cbKey);
1542
- subscriptions.delete(cbKey);
1543
- }
1544
- };
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
- };
1593
- var makeRunQueryKey = () => `run-query.${Date.now().toString(36)}.${Math.random().toString(36).slice(2, 10)}`;
1594
- var runNetworkQuery = async ({ modelName, query, options = {}, timeoutMs = 1e4 }) => {
1595
- if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("runNetworkQuery: modelName must be a non-empty string");
1596
- preparePopulateCacheOptions({
1597
- projection: options.projection,
1598
- populate: options.populate
1599
- }, "runNetworkQuery");
1600
- const hasTimeout = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0;
1601
- const timeoutStartedAt = hasTimeout ? Date.now() : 0;
1602
- if (!socket || socket.readyState !== WebSocket.OPEN) {
1603
- if (currentTenantId && currentUid) try {
1604
- const connectAttempt = connectInternal(currentTenantId, currentUid, connectOptions, { resetReconnectAttempts: false });
1605
- if (hasTimeout) await new Promise((resolve, reject) => {
1606
- const timeoutId = setRuntimeTimeout(() => {
1607
- reject(new Error(RUN_NETWORK_COUNT_TIMEOUT_ERROR));
1608
- }, timeoutMs);
1609
- connectAttempt.then(() => {
1610
- clearRuntimeTimeout(timeoutId);
1611
- resolve();
1612
- }, (error) => {
1613
- clearRuntimeTimeout(timeoutId);
1614
- reject(error);
1615
- });
1616
- });
1617
- else await connectAttempt;
1618
- } catch {}
1619
- }
1620
- if (!socket || socket.readyState !== WebSocket.OPEN) {
1621
- if (hasTimeout && Date.now() - timeoutStartedAt >= timeoutMs) throw new Error(RUN_NETWORK_QUERY_TIMEOUT_ERROR);
1622
- throw new Error("runNetworkQuery: RTS socket is not connected");
1623
- }
1624
- const remainingTimeoutMs = hasTimeout ? Math.max(0, timeoutMs - (Date.now() - timeoutStartedAt)) : null;
1625
- if (remainingTimeoutMs !== null && remainingTimeoutMs <= 0) throw new Error(RUN_NETWORK_QUERY_TIMEOUT_ERROR);
1626
- const resolvedOptions = options.key ? options : {
1627
- ...options,
1628
- key: makeRunQueryKey()
1629
- };
1630
- const queryKey = computeRtsQueryKey(query, resolvedOptions);
1631
- const cbKey = `${modelName}.${queryKey}`;
1632
- return await new Promise((resolve, reject) => {
1633
- let settled = false;
1634
- let timeoutId = null;
1635
- const cleanup = () => {
1636
- const callbacks = queryCallbacks.get(cbKey);
1637
- callbacks?.delete(callback);
1638
- if (callbacks && callbacks.size === 0) queryCallbacks.delete(cbKey);
1639
- if (timeoutId !== null) clearRuntimeTimeout(timeoutId);
1640
- };
1641
- const settle = (next) => {
1642
- if (settled) return;
1643
- settled = true;
1644
- cleanup();
1645
- next();
1646
- };
1647
- const callback = (error, data, context) => {
1648
- if (error) {
1649
- settle(() => reject(error));
1650
- return;
1651
- }
1652
- settle(() => resolve({
1653
- data,
1654
- context
1655
- }));
1656
- };
1657
- const callbacks = queryCallbacks.get(cbKey) ?? /* @__PURE__ */ new Set();
1658
- callbacks.add(callback);
1659
- queryCallbacks.set(cbKey, callbacks);
1660
- if (remainingTimeoutMs !== null) timeoutId = setRuntimeTimeout(() => {
1661
- settle(() => reject(new Error(RUN_NETWORK_QUERY_TIMEOUT_ERROR)));
1662
- }, remainingTimeoutMs);
1663
- sendToServer({
1664
- type: "run-query",
1665
- modelName,
1666
- queryKey,
1667
- query,
1668
- options: resolvedOptions
1669
- });
1670
- });
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
- };
1750
- var sendMessage = (event, payload) => {
1751
- sendToServer({
1752
- type: "event",
1753
- event,
1754
- payload
1755
- });
1756
- };
1757
- var onMessage = (event, callback) => {
1758
- const set = messageCallbacks.get(event) ?? /* @__PURE__ */ new Set();
1759
- set.add(callback);
1760
- messageCallbacks.set(event, set);
1761
- return () => {
1762
- const callbacks = messageCallbacks.get(event);
1763
- callbacks?.delete(callback);
1764
- if (callbacks && callbacks.size === 0) messageCallbacks.delete(event);
1765
- };
1766
- };
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
1967
- //#region src/rts/useQuery.ts
1968
- var normalizePageInfo = (value) => {
1969
- if (!value || typeof value !== "object") return void 0;
1970
- if (Array.isArray(value)) return void 0;
1971
- const raw = value;
1972
- if (typeof raw.hasNextPage !== "boolean" || typeof raw.hasPrevPage !== "boolean") return void 0;
1973
- const nextCursor = typeof raw.nextCursor === "string" && raw.nextCursor ? raw.nextCursor : void 0;
1974
- const prevCursor = typeof raw.prevCursor === "string" && raw.prevCursor ? raw.prevCursor : void 0;
1975
- return {
1976
- hasNextPage: raw.hasNextPage,
1977
- hasPrevPage: raw.hasPrevPage,
1978
- ...nextCursor ? { nextCursor } : {},
1979
- ...prevCursor ? { prevCursor } : {}
1980
- };
1981
- };
1982
- var normalizeTotalCount = (value) => {
1983
- if (typeof value !== "number") return void 0;
1984
- if (!Number.isFinite(value) || value < 0) return void 0;
1985
- return Math.floor(value);
1986
- };
1987
- var getDocId = (doc) => {
1988
- if (!doc || typeof doc !== "object") return "";
1989
- const id = doc._id;
1990
- return typeof id === "string" ? id.trim() : "";
1991
- };
1992
- var dedupeById = (docs) => {
1993
- const seen = /* @__PURE__ */ new Set();
1994
- const merged = [];
1995
- for (const doc of docs) {
1996
- const id = getDocId(doc);
1997
- if (id && seen.has(id)) continue;
1998
- if (id) seen.add(id);
1999
- merged.push(doc);
2000
- }
2001
- return merged;
2002
- };
2003
- var flattenLoadedPages = (previousPages, headPage, nextPages) => {
2004
- const merged = [];
2005
- for (const page of previousPages) merged.push(...page.nodes);
2006
- if (headPage) merged.push(...headPage.nodes);
2007
- for (const page of nextPages) merged.push(...page.nodes);
2008
- return dedupeById(merged);
2009
- };
2010
- var assertIncludeOnlyProjection = (projection) => {
2011
- if (!projection) return;
2012
- for (const [path, value] of Object.entries(projection)) {
2013
- if (!path.trim()) continue;
2014
- if (value !== 1) throw new Error("useQuery: projection must be include-only (value 1); exclusion projection is not supported");
2015
- }
2016
- };
2017
- var useQuery = (modelName, query = {}, options = {}) => {
2018
- if (typeof modelName !== "string" || modelName.trim().length === 0) throw new Error("useQuery: modelName must be a non-empty string");
2019
- assertIncludeOnlyProjection(options.projection);
2020
- const id = useId();
2021
- const enabled = options.enabled ?? true;
2022
- const ssrEnabled = options.ssr !== false;
2023
- const refreshOnMount = options.refreshOnMount === true;
2024
- const key = options.key ?? id;
2025
- const queryJson = JSON.stringify(query);
2026
- const projectionJson = options.projection ? JSON.stringify(options.projection) : "";
2027
- const sortJson = options.sort ? JSON.stringify(options.sort) : "";
2028
- const limitStr = typeof options.limit === "number" ? String(options.limit) : "";
2029
- const populateJson = options.populate ? JSON.stringify(options.populate) : "";
2030
- const paginationJson = options.pagination ? JSON.stringify(options.pagination) : "";
2031
- preparePopulateCacheOptions({
2032
- projection: options.projection,
2033
- populate: options.populate
2034
- }, "useQuery");
2035
- const isPaginated = Boolean(options.pagination);
2036
- const queryKey = computeRtsQueryKey(query, {
2037
- key,
2038
- projection: options.projection,
2039
- sort: options.sort,
2040
- limit: options.limit,
2041
- populate: options.populate,
2042
- pagination: options.pagination
2043
- });
2044
- const ssrRuntime = useRtsSsrRuntime();
2045
- if (enabled && ssrEnabled && ssrRuntime) ssrRuntime.registerQuery({
2046
- modelName,
2047
- query,
2048
- options: {
2049
- key,
2050
- projection: options.projection,
2051
- sort: options.sort,
2052
- limit: options.limit,
2053
- populate: options.populate,
2054
- pagination: options.pagination
2055
- },
2056
- queryKey
2057
- });
2058
- const seedDataRaw = useMemo(() => enabled && ssrEnabled ? ssrRuntime ? ssrRuntime.getQueryData(modelName, queryKey) : peekHydratedRtsQueryData(modelName, queryKey) : void 0, [
2059
- enabled,
2060
- ssrEnabled,
2061
- ssrRuntime,
2062
- modelName,
2063
- queryKey
2064
- ]);
2065
- const seedPageInfoRaw = useMemo(() => enabled && ssrEnabled ? ssrRuntime ? ssrRuntime.getQueryPageInfo(modelName, queryKey) : peekHydratedRtsQueryPageInfo(modelName, queryKey) : void 0, [
2066
- enabled,
2067
- ssrEnabled,
2068
- ssrRuntime,
2069
- modelName,
2070
- queryKey
2071
- ]);
2072
- const seedTotalCountRaw = useMemo(() => enabled && ssrEnabled ? ssrRuntime ? ssrRuntime.getQueryTotalCount(modelName, queryKey) : peekHydratedRtsQueryTotalCount(modelName, queryKey) : void 0, [
2073
- enabled,
2074
- ssrEnabled,
2075
- ssrRuntime,
2076
- modelName,
2077
- queryKey
2078
- ]);
2079
- const hasSeedData = Array.isArray(seedDataRaw);
2080
- const seedData = hasSeedData ? seedDataRaw : void 0;
2081
- const seedPageInfo = normalizePageInfo(seedPageInfoRaw);
2082
- const seedTotalCount = normalizeTotalCount(seedTotalCountRaw);
2083
- const seedJson = (() => {
2084
- if (!hasSeedData) return "";
2085
- try {
2086
- return JSON.stringify(seedDataRaw);
2087
- } catch {
2088
- return "";
2089
- }
2090
- })();
2091
- const seedPageInfoJson = (() => {
2092
- if (!seedPageInfo) return "";
2093
- try {
2094
- return JSON.stringify(seedPageInfo);
2095
- } catch {
2096
- return "";
2097
- }
2098
- })();
2099
- const seedTotalCountStr = seedTotalCount !== void 0 ? String(seedTotalCount) : "";
2100
- const [data, setData] = useState(() => isPaginated ? void 0 : seedData);
2101
- const [headPage, setHeadPage] = useState(() => isPaginated && seedData ? {
2102
- nodes: seedData,
2103
- ...seedPageInfo ? { pageInfo: seedPageInfo } : {}
2104
- } : null);
2105
- const [totalCount, setTotalCount] = useState(() => isPaginated ? seedTotalCount : void 0);
2106
- const [previousPages, setPreviousPages] = useState([]);
2107
- const [nextPages, setNextPages] = useState([]);
2108
- const [source, setSource] = useState(() => hasSeedData ? "cache" : void 0);
2109
- const [error, setError] = useState(void 0);
2110
- const [loading, setLoading] = useState(enabled && !hasSeedData);
2111
- const [pagingDirection, setPagingDirection] = useState(null);
2112
- const hasFirstReply = useRef(false);
2113
- const hasNetworkReply = useRef(false);
2114
- const lastDataJsonRef = useRef("");
2115
- const previousPagesRef = useRef([]);
2116
- const nextPagesRef = useRef([]);
2117
- const headPageRef = useRef(null);
2118
- const pagingDirectionRef = useRef(null);
2119
- useEffect(() => {
2120
- previousPagesRef.current = previousPages;
2121
- }, [previousPages]);
2122
- useEffect(() => {
2123
- nextPagesRef.current = nextPages;
2124
- }, [nextPages]);
2125
- useEffect(() => {
2126
- headPageRef.current = headPage;
2127
- }, [headPage]);
2128
- useEffect(() => {
2129
- pagingDirectionRef.current = pagingDirection;
2130
- }, [pagingDirection]);
2131
- useEffect(() => {
2132
- if (!ssrRuntime && enabled && ssrEnabled && hasSeedData) consumeHydratedRtsQueryData(modelName, queryKey);
2133
- hasFirstReply.current = hasSeedData;
2134
- hasNetworkReply.current = false;
2135
- lastDataJsonRef.current = seedJson;
2136
- setError(void 0);
2137
- setPreviousPages([]);
2138
- setNextPages([]);
2139
- if (!enabled) {
2140
- setLoading(false);
2141
- setData(void 0);
2142
- setHeadPage(null);
2143
- setTotalCount(void 0);
2144
- setSource(void 0);
2145
- return;
2146
- }
2147
- if (hasSeedData) {
2148
- const nextSeedData = seedData;
2149
- setLoading(false);
2150
- setSource("cache");
2151
- if (isPaginated) {
2152
- setHeadPage({
2153
- nodes: nextSeedData,
2154
- ...seedPageInfo ? { pageInfo: seedPageInfo } : {}
2155
- });
2156
- setTotalCount(seedTotalCount);
2157
- setData(void 0);
2158
- } else {
2159
- setData(nextSeedData);
2160
- setTotalCount(void 0);
2161
- setHeadPage(null);
2162
- }
2163
- return;
2164
- }
2165
- setData(void 0);
2166
- setHeadPage(null);
2167
- setTotalCount(void 0);
2168
- setLoading(true);
2169
- }, [
2170
- enabled,
2171
- ssrEnabled,
2172
- ssrRuntime,
2173
- modelName,
2174
- queryKey,
2175
- hasSeedData,
2176
- seedJson,
2177
- seedPageInfoJson,
2178
- seedTotalCountStr,
2179
- isPaginated
2180
- ]);
2181
- useEffect(() => {
2182
- if (!enabled) return;
2183
- const runInitialNetworkQuery = refreshOnMount || !hasSeedData;
2184
- const runInitialLocalQuery = !hasSeedData && !isPaginated;
2185
- const unsubscribe = registerQuery(modelName, query, {
2186
- key,
2187
- projection: options.projection,
2188
- sort: options.sort,
2189
- limit: options.limit,
2190
- populate: options.populate,
2191
- pagination: options.pagination
2192
- }, (err, result, context) => {
2193
- if (context.source === "cache" && hasNetworkReply.current) return;
2194
- if (context.source === "network") hasNetworkReply.current = true;
2195
- setLoading(false);
2196
- if (err) {
2197
- setError(err);
2198
- return;
2199
- }
2200
- if (!Array.isArray(result)) return;
2201
- if (context.source === "network" && context.isLocal && options.skipLocal && hasFirstReply.current) return;
2202
- hasFirstReply.current = true;
2203
- const networkPageInfo = context.source === "network" ? context.pageInfo : void 0;
2204
- const networkTotalCount = context.source === "network" ? context.totalCount : void 0;
2205
- const payloadForHash = isPaginated ? {
2206
- result,
2207
- pageInfo: networkPageInfo,
2208
- totalCount: networkTotalCount
2209
- } : result;
2210
- let nextJson = "";
2211
- try {
2212
- nextJson = JSON.stringify(payloadForHash);
2213
- } catch {
2214
- nextJson = "";
2215
- }
2216
- if (nextJson && nextJson === lastDataJsonRef.current) {
2217
- setSource(context.source);
2218
- return;
2219
- }
2220
- lastDataJsonRef.current = nextJson;
2221
- setSource(context.source);
2222
- setError(void 0);
2223
- if (isPaginated) {
2224
- setHeadPage({
2225
- nodes: result,
2226
- ...networkPageInfo ? { pageInfo: networkPageInfo } : {}
2227
- });
2228
- setTotalCount((current) => networkTotalCount === void 0 ? current : networkTotalCount);
2229
- return;
2230
- }
2231
- setData(result);
2232
- setTotalCount(void 0);
2233
- }, {
2234
- runInitialNetworkQuery,
2235
- runInitialLocalQuery
2236
- });
2237
- return () => {
2238
- unsubscribe?.();
2239
- };
2240
- }, [
2241
- enabled,
2242
- modelName,
2243
- queryKey,
2244
- queryJson,
2245
- projectionJson,
2246
- sortJson,
2247
- limitStr,
2248
- populateJson,
2249
- paginationJson,
2250
- hasSeedData,
2251
- refreshOnMount,
2252
- isPaginated
2253
- ]);
2254
- const effectivePageInfo = useMemo(() => {
2255
- if (!isPaginated) return void 0;
2256
- const firstPageInfo = previousPages.length > 0 ? previousPages[0]?.pageInfo : headPage?.pageInfo;
2257
- const lastPageInfo = nextPages.length > 0 ? nextPages[nextPages.length - 1]?.pageInfo : headPage?.pageInfo;
2258
- if (!firstPageInfo && !lastPageInfo) return void 0;
2259
- const hasPrevPage = Boolean(firstPageInfo?.hasPrevPage);
2260
- const hasNextPage = Boolean(lastPageInfo?.hasNextPage);
2261
- const prevCursor = firstPageInfo?.prevCursor;
2262
- const nextCursor = lastPageInfo?.nextCursor;
2263
- return {
2264
- hasPrevPage,
2265
- hasNextPage,
2266
- ...prevCursor ? { prevCursor } : {},
2267
- ...nextCursor ? { nextCursor } : {}
2268
- };
2269
- }, [
2270
- headPage,
2271
- isPaginated,
2272
- nextPages,
2273
- previousPages
2274
- ]);
2275
- const mergedPaginatedData = useMemo(() => {
2276
- if (!isPaginated) return void 0;
2277
- if (!headPage && previousPages.length === 0 && nextPages.length === 0) return void 0;
2278
- return flattenLoadedPages(previousPages, headPage, nextPages);
2279
- }, [
2280
- headPage,
2281
- isPaginated,
2282
- nextPages,
2283
- previousPages
2284
- ]);
2285
- const fetchNext = async () => {
2286
- if (!enabled || !isPaginated || !options.pagination) return false;
2287
- if (pagingDirectionRef.current) return false;
2288
- const currentHead = headPageRef.current;
2289
- const currentNextPages = nextPagesRef.current;
2290
- const cursor = currentNextPages.length > 0 ? currentNextPages[currentNextPages.length - 1]?.pageInfo?.nextCursor : currentHead?.pageInfo?.nextCursor;
2291
- const hasNextPage_0 = currentNextPages.length > 0 ? Boolean(currentNextPages[currentNextPages.length - 1]?.pageInfo?.hasNextPage) : Boolean(currentHead?.pageInfo?.hasNextPage);
2292
- if (!cursor || !hasNextPage_0) return false;
2293
- setPagingDirection("next");
2294
- setLoading(true);
2295
- setError(void 0);
2296
- try {
2297
- const response = await runNetworkQuery({
2298
- modelName,
2299
- query,
2300
- options: {
2301
- key,
2302
- projection: options.projection,
2303
- sort: options.sort,
2304
- limit: options.limit,
2305
- populate: options.populate,
2306
- pagination: {
2307
- ...options.pagination,
2308
- direction: "next",
2309
- cursor
2310
- }
2311
- }
2312
- });
2313
- if (!Array.isArray(response.data)) return false;
2314
- const page = {
2315
- nodes: response.data,
2316
- ...response.context.source === "network" && response.context.pageInfo ? { pageInfo: response.context.pageInfo } : {}
2317
- };
2318
- setSource("network");
2319
- if (response.context.source === "network" && response.context.totalCount !== void 0) setTotalCount(response.context.totalCount);
2320
- setNextPages((current_0) => [...current_0, page]);
2321
- return true;
2322
- } catch (err_0) {
2323
- setError(err_0);
2324
- return false;
2325
- } finally {
2326
- setPagingDirection(null);
2327
- setLoading(false);
2328
- }
2329
- };
2330
- const fetchPrevious = async () => {
2331
- if (!enabled || !isPaginated || !options.pagination) return false;
2332
- if (pagingDirectionRef.current) return false;
2333
- const currentHead_0 = headPageRef.current;
2334
- const currentPreviousPages = previousPagesRef.current;
2335
- const cursor_0 = currentPreviousPages.length > 0 ? currentPreviousPages[0]?.pageInfo?.prevCursor : currentHead_0?.pageInfo?.prevCursor;
2336
- const hasPrevPage_0 = currentPreviousPages.length > 0 ? Boolean(currentPreviousPages[0]?.pageInfo?.hasPrevPage) : Boolean(currentHead_0?.pageInfo?.hasPrevPage);
2337
- if (!cursor_0 || !hasPrevPage_0) return false;
2338
- setPagingDirection("prev");
2339
- setLoading(true);
2340
- setError(void 0);
2341
- try {
2342
- const response_0 = await runNetworkQuery({
2343
- modelName,
2344
- query,
2345
- options: {
2346
- key,
2347
- projection: options.projection,
2348
- sort: options.sort,
2349
- limit: options.limit,
2350
- populate: options.populate,
2351
- pagination: {
2352
- ...options.pagination,
2353
- direction: "prev",
2354
- cursor: cursor_0
2355
- }
2356
- }
2357
- });
2358
- if (!Array.isArray(response_0.data)) return false;
2359
- const page_0 = {
2360
- nodes: response_0.data,
2361
- ...response_0.context.source === "network" && response_0.context.pageInfo ? { pageInfo: response_0.context.pageInfo } : {}
2362
- };
2363
- setSource("network");
2364
- if (response_0.context.source === "network" && response_0.context.totalCount !== void 0) setTotalCount(response_0.context.totalCount);
2365
- setPreviousPages((current_1) => [page_0, ...current_1]);
2366
- return true;
2367
- } catch (err_1) {
2368
- setError(err_1);
2369
- return false;
2370
- } finally {
2371
- setPagingDirection(null);
2372
- setLoading(false);
2373
- }
2374
- };
2375
- const resetPagination = () => {
2376
- if (!isPaginated) return;
2377
- setPreviousPages([]);
2378
- setNextPages([]);
2379
- };
2380
- return useMemo(() => ({
2381
- data: isPaginated ? mergedPaginatedData : data,
2382
- pageInfo: effectivePageInfo,
2383
- totalCount: isPaginated ? totalCount : void 0,
2384
- source,
2385
- error,
2386
- loading,
2387
- fetchNext,
2388
- fetchPrevious,
2389
- resetPagination
2390
- }), [
2391
- data,
2392
- effectivePageInfo,
2393
- error,
2394
- fetchNext,
2395
- fetchPrevious,
2396
- isPaginated,
2397
- loading,
2398
- mergedPaginatedData,
2399
- source,
2400
- totalCount
2401
- ]);
2402
- };
2403
- //#endregion
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 };
2405
-
2406
- //# sourceMappingURL=rts-rSMRh4Xw.js.map