@spooky-sync/client-solid 0.0.1-canary.26 → 0.0.1-canary.261
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/AGENTS.md +70 -0
- package/README.md +20 -0
- package/dist/index.cjs +670 -158
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +379 -22
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +379 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +661 -160
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
- package/skills/{spooky-solid → sp00ky-solid}/SKILL.md +85 -14
- package/skills/{spooky-solid → sp00ky-solid}/references/file-hooks.md +34 -5
- package/src/cache/index.ts +1 -1
- package/src/cache/surrealdb-wasm-factory.ts +4 -1
- package/src/index.ts +179 -61
- package/src/lib/Blurhash.ts +54 -0
- package/src/lib/BucketImage.ts +141 -0
- package/src/lib/Sp00kyProvider.ts +104 -0
- package/src/lib/context.ts +3 -3
- package/src/lib/create-preload.ts +112 -0
- package/src/lib/models.ts +1 -1
- package/src/lib/use-app-release.ts +89 -0
- package/src/lib/use-blurhash.ts +77 -0
- package/src/lib/use-bucket-image.ts +100 -0
- package/src/lib/use-crdt-field.ts +68 -0
- package/src/lib/use-download-file.ts +66 -110
- package/src/lib/use-feature-flag.ts +50 -0
- package/src/lib/use-file-upload.ts +10 -4
- package/src/lib/use-impersonation.ts +30 -0
- package/src/lib/use-query.ts +168 -28
- package/src/lib/use-storage-status.ts +44 -0
- package/src/lib/use-sync-status.ts +64 -0
- package/src/types/index.ts +3 -4
- package/src/lib/SpookyProvider.ts +0 -55
package/dist/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Sp00kyClient, decodeBlurhash, fileToUint8Array, semverGt } from "@spooky-sync/core";
|
|
2
2
|
import { RecordId, Uuid } from "surrealdb";
|
|
3
|
-
import { createComponent, createContext, createEffect, createMemo, createSignal, mergeProps, onCleanup, onMount, useContext } from "solid-js";
|
|
3
|
+
import { children, createComponent, createContext, createEffect, createMemo, createSignal, mergeProps, on, onCleanup, onMount, useContext } from "solid-js";
|
|
4
|
+
import { createStore, reconcile } from "solid-js/store";
|
|
4
5
|
|
|
5
6
|
//#region src/lib/context.ts
|
|
6
|
-
const
|
|
7
|
+
const Sp00kyContext = createContext();
|
|
7
8
|
function useDb() {
|
|
8
|
-
const db = useContext(
|
|
9
|
-
if (!db) throw new Error("useDb must be used within a <
|
|
9
|
+
const db = useContext(Sp00kyContext);
|
|
10
|
+
if (!db) throw new Error("useDb must be used within a <Sp00kyProvider>. Wrap your app in <Sp00kyProvider config={...}>.");
|
|
10
11
|
return db;
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -21,30 +22,73 @@ function useQuery(dbOrQuery, queryOrOptions, maybeOptions) {
|
|
|
21
22
|
finalQuery = queryOrOptions;
|
|
22
23
|
options = maybeOptions;
|
|
23
24
|
} else {
|
|
24
|
-
const contextDb = useContext(
|
|
25
|
-
if (!contextDb) throw new Error("useQuery: No db argument provided and no
|
|
25
|
+
const contextDb = useContext(Sp00kyContext);
|
|
26
|
+
if (!contextDb) throw new Error("useQuery: No db argument provided and no Sp00kyContext found. Either pass a SyncedDb instance or wrap your app in <Sp00kyProvider>.");
|
|
26
27
|
db = contextDb;
|
|
27
28
|
finalQuery = dbOrQuery;
|
|
28
29
|
options = queryOrOptions;
|
|
29
30
|
}
|
|
30
|
-
const [data, setData] = createSignal(void 0);
|
|
31
31
|
const [error, setError] = createSignal(void 0);
|
|
32
32
|
const [isFetched, setIsFetched] = createSignal(false);
|
|
33
|
-
const [
|
|
33
|
+
const [isFetching, setIsFetching] = createSignal(false);
|
|
34
|
+
const [state, setState] = createStore({ value: void 0 });
|
|
35
|
+
const [version, setVersion] = createSignal(0);
|
|
36
|
+
const data = () => {
|
|
37
|
+
version();
|
|
38
|
+
return state.value;
|
|
39
|
+
};
|
|
34
40
|
let prevQueryString;
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
let runId = 0;
|
|
42
|
+
let activeUnsub;
|
|
43
|
+
let activeHash;
|
|
44
|
+
const teardownActive = () => {
|
|
45
|
+
activeUnsub?.();
|
|
46
|
+
activeUnsub = void 0;
|
|
47
|
+
};
|
|
48
|
+
const sp00ky = db.getSp00ky();
|
|
49
|
+
/**
|
|
50
|
+
* Registration can fail — the canonical case is the SSP answering 503
|
|
51
|
+
* NOT_READY while it bootstraps. Nothing here used to catch that: the
|
|
52
|
+
* rejection escaped as an unhandled promise, `isFetched` stayed false, and
|
|
53
|
+
* `isLoading()` therefore stayed true FOREVER, which is what a spinner that
|
|
54
|
+
* never resolves actually was. Surface it as `error()` instead; the sync
|
|
55
|
+
* scheduler retries the registration underneath, so a transient failure
|
|
56
|
+
* still recovers on its own.
|
|
57
|
+
*/
|
|
58
|
+
const initQuery = async (query, myRun) => {
|
|
59
|
+
try {
|
|
60
|
+
await subscribeQuery(query, myRun);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
if (myRun !== runId) return;
|
|
63
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const subscribeQuery = async (query, myRun) => {
|
|
37
67
|
const { hash } = await query.run();
|
|
68
|
+
if (myRun !== runId) return;
|
|
69
|
+
activeHash = hash;
|
|
38
70
|
setError(void 0);
|
|
39
71
|
let isFirstCall = true;
|
|
40
|
-
const unsub = await
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
72
|
+
const unsub = await sp00ky.subscribe(hash, (e) => {
|
|
73
|
+
const queryData = query.isOne ? e[0] : e;
|
|
74
|
+
const reconcileStart = performance.now();
|
|
75
|
+
setState("value", reconcile(queryData, { key: "id" }));
|
|
76
|
+
setVersion((v) => v + 1);
|
|
77
|
+
sp00ky.reportFrontendTiming(hash, performance.now() - reconcileStart);
|
|
78
|
+
const hasData = query.isOne ? queryData !== null && queryData !== void 0 : e.length > 0;
|
|
44
79
|
if (!isFirstCall || hasData) setIsFetched(true);
|
|
45
80
|
isFirstCall = false;
|
|
46
81
|
}, { immediate: true });
|
|
47
|
-
|
|
82
|
+
const unsubStatus = sp00ky.subscribeQueryStatus(hash, (status) => setIsFetching(status === "fetching"), { immediate: true });
|
|
83
|
+
const teardown = () => {
|
|
84
|
+
unsub();
|
|
85
|
+
unsubStatus();
|
|
86
|
+
};
|
|
87
|
+
if (myRun !== runId) {
|
|
88
|
+
teardown();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
activeUnsub = teardown;
|
|
48
92
|
};
|
|
49
93
|
createEffect(() => {
|
|
50
94
|
if (!(options?.enabled?.() ?? true)) {
|
|
@@ -53,22 +97,254 @@ function useQuery(dbOrQuery, queryOrOptions, maybeOptions) {
|
|
|
53
97
|
}
|
|
54
98
|
const query = typeof finalQuery === "function" ? finalQuery() : finalQuery;
|
|
55
99
|
if (!query) return;
|
|
56
|
-
const queryString =
|
|
100
|
+
const queryString = String(query.hash);
|
|
57
101
|
if (queryString === prevQueryString) return;
|
|
58
102
|
prevQueryString = queryString;
|
|
103
|
+
const myRun = ++runId;
|
|
104
|
+
teardownActive();
|
|
59
105
|
setIsFetched(false);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
106
|
+
setError(void 0);
|
|
107
|
+
initQuery(query, myRun);
|
|
108
|
+
});
|
|
109
|
+
onCleanup(() => {
|
|
110
|
+
runId++;
|
|
111
|
+
teardownActive();
|
|
112
|
+
if (options?.deregisterOnCleanup && activeHash) sp00ky.deregisterQuery(activeHash);
|
|
64
113
|
});
|
|
65
114
|
const isLoading = () => {
|
|
66
115
|
return !isFetched() && error() === void 0;
|
|
67
116
|
};
|
|
117
|
+
const isSettled = () => isFetched() && !isFetching();
|
|
68
118
|
return {
|
|
69
119
|
data,
|
|
70
120
|
error,
|
|
71
|
-
isLoading
|
|
121
|
+
isLoading,
|
|
122
|
+
isFetching,
|
|
123
|
+
isSettled
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/lib/create-preload.ts
|
|
129
|
+
/**
|
|
130
|
+
* Reactive, fire-and-forget prewarm. Resolves the query (calling it if it's a
|
|
131
|
+
* function so it tracks reactive deps), dedupes on the query's stable identity
|
|
132
|
+
* hash, and registers it via `db.preload`: a live query nobody subscribes to,
|
|
133
|
+
* evicted a ttl after it was registered unless a view mounts it first. Nothing
|
|
134
|
+
* to clean up here.
|
|
135
|
+
*
|
|
136
|
+
* Typical use: inside a list row, preload the detail query the user is likely
|
|
137
|
+
* to open next, so navigation paints from cache instead of the network.
|
|
138
|
+
*/
|
|
139
|
+
function createPreload(dbOrQuery, queryOrOptions, maybeOptions) {
|
|
140
|
+
let db;
|
|
141
|
+
let finalQuery;
|
|
142
|
+
let options;
|
|
143
|
+
if (dbOrQuery instanceof SyncedDb) {
|
|
144
|
+
db = dbOrQuery;
|
|
145
|
+
finalQuery = queryOrOptions;
|
|
146
|
+
options = maybeOptions;
|
|
147
|
+
} else {
|
|
148
|
+
const contextDb = useContext(Sp00kyContext);
|
|
149
|
+
if (!contextDb) throw new Error("createPreload: No db argument provided and no Sp00kyContext found. Either pass a SyncedDb instance or wrap your app in <Sp00kyProvider>.");
|
|
150
|
+
db = contextDb;
|
|
151
|
+
finalQuery = dbOrQuery;
|
|
152
|
+
options = queryOrOptions;
|
|
153
|
+
}
|
|
154
|
+
let prevHash;
|
|
155
|
+
createEffect(() => {
|
|
156
|
+
if (!(options?.enabled?.() ?? true)) return;
|
|
157
|
+
const query = typeof finalQuery === "function" ? finalQuery() : finalQuery;
|
|
158
|
+
if (!query) return;
|
|
159
|
+
if (query.hash === prevHash) return;
|
|
160
|
+
prevHash = query.hash;
|
|
161
|
+
db.getSp00ky().preload(query, { signal: options?.signal });
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/lib/use-sync-status.ts
|
|
167
|
+
/**
|
|
168
|
+
* Observe sync health for a "can't reach the server" banner / indicator.
|
|
169
|
+
*
|
|
170
|
+
* Backed by `db.subscribeToSyncHealth`. Individual sync failures (a transient
|
|
171
|
+
* remote 500 on query registration, a dropped socket) are absorbed by the
|
|
172
|
+
* retry and never flip this; `isDegraded()` only goes true once failures
|
|
173
|
+
* persist for the configured number of consecutive rounds (sp00ky core config
|
|
174
|
+
* `syncHealth.degradeAfterConsecutiveFailures`, default 3), and flips back on
|
|
175
|
+
* the next successful round. Must be used within a `<Sp00kyProvider>`.
|
|
176
|
+
*/
|
|
177
|
+
function useSyncStatus() {
|
|
178
|
+
const db = useDb();
|
|
179
|
+
const [health, setHealth] = createSignal(db.syncHealth);
|
|
180
|
+
onCleanup(db.subscribeToSyncHealth(setHealth));
|
|
181
|
+
return {
|
|
182
|
+
health,
|
|
183
|
+
status: () => health().status,
|
|
184
|
+
isHealthy: () => health().status === "healthy",
|
|
185
|
+
isDegraded: () => health().status === "degraded",
|
|
186
|
+
everConnected: () => health().everConnected,
|
|
187
|
+
isOffline: () => health().status === "degraded" && health().everConnected,
|
|
188
|
+
connection: () => health().connection,
|
|
189
|
+
isReconnecting: () => health().connection === "reconnecting"
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/lib/use-impersonation.ts
|
|
195
|
+
/**
|
|
196
|
+
* Observe admin impersonation (started from the DevTools). The client already
|
|
197
|
+
* shows its own warning bar with a Stop button; use this for anything extra,
|
|
198
|
+
* such as hiding actions an admin should not take on someone's behalf.
|
|
199
|
+
* Must be used within a `<Sp00kyProvider>`.
|
|
200
|
+
*/
|
|
201
|
+
function useImpersonation() {
|
|
202
|
+
const db = useDb();
|
|
203
|
+
const [impersonation, setImpersonation] = createSignal(db.auth.impersonation);
|
|
204
|
+
onCleanup(db.auth.subscribeImpersonation(setImpersonation));
|
|
205
|
+
return {
|
|
206
|
+
impersonation,
|
|
207
|
+
isImpersonating: () => impersonation() !== null,
|
|
208
|
+
stop: () => db.auth.stopImpersonating()
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/lib/use-storage-status.ts
|
|
214
|
+
/**
|
|
215
|
+
* Observe how durable the LOCAL cache is, for a "no local storage" warning.
|
|
216
|
+
*
|
|
217
|
+
* Under `localEngine: 'sqlite'` with `store: 'indexeddb'` the durable store is
|
|
218
|
+
* the OPFS SAHPool VFS, and only ONE client per bucket can hold it open: a
|
|
219
|
+
* second tab of the same app cannot get it and runs in memory instead (the
|
|
220
|
+
* engine retries first, so a closing tab's lock is usually waited out). Must be
|
|
221
|
+
* used within a `<Sp00kyProvider>`.
|
|
222
|
+
*/
|
|
223
|
+
function useStorageStatus() {
|
|
224
|
+
const db = useDb();
|
|
225
|
+
const [health, setHealth] = createSignal(db.storageHealth);
|
|
226
|
+
onCleanup(db.subscribeToStorageHealth(setHealth));
|
|
227
|
+
return {
|
|
228
|
+
health,
|
|
229
|
+
status: () => health().status,
|
|
230
|
+
isPersistent: () => health().status === "persistent",
|
|
231
|
+
isMemoryFallback: () => health().fallback
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/lib/use-crdt-field.ts
|
|
237
|
+
function useCrdtField(table, recordId, field, fallbackText) {
|
|
238
|
+
const db = useContext(Sp00kyContext);
|
|
239
|
+
if (!db) throw new Error("useCrdtField must be used within a <Sp00kyProvider>");
|
|
240
|
+
const [crdtField, setCrdtField] = createSignal(null);
|
|
241
|
+
let currentId;
|
|
242
|
+
let initialized = false;
|
|
243
|
+
createEffect(() => {
|
|
244
|
+
const id = recordId();
|
|
245
|
+
if (initialized && id === currentId) return;
|
|
246
|
+
if (currentId && crdtField()) {
|
|
247
|
+
db.getSp00ky().closeCrdtField(table, currentId, field);
|
|
248
|
+
setCrdtField(null);
|
|
249
|
+
}
|
|
250
|
+
currentId = id;
|
|
251
|
+
initialized = true;
|
|
252
|
+
if (!id) return;
|
|
253
|
+
const sp00ky = db.getSp00ky();
|
|
254
|
+
const text = fallbackText?.();
|
|
255
|
+
sp00ky.openCrdtField(table, id, field, text).then((cf) => {
|
|
256
|
+
if (currentId === id) setCrdtField(cf);
|
|
257
|
+
}).catch((err) => {
|
|
258
|
+
console.error(`[useCrdtField] Failed to open CRDT field ${table}.${field} on ${id}:`, err);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
onCleanup(() => {
|
|
262
|
+
if (currentId && crdtField()) {
|
|
263
|
+
db.getSp00ky().closeCrdtField(table, currentId, field);
|
|
264
|
+
setCrdtField(null);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
return crdtField;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/lib/use-feature-flag.ts
|
|
272
|
+
/**
|
|
273
|
+
* Subscribe to a feature flag for the currently authenticated user.
|
|
274
|
+
*
|
|
275
|
+
* Returns three Solid accessors that update reactively whenever the
|
|
276
|
+
* server-materialized assignment in `_00_user_feature` changes. Backed by
|
|
277
|
+
* the same SSP + sync pipeline that powers `useQuery`, so toggling a flag
|
|
278
|
+
* via `spky flag enable <key>` propagates to the UI without a refresh.
|
|
279
|
+
*
|
|
280
|
+
* `enabled()` is `true` when the resolved variant exists and is not 'off'.
|
|
281
|
+
* For multi-variant flags, prefer `variant()` directly.
|
|
282
|
+
*/
|
|
283
|
+
function useFeatureFlag(key, options) {
|
|
284
|
+
const handle = useDb().getSp00ky().feature(key, options);
|
|
285
|
+
const [variant, setVariant] = createSignal(handle.variant());
|
|
286
|
+
const [payload, setPayload] = createSignal(handle.payload());
|
|
287
|
+
const unsub = handle.subscribe((s) => {
|
|
288
|
+
setVariant(s.variant ?? options?.fallback);
|
|
289
|
+
setPayload(s.payload);
|
|
290
|
+
});
|
|
291
|
+
onCleanup(() => {
|
|
292
|
+
unsub();
|
|
293
|
+
handle.close();
|
|
294
|
+
});
|
|
295
|
+
return {
|
|
296
|
+
variant,
|
|
297
|
+
payload,
|
|
298
|
+
enabled: () => {
|
|
299
|
+
const v = variant();
|
|
300
|
+
return v !== void 0 && v !== "off";
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/lib/use-app-release.ts
|
|
307
|
+
async function reloadForSnapshot(snapshot) {
|
|
308
|
+
if (typeof window === "undefined") return;
|
|
309
|
+
if (snapshot.cacheBust) try {
|
|
310
|
+
if (window.caches) {
|
|
311
|
+
const keys = await window.caches.keys();
|
|
312
|
+
await Promise.all(keys.map((k) => window.caches.delete(k)));
|
|
313
|
+
}
|
|
314
|
+
if (navigator.serviceWorker) {
|
|
315
|
+
const regs = await navigator.serviceWorker.getRegistrations();
|
|
316
|
+
for (const r of regs) r.update().catch(() => {});
|
|
317
|
+
}
|
|
318
|
+
window.location.href = window.location.pathname + "?cb=" + Date.now();
|
|
319
|
+
return;
|
|
320
|
+
} catch {}
|
|
321
|
+
window.location.reload();
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Observe the app's announced release (`_00_app_release:<app>`, written by
|
|
325
|
+
* `spky deploy` / `spky release`) and compare it against the running build.
|
|
326
|
+
*
|
|
327
|
+
* Typical use: mount a small "new version available — Reload" notification
|
|
328
|
+
* gated on `updateAvailable()`, auto-invoking `reload()` when `mandatory()`
|
|
329
|
+
* (guard the auto path against reload loops with a per-version marker, since
|
|
330
|
+
* a client can reload while the deploy is still rolling out and land on the
|
|
331
|
+
* old bundle again).
|
|
332
|
+
*/
|
|
333
|
+
function useAppRelease(options) {
|
|
334
|
+
const handle = useDb().getSp00ky().appRelease(options.app, { ttl: options.ttl });
|
|
335
|
+
const [snapshot, setSnapshot] = createSignal(handle.snapshot());
|
|
336
|
+
const unsub = handle.subscribe(setSnapshot);
|
|
337
|
+
onCleanup(() => {
|
|
338
|
+
unsub();
|
|
339
|
+
handle.close();
|
|
340
|
+
});
|
|
341
|
+
const updateAvailable = () => semverGt(snapshot().version, options.currentVersion);
|
|
342
|
+
return {
|
|
343
|
+
latestVersion: () => snapshot().version,
|
|
344
|
+
updateAvailable,
|
|
345
|
+
mandatory: () => updateAvailable() && snapshot().mandatory,
|
|
346
|
+
cacheBust: () => snapshot().cacheBust,
|
|
347
|
+
reload: () => reloadForSnapshot(snapshot())
|
|
72
348
|
};
|
|
73
349
|
}
|
|
74
350
|
|
|
@@ -94,7 +370,7 @@ function useFileUpload(dbOrBucketName, maybeBucketName) {
|
|
|
94
370
|
const validate = (file) => {
|
|
95
371
|
const config = db.getBucketConfig(bucketName);
|
|
96
372
|
if (!config) return;
|
|
97
|
-
if (config.maxSize
|
|
373
|
+
if (config.maxSize !== null && config.maxSize !== void 0 && file.size > config.maxSize) {
|
|
98
374
|
const maxMB = (config.maxSize / (1024 * 1024)).toFixed(1);
|
|
99
375
|
throw new Error(`File exceeds maximum size of ${maxMB} MB.`);
|
|
100
376
|
}
|
|
@@ -106,7 +382,7 @@ function useFileUpload(dbOrBucketName, maybeBucketName) {
|
|
|
106
382
|
}
|
|
107
383
|
}
|
|
108
384
|
};
|
|
109
|
-
const upload = async (path, file) => {
|
|
385
|
+
const upload = async (path, file, options) => {
|
|
110
386
|
setError(null);
|
|
111
387
|
try {
|
|
112
388
|
validate(file);
|
|
@@ -117,7 +393,7 @@ function useFileUpload(dbOrBucketName, maybeBucketName) {
|
|
|
117
393
|
setIsUploading(true);
|
|
118
394
|
try {
|
|
119
395
|
const bytes = await fileToUint8Array(file);
|
|
120
|
-
await db.bucket(bucketName).put(path, bytes);
|
|
396
|
+
return await db.bucket(bucketName).put(path, bytes, options);
|
|
121
397
|
} catch (e) {
|
|
122
398
|
setError(e instanceof Error ? e : new Error(String(e)));
|
|
123
399
|
} finally {
|
|
@@ -167,20 +443,6 @@ function useFileUpload(dbOrBucketName, maybeBucketName) {
|
|
|
167
443
|
|
|
168
444
|
//#endregion
|
|
169
445
|
//#region src/lib/use-download-file.ts
|
|
170
|
-
const downloadCache = /* @__PURE__ */ new Map();
|
|
171
|
-
const inflightRequests = /* @__PURE__ */ new Map();
|
|
172
|
-
function cacheKey(bucket, path) {
|
|
173
|
-
return `${bucket}:${path}`;
|
|
174
|
-
}
|
|
175
|
-
function releaseEntry(key) {
|
|
176
|
-
const entry = downloadCache.get(key);
|
|
177
|
-
if (!entry) return;
|
|
178
|
-
entry.refCount--;
|
|
179
|
-
if (entry.refCount <= 0) {
|
|
180
|
-
URL.revokeObjectURL(entry.url);
|
|
181
|
-
downloadCache.delete(key);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
446
|
function useDownloadFile(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeOptions) {
|
|
185
447
|
let db;
|
|
186
448
|
let bucketName;
|
|
@@ -201,63 +463,15 @@ function useDownloadFile(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeO
|
|
|
201
463
|
const [url, setUrl] = createSignal(null);
|
|
202
464
|
const [isLoading, setIsLoading] = createSignal(false);
|
|
203
465
|
const [error, setError] = createSignal(null);
|
|
204
|
-
let
|
|
466
|
+
let lease = null;
|
|
205
467
|
let privateUrl = null;
|
|
206
|
-
let refetchTrigger;
|
|
207
468
|
const [refetchSignal, setRefetchSignal] = createSignal(0);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
currentKey = key;
|
|
215
|
-
return cached.url;
|
|
216
|
-
}
|
|
217
|
-
const inflight = inflightRequests.get(key);
|
|
218
|
-
if (inflight) {
|
|
219
|
-
const result = await inflight;
|
|
220
|
-
if (result) {
|
|
221
|
-
const entry = downloadCache.get(key);
|
|
222
|
-
if (entry) {
|
|
223
|
-
entry.refCount++;
|
|
224
|
-
currentKey = key;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return result;
|
|
228
|
-
}
|
|
229
|
-
const promise = (async () => {
|
|
230
|
-
const content = await db.bucket(bucketName).get(filePath);
|
|
231
|
-
if (!content) return null;
|
|
232
|
-
const objectUrl = URL.createObjectURL(new Blob([content]));
|
|
233
|
-
downloadCache.set(key, {
|
|
234
|
-
url: objectUrl,
|
|
235
|
-
refCount: 1
|
|
236
|
-
});
|
|
237
|
-
return objectUrl;
|
|
238
|
-
})();
|
|
239
|
-
inflightRequests.set(key, promise);
|
|
240
|
-
try {
|
|
241
|
-
const result = await promise;
|
|
242
|
-
currentKey = key;
|
|
243
|
-
return result;
|
|
244
|
-
} finally {
|
|
245
|
-
inflightRequests.delete(key);
|
|
246
|
-
}
|
|
247
|
-
} else {
|
|
248
|
-
const content = await db.bucket(bucketName).get(filePath);
|
|
249
|
-
if (!content) return null;
|
|
250
|
-
const objectUrl = URL.createObjectURL(new Blob([content]));
|
|
251
|
-
privateUrl = objectUrl;
|
|
252
|
-
return objectUrl;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
function releaseCurrentEntry() {
|
|
256
|
-
if (useCache && currentKey) {
|
|
257
|
-
releaseEntry(currentKey);
|
|
258
|
-
currentKey = null;
|
|
259
|
-
}
|
|
260
|
-
if (!useCache && privateUrl) {
|
|
469
|
+
/** Consumed by the next effect run, so `refetch()` bypasses every layer once. */
|
|
470
|
+
let reloadOnce = false;
|
|
471
|
+
function releaseCurrent() {
|
|
472
|
+
lease?.release();
|
|
473
|
+
lease = null;
|
|
474
|
+
if (privateUrl) {
|
|
261
475
|
URL.revokeObjectURL(privateUrl);
|
|
262
476
|
privateUrl = null;
|
|
263
477
|
}
|
|
@@ -265,29 +479,40 @@ function useDownloadFile(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeO
|
|
|
265
479
|
createEffect(() => {
|
|
266
480
|
const filePath = path();
|
|
267
481
|
refetchSignal();
|
|
268
|
-
|
|
482
|
+
releaseCurrent();
|
|
269
483
|
if (!filePath) {
|
|
270
484
|
setUrl(null);
|
|
271
485
|
setIsLoading(false);
|
|
272
486
|
setError(null);
|
|
273
487
|
return;
|
|
274
488
|
}
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const cached = downloadCache.get(key);
|
|
278
|
-
if (cached) {
|
|
279
|
-
cached.refCount++;
|
|
280
|
-
currentKey = key;
|
|
281
|
-
setUrl(cached.url);
|
|
282
|
-
setIsLoading(false);
|
|
283
|
-
setError(null);
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
489
|
+
const reload = reloadOnce;
|
|
490
|
+
reloadOnce = false;
|
|
287
491
|
let cancelled = false;
|
|
288
492
|
setIsLoading(true);
|
|
289
493
|
setError(null);
|
|
290
|
-
|
|
494
|
+
const bucket = db.bucket(bucketName);
|
|
495
|
+
(useCache ? bucket.url(filePath, {
|
|
496
|
+
persist: options.persist !== false,
|
|
497
|
+
pin: options.pin,
|
|
498
|
+
revalidate: options.revalidate,
|
|
499
|
+
reload
|
|
500
|
+
}).then((acquired) => {
|
|
501
|
+
if (!acquired) return null;
|
|
502
|
+
if (cancelled) {
|
|
503
|
+
acquired.release();
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
lease = acquired;
|
|
507
|
+
return acquired.url;
|
|
508
|
+
}) : bucket.read(filePath, {
|
|
509
|
+
persist: false,
|
|
510
|
+
reload: true
|
|
511
|
+
}).then((blob) => {
|
|
512
|
+
if (!blob || cancelled) return null;
|
|
513
|
+
privateUrl = URL.createObjectURL(blob);
|
|
514
|
+
return privateUrl;
|
|
515
|
+
})).then((result) => {
|
|
291
516
|
if (!cancelled) {
|
|
292
517
|
setUrl(result);
|
|
293
518
|
setIsLoading(false);
|
|
@@ -303,18 +528,11 @@ function useDownloadFile(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeO
|
|
|
303
528
|
});
|
|
304
529
|
});
|
|
305
530
|
onCleanup(() => {
|
|
306
|
-
|
|
531
|
+
releaseCurrent();
|
|
307
532
|
});
|
|
308
533
|
const refetch = () => {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (entry) {
|
|
312
|
-
URL.revokeObjectURL(entry.url);
|
|
313
|
-
downloadCache.delete(currentKey);
|
|
314
|
-
}
|
|
315
|
-
currentKey = null;
|
|
316
|
-
}
|
|
317
|
-
refetchTrigger();
|
|
534
|
+
reloadOnce = true;
|
|
535
|
+
setRefetchSignal((n) => n + 1);
|
|
318
536
|
};
|
|
319
537
|
return {
|
|
320
538
|
url,
|
|
@@ -325,26 +543,251 @@ function useDownloadFile(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeO
|
|
|
325
543
|
}
|
|
326
544
|
|
|
327
545
|
//#endregion
|
|
328
|
-
//#region src/lib/
|
|
329
|
-
function
|
|
546
|
+
//#region src/lib/use-blurhash.ts
|
|
547
|
+
function useBlurhash(dbOrBucketName, bucketNameOrPath, maybePath) {
|
|
548
|
+
let db;
|
|
549
|
+
let bucketName;
|
|
550
|
+
let path;
|
|
551
|
+
if (typeof dbOrBucketName === "string") {
|
|
552
|
+
db = useDb();
|
|
553
|
+
bucketName = dbOrBucketName;
|
|
554
|
+
path = bucketNameOrPath;
|
|
555
|
+
} else {
|
|
556
|
+
db = dbOrBucketName;
|
|
557
|
+
bucketName = bucketNameOrPath;
|
|
558
|
+
path = maybePath;
|
|
559
|
+
}
|
|
560
|
+
const [hash, setHash] = createSignal(null);
|
|
561
|
+
const [isLoading, setIsLoading] = createSignal(false);
|
|
562
|
+
createEffect(() => {
|
|
563
|
+
const filePath = path();
|
|
564
|
+
if (!filePath) {
|
|
565
|
+
setHash(null);
|
|
566
|
+
setIsLoading(false);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
let cancelled = false;
|
|
570
|
+
setIsLoading(true);
|
|
571
|
+
db.bucket(bucketName).blurhash(filePath).then((result) => {
|
|
572
|
+
if (cancelled) return;
|
|
573
|
+
setHash(result);
|
|
574
|
+
setIsLoading(false);
|
|
575
|
+
}).catch(() => {
|
|
576
|
+
if (cancelled) return;
|
|
577
|
+
setHash(null);
|
|
578
|
+
setIsLoading(false);
|
|
579
|
+
});
|
|
580
|
+
onCleanup(() => {
|
|
581
|
+
cancelled = true;
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
return {
|
|
585
|
+
hash,
|
|
586
|
+
isLoading
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
//#endregion
|
|
591
|
+
//#region src/lib/use-bucket-image.ts
|
|
592
|
+
function useBucketImage(dbOrBucketName, bucketNameOrPath, pathOrOptions, maybeOptions) {
|
|
593
|
+
let db;
|
|
594
|
+
let bucketName;
|
|
595
|
+
let path;
|
|
596
|
+
let options;
|
|
597
|
+
if (typeof dbOrBucketName === "string") {
|
|
598
|
+
db = useDb();
|
|
599
|
+
bucketName = dbOrBucketName;
|
|
600
|
+
path = bucketNameOrPath;
|
|
601
|
+
options = pathOrOptions ?? {};
|
|
602
|
+
} else {
|
|
603
|
+
db = dbOrBucketName;
|
|
604
|
+
bucketName = bucketNameOrPath;
|
|
605
|
+
path = pathOrOptions;
|
|
606
|
+
options = maybeOptions ?? {};
|
|
607
|
+
}
|
|
608
|
+
const wantHash = options.blurhash !== false;
|
|
609
|
+
const { hash } = useBlurhash(db, bucketName, () => wantHash ? path() : null);
|
|
610
|
+
const file = useDownloadFile(db, bucketName, path, options);
|
|
611
|
+
const [ready, setReady] = createSignal(false);
|
|
612
|
+
createEffect(on(file.url, () => setReady(false), { defer: true }));
|
|
613
|
+
const gate = (img) => {
|
|
614
|
+
const done = () => setReady(true);
|
|
615
|
+
if (typeof img.decode === "function") img.decode().then(done, done);
|
|
616
|
+
else if (img.complete) done();
|
|
617
|
+
else {
|
|
618
|
+
img.onload = done;
|
|
619
|
+
img.onerror = done;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
return {
|
|
623
|
+
...file,
|
|
624
|
+
blurhash: hash,
|
|
625
|
+
ready,
|
|
626
|
+
gate
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
//#endregion
|
|
631
|
+
//#region src/lib/Blurhash.ts
|
|
632
|
+
/**
|
|
633
|
+
* A blurhash painted onto a canvas, once per hash change. Size the canvas via
|
|
634
|
+
* `class`/`style` (e.g. `absolute inset-0 w-full h-full`); the internal decode
|
|
635
|
+
* resolution stays tiny regardless of the displayed size.
|
|
636
|
+
*/
|
|
637
|
+
function Blurhash(props) {
|
|
638
|
+
if (typeof document === "undefined") return null;
|
|
639
|
+
const canvas = document.createElement("canvas");
|
|
640
|
+
createEffect(() => {
|
|
641
|
+
canvas.className = props.class ?? "";
|
|
642
|
+
});
|
|
643
|
+
createEffect(() => {
|
|
644
|
+
canvas.style.cssText = props.style ?? "";
|
|
645
|
+
});
|
|
646
|
+
createEffect(() => {
|
|
647
|
+
const width = props.width ?? 32;
|
|
648
|
+
const height = props.height ?? 32;
|
|
649
|
+
canvas.width = width;
|
|
650
|
+
canvas.height = height;
|
|
651
|
+
const hash = props.hash;
|
|
652
|
+
if (!hash) return;
|
|
653
|
+
try {
|
|
654
|
+
const pixels = decodeBlurhash(hash, width, height, props.punch ?? 1);
|
|
655
|
+
const ctx = canvas.getContext("2d");
|
|
656
|
+
if (!ctx) return;
|
|
657
|
+
const imageData = ctx.createImageData(width, height);
|
|
658
|
+
imageData.data.set(pixels);
|
|
659
|
+
ctx.putImageData(imageData, 0, 0);
|
|
660
|
+
} catch {}
|
|
661
|
+
});
|
|
662
|
+
return canvas;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
//#endregion
|
|
666
|
+
//#region src/lib/BucketImage.ts
|
|
667
|
+
const LAYER_STYLE = "position:absolute;inset:0;width:100%;height:100%;";
|
|
668
|
+
/**
|
|
669
|
+
* A bucket image that never pops in: it layers (bottom to top) your `fallback`
|
|
670
|
+
* plate, the automatically stored blurhash, and the real image, which stays
|
|
671
|
+
* transparent until the bitmap is DECODED and then crossfades over the
|
|
672
|
+
* placeholders. Placeholder layers unmount once the fade settles. Respects
|
|
673
|
+
* prefers-reduced-motion (instant swap). The container is made
|
|
674
|
+
* `position: relative` unless your `class` positions it already.
|
|
675
|
+
*
|
|
676
|
+
* ```tsx
|
|
677
|
+
* <BucketImage bucket="covers" path={row.cover_key} class="absolute inset-0"
|
|
678
|
+
* fallback={<MyPlate />} alt="" />
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
function BucketImage(props) {
|
|
682
|
+
if (typeof document === "undefined") return null;
|
|
683
|
+
const image = useBucketImage(props.bucket, () => props.path, {
|
|
684
|
+
...props.options,
|
|
685
|
+
blurhash: props.blurhash !== false
|
|
686
|
+
});
|
|
687
|
+
const reducedMotion = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
688
|
+
const root = document.createElement("div");
|
|
689
|
+
createEffect(() => {
|
|
690
|
+
root.className = props.class ?? "";
|
|
691
|
+
});
|
|
692
|
+
onMount(() => {
|
|
693
|
+
if (getComputedStyle(root).position === "static") root.style.position = "relative";
|
|
694
|
+
});
|
|
695
|
+
const placeholder = document.createElement("div");
|
|
696
|
+
placeholder.style.cssText = LAYER_STYLE;
|
|
697
|
+
const fallbackHolder = document.createElement("div");
|
|
698
|
+
fallbackHolder.style.cssText = LAYER_STYLE;
|
|
699
|
+
placeholder.append(fallbackHolder);
|
|
700
|
+
const resolvedFallback = children(() => props.fallback);
|
|
701
|
+
createEffect(() => {
|
|
702
|
+
const nodes = resolvedFallback.toArray().filter((node) => node instanceof Node);
|
|
703
|
+
fallbackHolder.replaceChildren(...nodes);
|
|
704
|
+
});
|
|
705
|
+
const hashCanvas = Blurhash({
|
|
706
|
+
get hash() {
|
|
707
|
+
return image.blurhash();
|
|
708
|
+
},
|
|
709
|
+
style: LAYER_STYLE
|
|
710
|
+
});
|
|
711
|
+
if (hashCanvas instanceof Node) placeholder.append(hashCanvas);
|
|
712
|
+
const img = document.createElement("img");
|
|
713
|
+
img.decoding = "async";
|
|
714
|
+
img.style.cssText = `${LAYER_STYLE}opacity:0;`;
|
|
715
|
+
createEffect(() => {
|
|
716
|
+
img.className = props.imgClass ?? "";
|
|
717
|
+
});
|
|
718
|
+
createEffect(() => {
|
|
719
|
+
img.style.objectFit = props.fit ?? "cover";
|
|
720
|
+
});
|
|
721
|
+
createEffect(() => {
|
|
722
|
+
img.alt = props.alt ?? "";
|
|
723
|
+
});
|
|
724
|
+
createEffect(() => {
|
|
725
|
+
img.style.transition = reducedMotion ? "none" : `opacity ${props.transition ?? 300}ms ${props.easing ?? "cubic-bezier(0.16, 1, 0.3, 1)"}`;
|
|
726
|
+
});
|
|
727
|
+
createEffect(() => {
|
|
728
|
+
const url = image.url();
|
|
729
|
+
if (!url) {
|
|
730
|
+
img.removeAttribute("src");
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
img.src = url;
|
|
734
|
+
image.gate(img);
|
|
735
|
+
});
|
|
736
|
+
createEffect(() => {
|
|
737
|
+
img.style.opacity = image.ready() ? "1" : "0";
|
|
738
|
+
});
|
|
739
|
+
const [settled, setSettled] = createSignal(false);
|
|
740
|
+
createEffect(() => {
|
|
741
|
+
if (!image.ready()) {
|
|
742
|
+
setSettled(false);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const wait = (reducedMotion ? 0 : props.transition ?? 300) + 120;
|
|
746
|
+
const timer = setTimeout(() => setSettled(true), wait);
|
|
747
|
+
onCleanup(() => clearTimeout(timer));
|
|
748
|
+
});
|
|
749
|
+
createEffect(() => {
|
|
750
|
+
if (settled()) placeholder.remove();
|
|
751
|
+
else if (!placeholder.isConnected) root.insertBefore(placeholder, img);
|
|
752
|
+
});
|
|
753
|
+
root.append(placeholder, img);
|
|
754
|
+
return root;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
//#endregion
|
|
758
|
+
//#region src/lib/Sp00kyProvider.ts
|
|
759
|
+
function Sp00kyProvider(props) {
|
|
330
760
|
const merged = mergeProps({ fallback: void 0 }, props);
|
|
331
761
|
const [db, setDb] = createSignal(void 0);
|
|
762
|
+
let disposed = false;
|
|
763
|
+
onCleanup(() => {
|
|
764
|
+
disposed = true;
|
|
765
|
+
});
|
|
332
766
|
onMount(async () => {
|
|
333
767
|
try {
|
|
334
768
|
const instance = new SyncedDb(merged.config);
|
|
335
769
|
await instance.init();
|
|
770
|
+
if (disposed) {
|
|
771
|
+
await instance.close();
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
if (merged.preload) try {
|
|
775
|
+
await merged.preload(instance);
|
|
776
|
+
} catch (e) {
|
|
777
|
+
console.error("Sp00kyProvider: preload failed; revealing UI anyway", e);
|
|
778
|
+
}
|
|
336
779
|
setDb(() => instance);
|
|
337
780
|
merged.onReady?.(instance);
|
|
338
781
|
} catch (e) {
|
|
339
782
|
const error = e instanceof Error ? e : new Error(String(e));
|
|
340
783
|
if (merged.onError) merged.onError(error);
|
|
341
|
-
else console.error("
|
|
784
|
+
else console.error("Sp00kyProvider: Failed to initialize database", error);
|
|
342
785
|
}
|
|
343
786
|
});
|
|
344
787
|
return createMemo(() => {
|
|
345
788
|
const instance = db();
|
|
346
789
|
if (!instance) return merged.fallback;
|
|
347
|
-
return createComponent(
|
|
790
|
+
return createComponent(Sp00kyContext.Provider, {
|
|
348
791
|
value: instance,
|
|
349
792
|
get children() {
|
|
350
793
|
return merged.children;
|
|
@@ -356,69 +799,94 @@ function SpookyProvider(props) {
|
|
|
356
799
|
//#endregion
|
|
357
800
|
//#region src/index.ts
|
|
358
801
|
/**
|
|
359
|
-
* SyncedDb - A thin wrapper around
|
|
360
|
-
* Delegates all logic to the underlying
|
|
802
|
+
* SyncedDb - A thin wrapper around sp00ky-ts for Solid.js integration
|
|
803
|
+
* Delegates all logic to the underlying sp00ky-ts instance
|
|
361
804
|
*/
|
|
362
805
|
var SyncedDb = class {
|
|
363
806
|
constructor(config) {
|
|
364
|
-
this.
|
|
807
|
+
this.sp00ky = null;
|
|
365
808
|
this._initialized = false;
|
|
366
809
|
this.config = config;
|
|
367
810
|
}
|
|
368
|
-
|
|
369
|
-
if (!this.
|
|
370
|
-
return this.
|
|
811
|
+
getSp00ky() {
|
|
812
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
813
|
+
return this.sp00ky;
|
|
371
814
|
}
|
|
372
815
|
/**
|
|
373
|
-
* Initialize the
|
|
816
|
+
* Initialize the sp00ky-ts instance
|
|
374
817
|
*/
|
|
375
818
|
async init() {
|
|
376
819
|
if (this._initialized) return;
|
|
377
|
-
this.
|
|
378
|
-
await this.
|
|
820
|
+
this.sp00ky = new Sp00kyClient(this.config);
|
|
821
|
+
await this.sp00ky.init();
|
|
379
822
|
this._initialized = true;
|
|
380
823
|
}
|
|
381
824
|
/**
|
|
825
|
+
* Tear down the client: leaves the tabs broker, closes the local store and
|
|
826
|
+
* remote socket, and frees the wasm circuit. Without this a remounted provider
|
|
827
|
+
* (or an HMR reload) strands a whole client, and the abandoned wasm heaps stay
|
|
828
|
+
* resident because V8 cannot see how much wasm memory a dropped wrapper holds.
|
|
829
|
+
*/
|
|
830
|
+
async close() {
|
|
831
|
+
const instance = this.sp00ky;
|
|
832
|
+
this.sp00ky = null;
|
|
833
|
+
this._initialized = false;
|
|
834
|
+
if (instance) await instance.close();
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
382
837
|
* Create a new record in the database
|
|
383
838
|
*/
|
|
384
839
|
async create(id, payload) {
|
|
385
|
-
if (!this.
|
|
386
|
-
await this.
|
|
840
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
841
|
+
await this.sp00ky.create(id, payload);
|
|
387
842
|
}
|
|
388
843
|
/**
|
|
389
844
|
* Update an existing record in the database
|
|
390
845
|
*/
|
|
391
846
|
async update(tableName, recordId, payload, options) {
|
|
392
|
-
if (!this.
|
|
393
|
-
await this.
|
|
847
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
848
|
+
await this.sp00ky.update(tableName, recordId, payload, options);
|
|
394
849
|
}
|
|
395
850
|
/**
|
|
396
851
|
* Delete an existing record in the database
|
|
397
852
|
*/
|
|
398
853
|
async delete(tableName, selector) {
|
|
399
|
-
if (!this.
|
|
400
|
-
|
|
401
|
-
|
|
854
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
855
|
+
const isRecordId = selector instanceof RecordId || selector?.constructor?.name === "RecordId";
|
|
856
|
+
let id;
|
|
857
|
+
if (typeof selector === "string") id = selector;
|
|
858
|
+
else if (isRecordId) id = `${tableName}:${selector.id}`;
|
|
859
|
+
else throw new Error("Only string ID or RecordId selectors are supported currently with core");
|
|
860
|
+
await this.sp00ky.delete(tableName, id);
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Preload/prewarm a built query into the local cache without registering a
|
|
864
|
+
* live view. Fetches once and stores the rows (+ embedded related children)
|
|
865
|
+
* locally so a later `useQuery` for the same data paints instantly. Best-effort.
|
|
866
|
+
*/
|
|
867
|
+
async preload(finalQuery, options) {
|
|
868
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
869
|
+
await this.sp00ky.preload(finalQuery, options);
|
|
402
870
|
}
|
|
403
871
|
/**
|
|
404
872
|
* Query data from the database
|
|
405
873
|
*/
|
|
406
874
|
query(table) {
|
|
407
|
-
if (!this.
|
|
408
|
-
return this.
|
|
875
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
876
|
+
return this.sp00ky.query(table, {});
|
|
409
877
|
}
|
|
410
878
|
/**
|
|
411
879
|
* Run a backend operation
|
|
412
880
|
*/
|
|
413
881
|
async run(backend, path, payload, options) {
|
|
414
|
-
if (!this.
|
|
415
|
-
await this.
|
|
882
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
883
|
+
await this.sp00ky.run(backend, path, payload, options);
|
|
416
884
|
}
|
|
417
885
|
/**
|
|
418
886
|
* Authenticate with the database
|
|
419
887
|
*/
|
|
420
888
|
async authenticate(token) {
|
|
421
|
-
await this.
|
|
889
|
+
await this.sp00ky?.authenticate(token);
|
|
422
890
|
return new RecordId("user", "me");
|
|
423
891
|
}
|
|
424
892
|
/**
|
|
@@ -432,48 +900,81 @@ var SyncedDb = class {
|
|
|
432
900
|
* Sign out, clear session and local storage
|
|
433
901
|
*/
|
|
434
902
|
async signOut() {
|
|
435
|
-
if (!this.
|
|
436
|
-
await this.
|
|
903
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
904
|
+
await this.sp00ky.auth.signOut();
|
|
437
905
|
}
|
|
438
906
|
/**
|
|
439
907
|
* Execute a function with direct access to the remote database connection
|
|
440
908
|
*/
|
|
441
909
|
async useRemote(fn) {
|
|
442
|
-
if (!this.
|
|
443
|
-
return await this.
|
|
910
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
911
|
+
return await this.sp00ky.useRemote(fn);
|
|
444
912
|
}
|
|
445
913
|
/**
|
|
446
914
|
* Access the remote database service directly
|
|
447
915
|
*/
|
|
448
916
|
get remote() {
|
|
449
|
-
if (!this.
|
|
450
|
-
return this.
|
|
917
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
918
|
+
return this.sp00ky.remoteClient;
|
|
451
919
|
}
|
|
452
920
|
/**
|
|
453
921
|
* Access the local database service directly
|
|
454
922
|
*/
|
|
455
923
|
get local() {
|
|
456
|
-
if (!this.
|
|
457
|
-
return this.
|
|
924
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
925
|
+
return this.sp00ky.localClient;
|
|
458
926
|
}
|
|
459
927
|
/**
|
|
460
928
|
* Access the auth service
|
|
461
929
|
*/
|
|
462
930
|
get auth() {
|
|
463
|
-
if (!this.
|
|
464
|
-
return this.
|
|
931
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
932
|
+
return this.sp00ky.auth;
|
|
465
933
|
}
|
|
466
934
|
get pendingMutationCount() {
|
|
467
|
-
if (!this.
|
|
468
|
-
return this.
|
|
935
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
936
|
+
return this.sp00ky.pendingMutationCount;
|
|
937
|
+
}
|
|
938
|
+
/** Diagnostic — see `Sp00kyClient.liveRetryCount`. */
|
|
939
|
+
get liveRetryCount() {
|
|
940
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
941
|
+
return this.sp00ky.liveRetryCount;
|
|
469
942
|
}
|
|
470
943
|
subscribeToPendingMutations(cb) {
|
|
471
|
-
if (!this.
|
|
472
|
-
return this.
|
|
944
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
945
|
+
return this.sp00ky.subscribeToPendingMutations(cb);
|
|
946
|
+
}
|
|
947
|
+
/** Current sync-health snapshot. See {@link useSyncStatus}. */
|
|
948
|
+
get syncHealth() {
|
|
949
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
950
|
+
return this.sp00ky.syncHealth;
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Observe sync health. Fires immediately with the current status and again
|
|
954
|
+
* on every healthy↔degraded transition. Prefer the `useSyncStatus` hook in
|
|
955
|
+
* components; this is the imperative escape hatch.
|
|
956
|
+
*/
|
|
957
|
+
subscribeToSyncHealth(cb) {
|
|
958
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
959
|
+
return this.sp00ky.subscribeToSyncHealth(cb);
|
|
960
|
+
}
|
|
961
|
+
/** Current local-store durability snapshot. See {@link useStorageStatus}. */
|
|
962
|
+
get storageHealth() {
|
|
963
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
964
|
+
return this.sp00ky.storageHealth;
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Observe local-store durability. Fires immediately with the current snapshot
|
|
968
|
+
* and again on change. Prefer the `useStorageStatus` hook in components; this
|
|
969
|
+
* is the imperative escape hatch.
|
|
970
|
+
*/
|
|
971
|
+
subscribeToStorageHealth(cb) {
|
|
972
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
973
|
+
return this.sp00ky.subscribeToStorageHealth(cb);
|
|
473
974
|
}
|
|
474
975
|
bucket(name) {
|
|
475
|
-
if (!this.
|
|
476
|
-
return this.
|
|
976
|
+
if (!this.sp00ky) throw new Error("SyncedDb not initialized");
|
|
977
|
+
return this.sp00ky.bucket(name);
|
|
477
978
|
}
|
|
478
979
|
getBucketConfig(name) {
|
|
479
980
|
return this.config.schema.buckets?.find((b) => b.name === name);
|
|
@@ -481,5 +982,5 @@ var SyncedDb = class {
|
|
|
481
982
|
};
|
|
482
983
|
|
|
483
984
|
//#endregion
|
|
484
|
-
export { RecordId,
|
|
985
|
+
export { Blurhash, BucketImage, RecordId, Sp00kyProvider, SyncedDb, Uuid, createPreload, useAppRelease, useBlurhash, useBucketImage, useCrdtField, useDb, useDownloadFile, useFeatureFlag, useFileUpload, useImpersonation, useQuery, useStorageStatus, useSyncStatus };
|
|
485
986
|
//# sourceMappingURL=index.js.map
|