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