@pgxsinkit/client 0.1.41 → 0.1.48

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.
@@ -451,7 +451,14 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
451
451
  // Activate any lazy relations the query reads BEFORE registering the live query (ADR-0021) — the worker
452
452
  // owns the real client, so its `prepareQuery` does the real work; the tab's `prepareQuery` is a no-op.
453
453
  // Scan the ORIGINAL sql (the wrap only adds an outer `SELECT * FROM (...)`; table refs are inside).
454
- await active.prepareQuery({ sql, ...(use ? { use: use as SyncTableName<TRegistry>[] } : {}) });
454
+ // The activated keys travel back on the initial snapshot: activation is stream-start, not catch-up,
455
+ // so the tab needs them to await `groupReady` (its `hydrating` signal) without blocking this register.
456
+ const prepared = await active.prepareQuery({ sql, ...(use ? { use: use as SyncTableName<TRegistry>[] } : {}) });
457
+ // Hydration spans EVERY referenced consistency group (eager AND lazy), computed on the real client the
458
+ // worker owns: the referenced tables whose group is not yet caught up at subscribe time. Empty (steady
459
+ // state / sync disabled) → no `hydrated` gating at all; otherwise it drives both the `live-initial`
460
+ // `hydratingTables` field (the tab's `hydrating` source) and the `live-hydrated` block below.
461
+ const hydratingTables = active.hydratingTablesFor({ sql, ...(use ? { use: use as string[] } : {}) });
455
462
 
456
463
  const live = active.pglite.live;
457
464
  // Render the query safe to materialise: a JOIN with same-named columns fails BOTH live APIs' temp-view
@@ -469,7 +476,20 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
469
476
 
470
477
  const state = seedLiveDiffState(registered.initialResults.rows, pkColumns);
471
478
  // Initial snapshot verbatim (not a diff), correlated to the subscribe request so the tab resolves.
472
- postBridgeMessage(port, codec, "live-initial", { queryId, rows: registered.initialResults.rows }, id);
479
+ postBridgeMessage(
480
+ port,
481
+ codec,
482
+ "live-initial",
483
+ {
484
+ queryId,
485
+ rows: registered.initialResults.rows,
486
+ // `lazyTables` = the activated lazy relations (observability); `hydratingTables` = the referenced
487
+ // groups (eager OR lazy) still catching up, which the tab turns into its `hydrated` promise.
488
+ ...(prepared.lazyTables.length > 0 ? { lazyTables: prepared.lazyTables.map(String) } : {}),
489
+ ...(hydratingTables.length > 0 ? { hydratingTables: [...hydratingTables] } : {}),
490
+ },
491
+ id,
492
+ );
473
493
 
474
494
  const listener = (results: { rows: Record<string, unknown>[] }) => {
475
495
  // `subscribe` fires only on a real change (never the initial), so every fire yields a genuine delta.
@@ -481,6 +501,23 @@ export function defineSyncWorker<const TRegistry extends SyncTableRegistry>(
481
501
  port,
482
502
  unsubscribe: () => void registered.unsubscribe(listener),
483
503
  });
504
+
505
+ // Hydration completion travels the SAME port as the diff stream, so ordering is guaranteed: await the
506
+ // pending groups' catch-up (eager AND lazy), force one refresh of the live query against the caught-up
507
+ // store (its listener posts the rows as a live-diff, synchronously inside the refresh), THEN post
508
+ // `live-hydrated`. The tab therefore always has the caught-up rows before its `hydrated` resolves — a
509
+ // readiness signal on a side channel (e.g. the `groupReady` broadcast) can beat the rows and flash a
510
+ // false "empty" state. Skipped entirely when nothing is pending (steady state / sync disabled).
511
+ if (hydratingTables.length > 0) {
512
+ void Promise.all(hydratingTables.map((key) => active.groupReady(key as SyncTableName<TRegistry>)))
513
+ .then(async () => {
514
+ if (!liveSubs.has(queryId)) return; // unsubscribed while catching up
515
+ await registered.refresh();
516
+ if (!liveSubs.has(queryId)) return;
517
+ postBridgeMessage(port, codec, "live-hydrated", { queryId });
518
+ })
519
+ .catch(() => undefined); // a torn-down engine mid-catch-up must not surface as an unhandled rejection
520
+ }
484
521
  };
485
522
 
486
523
  // ─── Per-port message routing ────────────────────────────────────────────────────────────────────
@@ -71,6 +71,7 @@ export type BridgeMessageType =
71
71
  | "rpc-result"
72
72
  | "live-initial"
73
73
  | "live-diff"
74
+ | "live-hydrated"
74
75
  | "event";
75
76
 
76
77
  // ─── Payload shapes ───────────────────────────────────────────────────────────
@@ -304,6 +305,31 @@ export interface SubscribePayload {
304
305
  export interface LiveInitialPayload {
305
306
  queryId: string;
306
307
  rows: Record<string, unknown>[];
308
+ /**
309
+ * The `lazy` relations the worker's guard activated for this query (ADR-0021) — the relations held out
310
+ * of the eager boot set. Observability only: {@link hydratingTables}, not this, drives the tab's
311
+ * `hydrated` promise.
312
+ */
313
+ lazyTables?: string[];
314
+ /**
315
+ * The referenced consistency groups' member tables (eager OR lazy) that were NOT YET caught up when this
316
+ * snapshot was taken (ADR-0021 / ADR-0032). Non-empty → the tab builds a `hydrated` promise the worker
317
+ * settles via `live-hydrated` (posted after the catch-up rows on this same port). Absent → every
318
+ * referenced group was already ready (steady state) or sync is disabled: nothing to hydrate.
319
+ */
320
+ hydratingTables?: string[];
321
+ }
322
+
323
+ /**
324
+ * worker → tab: the subscription's lazy relations have completed their initial catch-up AND the live
325
+ * query has been refreshed against the caught-up store (ADR-0021). Posted on the SAME port as the
326
+ * live-diff stream, strictly after the refresh-triggered diff — so when the tab sees this, the rows
327
+ * reflecting the catch-up have already been delivered. Resolves the tab subscription's `hydrated`
328
+ * promise (the React hooks' `hydrating` gate); never sent for a query with no pending groups (steady
329
+ * state / sync disabled), i.e. an empty `live-initial` {@link LiveInitialPayload.hydratingTables}.
330
+ */
331
+ export interface LiveHydratedPayload {
332
+ queryId: string;
307
333
  }
308
334
 
309
335
  /**