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