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