@pgxsinkit/react 0.2.7 → 0.5.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pgxsinkit contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,11 +1,11 @@
1
1
  import { type DependencyList, type ReactNode } from "react";
2
- import { type ClientPGlite, type MutationListOptions, type MutationSummaryDetail, type SyncClient } from "@pgxsinkit/client";
2
+ import { type PgwasmClient, type MutationListOptions, type MutationSummaryDetail, type SyncClient } from "@pgxsinkit/client";
3
3
  import type { MutationSummary, SyncTableName, SyncTableRegistry } from "@pgxsinkit/contracts";
4
4
  import { type SelectedFields } from "./remap-live-row";
5
5
  /**
6
6
  * Minimal interface satisfied by every Drizzle select/query builder.
7
7
  * Calling `.toSQL()` extracts the SQL string and positional params without
8
- * executing the query, so they can be fed into PGlite's live query API. `_.selectedFields` is the
8
+ * executing the query, so they can be fed into pgwasm's live query API. `_.selectedFields` is the
9
9
  * select's field metadata, used to remap PGlite's snake_case rows back to the builder's field keys
10
10
  * ({@link remapLiveRow}) — without it the typed rows would carry the underlying column names.
11
11
  */
@@ -51,8 +51,8 @@ export declare function createSyncClientHooks<TRegistry extends SyncTableRegistr
51
51
  useLiveRows: <TRow extends Record<string, unknown> = Record<string, unknown>>(query: string, options?: {
52
52
  params?: readonly unknown[];
53
53
  ready?: boolean;
54
- /** Explicit PGlite instance — overrides the context client. Useful in tests or multi-db scenarios. */
55
- pglite?: ClientPGlite;
54
+ /** Explicit pgwasm instance — overrides the context client. Useful in tests or multi-db scenarios. */
55
+ pgwasm?: PgwasmClient;
56
56
  }) => {
57
57
  rows: TRow[];
58
58
  loading: boolean;
@@ -61,7 +61,7 @@ export declare function createSyncClientHooks<TRegistry extends SyncTableRegistr
61
61
  useLiveRow: <TRow extends Record<string, unknown> = Record<string, unknown>>(query: string, options?: {
62
62
  params?: readonly unknown[];
63
63
  ready?: boolean;
64
- pglite?: ClientPGlite;
64
+ pgwasm?: PgwasmClient;
65
65
  }) => {
66
66
  row: TRow | null;
67
67
  loading: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"create-hooks.d.ts","sourceRoot":"","sources":["../src/create-hooks.tsx"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,cAAc,EAAE,KAAK,SAAS,EAA4C,MAAM,OAAO,CAAC;AAErH,OAAO,EACL,KAAK,YAAY,EAEjB,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,EAE1B,KAAK,UAAU,EAEhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9F,OAAO,EAAuD,KAAK,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAE5G;;;;;;GAMG;AACH,UAAU,iBAAiB,CAAC,K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AK,iBAAiB,CAAC,KAAK,CAAC,CAAC;QACnE,IAAI,CAAC,EAAE,cAAc,CAAC;QACtB,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,KAAG;QAAE,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;KAAE;mCAiBtD;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,KAAG;QAC1D,OAAO,EAAE,eAAe,CAAC;QACzB,OAAO,EAAE,OAAO,CAAC;QACjB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;KACrB;gCAoEkC,mBAAmB,CAAC,SAAS,CAAC,GAAG;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,KAAG;QACxF,IAAI,EAAE,qBAAqB,EAAE,CAAC;QAC9B,OAAO,EAAE,OAAO,CAAC;QACjB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;KACrB;EA6EF"}
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@ import { Column, SQL, is } from "drizzle-orm";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  //#region src/remap-live-row.ts
6
6
  /**
7
- * Map a raw PGlite live-query row onto a Drizzle select's field keys.
7
+ * Map a raw pgwasm live-query row onto a Drizzle select's field keys.
8
8
  *
9
- * `useLiveDrizzleRows` feeds the builder's `.toSQL()` straight into PGlite's `live.query`, which
9
+ * `useLiveDrizzleRows` feeds the builder's `.toSQL()` straight into pgwasm's `live.query`, which
10
10
  * returns rows keyed by the **underlying column names** (snake_case) — `select({ assigneeId })`
11
11
  * produces SQL `select "assignee_id"`, so the raw row is `{ assignee_id }`, not `{ assigneeId }`.
12
12
  * Drizzle's own execution would remap these by position; the live query bypasses that, so this does it
@@ -78,7 +78,7 @@ function remapAliasedLiveRow(selectedFields, row) {
78
78
  //#endregion
79
79
  //#region src/create-hooks.tsx
80
80
  /**
81
- * The raw direct-PGlite subscription used only by `useLiveRows`'s explicit-`pglite` override (tests/multi-db).
81
+ * The raw direct-pgwasm subscription used only by `useLiveRows`'s explicit-`pglite` override (tests/multi-db).
82
82
  * Wraps `pglite.live.query` into the client's {@link LiveRowsSubscription} shape so the hook body treats the
83
83
  * override and the seam identically. The normal path goes through `client.subscribeLiveRows`.
84
84
  */
@@ -92,6 +92,12 @@ function subscribeRawPglite(pglite, query, params, onRows) {
92
92
  };
93
93
  });
94
94
  }
95
+ /**
96
+ * Shared "no subscription yet" rows. The hooks DERIVE their return value during render, so this has to be
97
+ * one stable identity — a fresh `[]` per render would defeat the memoised return object and re-fire every
98
+ * consumer effect that depends on `rows`.
99
+ */
100
+ var EMPTY_ROWS = [];
95
101
  var EMPTY_MUTATION_SUMMARY = {
96
102
  pendingCount: 0,
97
103
  sendingCount: 0,
@@ -130,62 +136,68 @@ function createSyncClientHooks() {
130
136
  * lazy-relation safety net (ADR-0021) — a raw string is not parameterised/quoted predictably, so a
131
137
  * `lazy` relation referenced here will read empty/stale unless you `client.ensureSynced([...])` first.
132
138
  * Prefer {@link useLiveDrizzleRows} / {@link useLiveQueryRaw} for anything touching lazy relations.
139
+ *
140
+ * `loading` and `rows` are derived from which subscription the latest snapshot belongs to, so a
141
+ * `ready: false → true` interlude with unchanged inputs keeps the previous rows while it re-subscribes
142
+ * (`loading: true`) instead of briefly reporting zero rows — the same "keep the previous rows while
143
+ * loading" policy already applied across a query change.
133
144
  */
134
145
  function useLiveRows(query, options) {
135
146
  const contextClient = useContext(SyncClientContext);
136
- const overridePglite = options?.pglite;
147
+ const overridePglite = options?.pgwasm;
137
148
  const ready = options?.ready ?? true;
138
149
  const paramsKey = JSON.stringify(options?.params ?? []);
139
150
  const stableParams = useMemo(() => JSON.parse(paramsKey), [paramsKey]);
140
- const [state, setState] = useState({
141
- rows: [],
142
- loading: ready,
151
+ const canRun = ready && (overridePglite != null || contextClient != null);
152
+ const run = useMemo(() => canRun ? {
153
+ query,
154
+ params: stableParams,
155
+ client: contextClient,
156
+ pglite: overridePglite
157
+ } : null, [
158
+ canRun,
159
+ query,
160
+ stableParams,
161
+ contextClient,
162
+ overridePglite
163
+ ]);
164
+ const [snapshot, setSnapshot] = useState({
165
+ run: null,
166
+ rows: EMPTY_ROWS,
143
167
  error: null
144
168
  });
145
169
  useEffect(() => {
146
- if (!(ready && (overridePglite != null || contextClient != null))) {
147
- setState({
148
- rows: [],
149
- loading: ready,
150
- error: null
151
- });
152
- return;
153
- }
170
+ if (run == null) return;
154
171
  let active = true;
155
172
  let subscription;
156
- setState((prev) => ({
157
- rows: prev.rows,
158
- loading: true,
159
- error: null
160
- }));
161
173
  const onRows = (rows) => {
162
174
  if (active) {
163
175
  syncDebug("live query updated → re-render", { rows: rows.length });
164
- setState({
176
+ setSnapshot({
177
+ run,
165
178
  rows,
166
- loading: false,
167
179
  error: null
168
180
  });
169
181
  }
170
182
  };
171
- (overridePglite ? subscribeRawPglite(overridePglite, query, stableParams, onRows) : contextClient.subscribeLiveRows({
172
- sql: query,
173
- params: stableParams
183
+ (run.pglite ? subscribeRawPglite(run.pglite, run.query, run.params, onRows) : run.client.subscribeLiveRows({
184
+ sql: run.query,
185
+ params: run.params
174
186
  }, (rows) => onRows(rows))).then((registered) => {
175
187
  if (!active) {
176
188
  registered.unsubscribe();
177
189
  return;
178
190
  }
179
191
  subscription = registered;
180
- setState({
192
+ setSnapshot({
193
+ run,
181
194
  rows: registered.initialRows,
182
- loading: false,
183
195
  error: null
184
196
  });
185
197
  }).catch((error) => {
186
- if (active) setState({
187
- rows: [],
188
- loading: false,
198
+ if (active) setSnapshot({
199
+ run,
200
+ rows: EMPTY_ROWS,
189
201
  error: error instanceof Error ? error : new Error(String(error))
190
202
  });
191
203
  });
@@ -193,14 +205,20 @@ function createSyncClientHooks() {
193
205
  active = false;
194
206
  subscription?.unsubscribe();
195
207
  };
196
- }, [
197
- contextClient,
198
- overridePglite,
199
- query,
200
- ready,
201
- stableParams
208
+ }, [run]);
209
+ const settled = run != null && snapshot.run === run;
210
+ const rows = run == null ? EMPTY_ROWS : snapshot.rows;
211
+ const loading = ready && !settled;
212
+ const error = settled ? snapshot.error : null;
213
+ return useMemo(() => ({
214
+ rows,
215
+ loading,
216
+ error
217
+ }), [
218
+ rows,
219
+ loading,
220
+ error
202
221
  ]);
203
- return state;
204
222
  }
205
223
  function useLiveRow(query, options) {
206
224
  const { rows, loading, error } = useLiveRows(query, options);
@@ -215,6 +233,11 @@ function createSyncClientHooks() {
215
233
  * then `client.prepareQuery` scans the compiled SQL for the lazy relations it reads (∪ the optional
216
234
  * `use`), activates + hydrates them, and only then subscribes the live query — so it is never
217
235
  * registered against an un-hydrated lazy relation (ADR-0021).
236
+ *
237
+ * `loading`/`hydrating`/`rows` are derived from which subscription the latest snapshot belongs to, so a
238
+ * `ready: false → true` interlude with unchanged inputs keeps the previous rows while it re-subscribes
239
+ * (`loading: true`) instead of briefly reporting zero rows — the same "keep the previous rows while
240
+ * loading" policy already applied across a query change.
218
241
  */
219
242
  function useGuardedDrizzleLive(buildQuery, deps, options) {
220
243
  const contextClient = useContext(SyncClientContext);
@@ -231,51 +254,48 @@ function createSyncClientHooks() {
231
254
  };
232
255
  }, [contextClient, ...deps]);
233
256
  const sqlKey = queryInfo != null ? JSON.stringify(queryInfo.sql) : null;
234
- const [state, setState] = useState({
235
- rows: [],
236
- loading: ready,
237
- hydrating: ready,
257
+ const run = useMemo(() => ready && contextClient != null && queryInfo != null ? {
258
+ client: contextClient,
259
+ queryInfo,
260
+ useList,
261
+ keepAliveMs
262
+ } : null, [
263
+ contextClient,
264
+ sqlKey,
265
+ useKey,
266
+ ready
267
+ ]);
268
+ const [snapshot, setSnapshot] = useState({
269
+ run: null,
270
+ rows: EMPTY_ROWS,
271
+ hydrated: false,
238
272
  error: null
239
273
  });
240
274
  useEffect(() => {
241
- if (!ready || contextClient == null || queryInfo == null) {
242
- setState({
243
- rows: [],
244
- loading: ready,
245
- hydrating: false,
246
- error: null
247
- });
248
- return;
249
- }
250
- const { sql, selectedFields } = queryInfo;
275
+ if (run == null) return;
276
+ const { sql, selectedFields } = run.queryInfo;
251
277
  const fields = liveFieldAliases(selectedFields);
252
278
  const mapRows = (rows) => rows.map((row) => fields ? remapAliasedLiveRow(selectedFields, row) : remapLiveRow(selectedFields, row));
253
279
  let active = true;
254
280
  let subscription;
255
- setState((prev) => ({
256
- rows: prev.rows,
257
- loading: true,
258
- hydrating: true,
259
- error: null
260
- }));
261
- contextClient.prepareQuery({
281
+ run.client.prepareQuery({
262
282
  sql: sql.sql,
263
- ...useList ? { use: useList } : {}
283
+ ...run.useList ? { use: run.useList } : {}
264
284
  }).then(() => {
265
285
  if (!active) return void 0;
266
- return contextClient.subscribeLiveRows({
286
+ return run.client.subscribeLiveRows({
267
287
  sql: sql.sql,
268
288
  params: sql.params,
269
289
  ...fields ? { fields } : {},
270
- ...useList ? { use: useList } : {},
271
- ...keepAliveMs != null ? { keepAliveMs } : {}
290
+ ...run.useList ? { use: run.useList } : {},
291
+ ...run.keepAliveMs != null ? { keepAliveMs: run.keepAliveMs } : {}
272
292
  }, (rows) => {
273
293
  if (active) {
274
294
  syncDebug("live query updated → re-render", { rows: rows.length });
275
- setState((prev) => ({
295
+ setSnapshot((prev) => ({
296
+ run,
276
297
  rows: mapRows(rows),
277
- loading: false,
278
- hydrating: prev.hydrating,
298
+ hydrated: prev.run === run ? prev.hydrated : false,
279
299
  error: null
280
300
  }));
281
301
  }
@@ -289,24 +309,24 @@ function createSyncClientHooks() {
289
309
  rows: registered.initialRows.length,
290
310
  hydrating: registered.hydrated != null
291
311
  });
292
- setState((prev) => ({
312
+ setSnapshot({
313
+ run,
293
314
  rows: mapRows(registered.initialRows),
294
- loading: false,
295
- hydrating: registered.hydrated != null ? prev.hydrating : false,
315
+ hydrated: registered.hydrated == null,
296
316
  error: null
297
- }));
317
+ });
298
318
  if (registered.hydrated) registered.hydrated.then(() => {
299
- if (active) setState((prev) => ({
319
+ if (active) setSnapshot((prev) => prev.run === run ? {
300
320
  ...prev,
301
- hydrating: false
302
- }));
321
+ hydrated: true
322
+ } : prev);
303
323
  });
304
324
  });
305
325
  }).catch((error) => {
306
- if (active) setState({
307
- rows: [],
308
- loading: false,
309
- hydrating: false,
326
+ if (active) setSnapshot({
327
+ run,
328
+ rows: EMPTY_ROWS,
329
+ hydrated: true,
310
330
  error: error instanceof Error ? error : new Error(String(error))
311
331
  });
312
332
  });
@@ -314,13 +334,23 @@ function createSyncClientHooks() {
314
334
  active = false;
315
335
  subscription?.unsubscribe();
316
336
  };
317
- }, [
318
- contextClient,
319
- sqlKey,
320
- useKey,
321
- ready
337
+ }, [run]);
338
+ const settled = run != null && snapshot.run === run;
339
+ const rows = run == null ? EMPTY_ROWS : snapshot.rows;
340
+ const loading = ready && !settled;
341
+ const hydrating = run != null && !(settled && snapshot.hydrated);
342
+ const error = settled ? snapshot.error : null;
343
+ return useMemo(() => ({
344
+ rows,
345
+ loading,
346
+ hydrating,
347
+ error
348
+ }), [
349
+ rows,
350
+ loading,
351
+ hydrating,
352
+ error
322
353
  ]);
323
- return state;
324
354
  }
325
355
  /**
326
356
  * Reactive query using a Drizzle select builder. The builder is re-created
@@ -385,35 +415,28 @@ function createSyncClientHooks() {
385
415
  * EVERY writable journal, folded to one {@link MutationSummary}. ONE subscription drives a global sync
386
416
  * indicator — no `hydrating` flag, because journals are local and never network-hydrated. Cheap enough to
387
417
  * mount permanently (ADR-0040 dedup: one registration regardless of subscriber count).
418
+ *
419
+ * `loading` and `summary` are derived from which subscription the latest snapshot belongs to, so a
420
+ * `ready: false → true` interlude keeps the previous summary while it re-subscribes (`loading: true`)
421
+ * instead of briefly reporting the zero summary.
388
422
  */
389
423
  function useMutationSummary(options) {
390
424
  const client = useContext(SyncClientContext);
391
425
  const ready = options?.ready ?? true;
392
- const [state, setState] = useState({
426
+ const run = useMemo(() => ready && client != null ? { client } : null, [ready, client]);
427
+ const [snapshot, setSnapshot] = useState({
428
+ run: null,
393
429
  summary: EMPTY_MUTATION_SUMMARY,
394
- loading: ready,
395
430
  error: null
396
431
  });
397
432
  useEffect(() => {
398
- if (!ready || client == null) {
399
- setState({
400
- summary: EMPTY_MUTATION_SUMMARY,
401
- loading: ready,
402
- error: null
403
- });
404
- return;
405
- }
433
+ if (run == null) return;
406
434
  let active = true;
407
435
  let subscription;
408
- setState((prev) => ({
409
- summary: prev.summary,
410
- loading: true,
411
- error: null
412
- }));
413
- client.mutations.subscribeSummary((summary) => {
414
- if (active) setState({
436
+ run.client.mutations.subscribeSummary((summary) => {
437
+ if (active) setSnapshot({
438
+ run,
415
439
  summary,
416
- loading: false,
417
440
  error: null
418
441
  });
419
442
  }).then((registered) => {
@@ -422,15 +445,15 @@ function createSyncClientHooks() {
422
445
  return;
423
446
  }
424
447
  subscription = registered;
425
- setState({
448
+ setSnapshot({
449
+ run,
426
450
  summary: registered.initial,
427
- loading: false,
428
451
  error: null
429
452
  });
430
453
  }).catch((error) => {
431
- if (active) setState({
454
+ if (active) setSnapshot({
455
+ run,
432
456
  summary: EMPTY_MUTATION_SUMMARY,
433
- loading: false,
434
457
  error: error instanceof Error ? error : new Error(String(error))
435
458
  });
436
459
  });
@@ -438,14 +461,31 @@ function createSyncClientHooks() {
438
461
  active = false;
439
462
  subscription?.unsubscribe();
440
463
  };
441
- }, [client, ready]);
442
- return state;
464
+ }, [run]);
465
+ const settled = run != null && snapshot.run === run;
466
+ const summary = run == null ? EMPTY_MUTATION_SUMMARY : snapshot.summary;
467
+ const loading = ready && !settled;
468
+ const error = settled ? snapshot.error : null;
469
+ return useMemo(() => ({
470
+ summary,
471
+ loading,
472
+ error
473
+ }), [
474
+ summary,
475
+ loading,
476
+ error
477
+ ]);
443
478
  }
444
479
  /**
445
480
  * Reactive filtered mutation detail list (`client.mutations.subscribe`): normalized journal rows across
446
481
  * every writable table, filtered by `table` / `entityKey` / `statuses` / `limit`, ordered newest-first.
447
482
  * Route/feature-scoped — mount it where a diagnostics view is open, not app-wide (prefer
448
483
  * {@link useMutationSummary} for a global indicator). No `hydrating` flag (journals are local).
484
+ *
485
+ * `loading` and `rows` are derived from which subscription the latest snapshot belongs to, so a
486
+ * `ready: false → true` interlude with unchanged filters keeps the previous rows while it re-subscribes
487
+ * (`loading: true`) instead of briefly reporting zero rows — the same "keep the previous rows while
488
+ * loading" policy already applied across a filter change.
449
489
  */
450
490
  function useMutationList(options) {
451
491
  const client = useContext(SyncClientContext);
@@ -453,31 +493,27 @@ function createSyncClientHooks() {
453
493
  const { ready: _ready, ...filters } = options ?? {};
454
494
  const filtersKey = JSON.stringify(filters);
455
495
  const stableFilters = useMemo(() => JSON.parse(filtersKey), [filtersKey]);
456
- const [state, setState] = useState({
457
- rows: [],
458
- loading: ready,
496
+ const run = useMemo(() => ready && client != null ? {
497
+ client,
498
+ filters: stableFilters
499
+ } : null, [
500
+ ready,
501
+ client,
502
+ stableFilters
503
+ ]);
504
+ const [snapshot, setSnapshot] = useState({
505
+ run: null,
506
+ rows: EMPTY_ROWS,
459
507
  error: null
460
508
  });
461
509
  useEffect(() => {
462
- if (!ready || client == null) {
463
- setState({
464
- rows: [],
465
- loading: ready,
466
- error: null
467
- });
468
- return;
469
- }
510
+ if (run == null) return;
470
511
  let active = true;
471
512
  let subscription;
472
- setState((prev) => ({
473
- rows: prev.rows,
474
- loading: true,
475
- error: null
476
- }));
477
- client.mutations.subscribe(stableFilters, (rows) => {
478
- if (active) setState({
513
+ run.client.mutations.subscribe(run.filters, (rows) => {
514
+ if (active) setSnapshot({
515
+ run,
479
516
  rows,
480
- loading: false,
481
517
  error: null
482
518
  });
483
519
  }).then((registered) => {
@@ -486,15 +522,15 @@ function createSyncClientHooks() {
486
522
  return;
487
523
  }
488
524
  subscription = registered;
489
- setState({
525
+ setSnapshot({
526
+ run,
490
527
  rows: registered.initial,
491
- loading: false,
492
528
  error: null
493
529
  });
494
530
  }).catch((error) => {
495
- if (active) setState({
496
- rows: [],
497
- loading: false,
531
+ if (active) setSnapshot({
532
+ run,
533
+ rows: EMPTY_ROWS,
498
534
  error: error instanceof Error ? error : new Error(String(error))
499
535
  });
500
536
  });
@@ -502,12 +538,20 @@ function createSyncClientHooks() {
502
538
  active = false;
503
539
  subscription?.unsubscribe();
504
540
  };
505
- }, [
506
- client,
507
- ready,
508
- filtersKey
541
+ }, [run]);
542
+ const settled = run != null && snapshot.run === run;
543
+ const rows = run == null ? EMPTY_ROWS : snapshot.rows;
544
+ const loading = ready && !settled;
545
+ const error = settled ? snapshot.error : null;
546
+ return useMemo(() => ({
547
+ rows,
548
+ loading,
549
+ error
550
+ }), [
551
+ rows,
552
+ loading,
553
+ error
509
554
  ]);
510
- return state;
511
555
  }
512
556
  return {
513
557
  SyncClientProvider,