@spooky-sync/client-solid2 0.0.1-canary.355 → 0.0.1-canary.357

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 CHANGED
@@ -36,6 +36,7 @@ Provider props are identical to client-solid: `config`, `fallback`, `preload` (a
36
36
 
37
37
  - **`useDb<typeof schema>()`** — the `SyncedDb<S>` instance. Same surface as client-solid: `create`, `update`, `delete`, `query`, `preload`, `run`, `bucket`, `remoteQuery`, `useRemote`, `auth`/`signOut`, `pendingMutationCount`, `subscribeToPendingMutations`, `syncHealth`/`storageHealth` + their subscribe methods.
38
38
  - **`createQuery(query, options?)`** (or `createQuery(db, query, options?)`) — reactive query. `useQuery` is a deprecated alias. Pass a thunk when the query depends on signals; read the signals inside it.
39
+ - **`useChannel(query, peers)`** (experimental peer to peer channels): opens a WebRTC channel for the component's lifetime to `peers` (a list or an accessor; own id skipped; both sides must list each other) for the rows `query` matches. `query` is a builder, built query, list, or accessor of one (nothing closes the channel) on a `-- @channel` table; only its `where` counts (evaluated per row, never registered), and the hook is typed from it. Returns `{ status(), peers(), update, create, delete }`; status is `idle` / `connecting` / `connected` / `failed` / `unsupported`. Plain `db.update` / `db.create` / `db.delete` on rows matching an open channel reach its peers in tens of ms and every query showing the row reflects them (a peer's create lands in the cache like an own optimistic create); `-- @ephemeral` fields go ONLY to peers. `orderBy` / `limit` / `offset` / `select` / `.one()` are ignored by a channel and warn unless the query has `.ignoreUnusedArgs()`. `db.channel(query, peers)` outside components. Peer writes are filtered by the client config `channels.accept[table]`. See `/docs/client/channels`.
39
40
  - `data()` — non-suspending. `[]` / `null` before the first result, keyed-reconciled live rows after.
40
41
  - `ready()` — suspending read for `<Loading fallback={...}>` (Solid 2's renamed `<Suspense>`).
41
42
  - `error()` — registration/sync failure (e.g. SSP 503 NOT_READY). Never thrown into the render tree; the sync scheduler retries underneath.
package/dist/index.cjs CHANGED
@@ -532,6 +532,68 @@ function useCrdtField(table, recordId, field) {
532
532
  return crdtField;
533
533
  }
534
534
 
535
+ //#endregion
536
+ //#region src/lib/use-channel.ts
537
+ const IDLE = {
538
+ status: "idle",
539
+ peers: []
540
+ };
541
+ /**
542
+ * Open a peer to peer channel to `peers` for the rows `query` matches, for
543
+ * the lifetime of the component. The query must be on a `-- @channel` table
544
+ * and only its `where` counts; it is checked against each row, never
545
+ * registered. Writes to matching rows (`db.update` / `db.create` /
546
+ * `db.delete`, or the channel's own) reach the peers over WebRTC in tens of
547
+ * ms; `-- @ephemeral` fields travel only this way. A connection forms when
548
+ * both sides list each other; your own id is skipped.
549
+ *
550
+ * ```ts
551
+ * const ch = useChannel(
552
+ * () => db.query('message').where({ conversation: convoId() }),
553
+ * () => [me(), other()]
554
+ * );
555
+ * ```
556
+ *
557
+ * `query` may be an accessor (re-read when it changes; `null` closes the
558
+ * channel until it returns a query again) or a list of queries.
559
+ */
560
+ function useChannel(query, peers) {
561
+ const db = useDb();
562
+ const queries = query;
563
+ const readQueries = () => typeof queries === "function" ? queries() : queries;
564
+ const readPeers = () => typeof peers === "function" ? peers() ?? [] : peers;
565
+ const [state, setState] = (0, solid_js.createSignal)(IDLE, { ownedWrite: true });
566
+ let channel = null;
567
+ let stop = () => {};
568
+ const close = () => {
569
+ stop();
570
+ channel?.close();
571
+ channel = null;
572
+ setState(IDLE);
573
+ };
574
+ const sync = (q) => {
575
+ if (!q) return close();
576
+ if (channel) return channel.setQueries(q);
577
+ channel = db.channel(q, (0, solid_js.untrack)(readPeers));
578
+ stop = channel.subscribe(setState);
579
+ };
580
+ sync((0, solid_js.untrack)(readQueries));
581
+ if (typeof queries === "function") (0, solid_js.createEffect)(() => queries(), (q) => sync(q));
582
+ if (typeof peers === "function") (0, solid_js.createEffect)(() => [...readPeers()], (next) => channel?.setPeers(next));
583
+ (0, solid_js.onCleanup)(close);
584
+ const open = () => {
585
+ if (!channel) throw new Error("This channel is closed (its query accessor returned nothing).");
586
+ return channel;
587
+ };
588
+ return {
589
+ status: () => state().status,
590
+ peers: () => state().peers,
591
+ update: ((...args) => open().update(...args)),
592
+ create: ((...args) => open().create(...args)),
593
+ delete: ((...args) => open().delete(...args))
594
+ };
595
+ }
596
+
535
597
  //#endregion
536
598
  //#region src/lib/use-feature-flag.ts
537
599
  /**
@@ -1202,6 +1264,15 @@ var SyncedDb = class {
1202
1264
  return this.sp00ky.query(table, {});
1203
1265
  }
1204
1266
  /**
1267
+ * Open a peer to peer channel to `peers` for the rows `query` matches (a
1268
+ * query on a `-- @channel` table; only its `where` counts). Prefer the
1269
+ * `useChannel` hook, which closes it with the component.
1270
+ */
1271
+ channel(query, peers) {
1272
+ if (!this.sp00ky) throw new Error("SyncedDb not initialized");
1273
+ return this.sp00ky.channel(query, peers);
1274
+ }
1275
+ /**
1205
1276
  * Run a backend operation
1206
1277
  */
1207
1278
  async run(backend, path, payload, options) {
@@ -1364,6 +1435,7 @@ exports.fromSubscription = fromSubscription;
1364
1435
  exports.useAppRelease = useAppRelease;
1365
1436
  exports.useBlurhash = useBlurhash;
1366
1437
  exports.useBucketImage = useBucketImage;
1438
+ exports.useChannel = useChannel;
1367
1439
  exports.useCrdtField = useCrdtField;
1368
1440
  exports.useDb = useDb;
1369
1441
  exports.useDownloadFile = useDownloadFile;