@spooky-sync/client-solid 0.0.1-canary.20 → 0.0.1-canary.201

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