@blaze-cache/react-query 0.2.1-beta → 0.2.5

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.
@@ -0,0 +1,66 @@
1
+ import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
2
+ import { readBlazeRecord } from './useBlazeQuery.js';
3
+ /**
4
+ * The options {@link useBlazeInfiniteQuery} hands to `useInfiniteQuery`: the caller's
5
+ * options with the Blaze Cache mirror seeded in, or the caller's options returned
6
+ * untouched.
7
+ *
8
+ * Infinite queries are mirrored as a whole — TanStack stores `InfiniteData` (every page
9
+ * plus its page params) as one `query.state.data`, so the sync layer persists that one
10
+ * object and this function seeds it straight back as `initialData`. The first render
11
+ * after a reload therefore paints **all previously loaded pages at once**, while
12
+ * `dataUpdatedAt` carries the original timestamp so TanStack still refetches in the
13
+ * background.
14
+ *
15
+ * The rules are exactly {@link seedBlazeQueryOptions}'s, deliberately:
16
+ *
17
+ * * A caller-supplied `initialData` or `initialDataUpdatedAt` is authoritative — the
18
+ * mirror stays out of it entirely.
19
+ * * With neither supplied, the mirror seeds both fields together, so the data paints
20
+ * instantly *and* is treated as stale.
21
+ */
22
+ export function seedBlazeInfiniteQueryOptions(cache, client, options) {
23
+ // Same authority rule as the non-infinite seed: an explicit `initialData` (function
24
+ // form included) or its timestamp short-circuits the mirror entirely.
25
+ if (options.initialData !== undefined || options.initialDataUpdatedAt !== undefined) {
26
+ return options;
27
+ }
28
+ const record = readBlazeRecord(cache, client, options);
29
+ if (record === undefined)
30
+ return options;
31
+ // The mirror's payload *is* an `InfiniteData` object — that is what TanStack put in
32
+ // the cache — so it slots into `initialData` without transformation. The cast exists
33
+ // because the record type is deliberately untyped (`unknown`).
34
+ return {
35
+ ...options,
36
+ initialData: record.data,
37
+ initialDataUpdatedAt: record.dataUpdatedAt,
38
+ };
39
+ }
40
+ /**
41
+ * `useInfiniteQuery` that starts from the Blaze Cache.
42
+ *
43
+ * A previously mirrored `InfiniteData` payload — every page and page param fetched in an
44
+ * earlier session — is injected as `initialData` **with its original `dataUpdatedAt`**,
45
+ * so the list paints fully populated on the first render after a reload while TanStack
46
+ * still considers it stale and refetches in the background. Caller-supplied
47
+ * `initialData`/`initialDataUpdatedAt` short-circuit the seeding entirely; see
48
+ * {@link seedBlazeInfiniteQueryOptions}.
49
+ *
50
+ * ```tsx
51
+ * const feed = useBlazeInfiniteQuery(cache, {
52
+ * queryKey: ['feed', userId],
53
+ * queryFn: ({ pageParam }) => fetchFeed(userId, pageParam),
54
+ * initialPageParam: 0,
55
+ * getNextPageParam: (lastPage) => lastPage.nextCursor,
56
+ * });
57
+ * // feed.data?.pages — the pages cached before the reload, immediately
58
+ * ```
59
+ *
60
+ * Requires a `QueryClientProvider` above it, exactly like `useInfiniteQuery`.
61
+ */
62
+ export function useBlazeInfiniteQuery(cache, options) {
63
+ const client = useQueryClient();
64
+ return useInfiniteQuery(seedBlazeInfiniteQueryOptions(cache, client, options));
65
+ }
66
+ //# sourceMappingURL=useBlazeInfiniteQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlazeInfiniteQuery.js","sourceRoot":"","sources":["../src/useBlazeInfiniteQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAUzE,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,6BAA6B,CAO3C,KAA0B,EAC1B,MAAmB,EACnB,OAAoF;IAEpF,oFAAoF;IACpF,sEAAsE;IACtE,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,OAAO,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;QACpF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,MAAM,GAAG,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IAEzC,oFAAoF;IACpF,qFAAqF;IACrF,+DAA+D;IAC/D,OAAO;QACL,GAAG,OAAO;QACV,WAAW,EAAE,MAAM,CAAC,IAA8C;QAClE,oBAAoB,EAAE,MAAM,CAAC,aAAa;KACoC,CAAC;AACnF,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,qBAAqB,CAOnC,KAA0B,EAC1B,OAAoF;IAEpF,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,gBAAgB,CACrB,6BAA6B,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CACtD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,54 @@
1
+ import type { DefaultError, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
2
+ import type { BlazeQueryCacheLike, BlazeQueryFilters } from './types.js';
3
+ /**
4
+ * The options {@link useBlazeMutation} accepts: TanStack's own `UseMutationOptions` —
5
+ * everything you can pass to `useMutation` — plus declarative, **two-sided** invalidation.
6
+ */
7
+ export interface BlazeMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> extends UseMutationOptions<TData, TError, TVariables, TContext> {
8
+ /**
9
+ * Queries invalidated after a successful mutation, one filter or an array of them —
10
+ * each applied through {@link invalidateBlazeQueries}, so TanStack marks them stale
11
+ * (active ones refetch) **and** the matching persisted records are purged.
12
+ *
13
+ * Runs after your own `onSuccess`, awaited in order, so callback side effects (writes,
14
+ * navigation) land before anything is invalidated — and a throwing `onSuccess` skips
15
+ * the invalidation exactly like it skips TanStack's own refetch chain.
16
+ *
17
+ * ```ts
18
+ * const addTodo = useBlazeMutation(cache, {
19
+ * mutationFn: (text: string) => api.addTodo(text),
20
+ * invalidate: { queryKey: ['todos'] },
21
+ * });
22
+ * ```
23
+ *
24
+ * Omit it to keep invalidation entirely in your own `onSuccess`/`onSettled` — those are
25
+ * untouched when no `invalidate` is given.
26
+ */
27
+ invalidate?: BlazeQueryFilters | BlazeQueryFilters[];
28
+ }
29
+ /**
30
+ * `useMutation` whose success path keeps the Blaze Cache mirror in step.
31
+ *
32
+ * Apart from the optional {@link BlazeMutationOptions.invalidate} — TanStack-side
33
+ * invalidation plus mirror purge through {@link invalidateBlazeQueries} — this is
34
+ * `useMutation` verbatim: same generics, same result type, same per-call callbacks, and
35
+ * your hook-level callbacks run first when both are present.
36
+ *
37
+ * ```tsx
38
+ * function AddTodo({ userId }: { userId: string }) {
39
+ * const addTodo = useBlazeMutation(cache, {
40
+ * mutationFn: (text: string) => api.addTodo(userId, text),
41
+ * invalidate: [
42
+ * { queryKey: ['todos', userId] },
43
+ * { tags: ['todos', `user:${userId}`] },
44
+ * ],
45
+ * onSuccess: () => toast('Added'),
46
+ * });
47
+ * // addTodo.mutate('Buy milk') — 'Added' toasts, then both filters invalidate
48
+ * }
49
+ * ```
50
+ *
51
+ * Requires a `QueryClientProvider` above it, exactly like `useMutation`.
52
+ */
53
+ export declare function useBlazeMutation<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown>(cache: BlazeQueryCacheLike, options: BlazeMutationOptions<TData, TError, TVariables, TContext>): UseMutationResult<TData, TError, TVariables, TContext>;
54
+ //# sourceMappingURL=useBlazeMutation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlazeMutation.d.ts","sourceRoot":"","sources":["../src/useBlazeMutation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAGjG,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEzE;;;GAGG;AACH,MAAM,WAAW,oBAAoB,CACnC,KAAK,GAAG,OAAO,EACf,MAAM,GAAG,YAAY,EACrB,UAAU,GAAG,IAAI,EACjB,QAAQ,GAAG,OAAO,CAClB,SAAQ,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC;IAC/D;;;;;;;;;;;;;;;;;;OAkBG;IACH,UAAU,CAAC,EAAE,iBAAiB,GAAG,iBAAiB,EAAE,CAAC;CACtD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,GAAG,OAAO,EACf,MAAM,GAAG,YAAY,EACrB,UAAU,GAAG,IAAI,EACjB,QAAQ,GAAG,OAAO,EAElB,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,GACjE,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CA2BxD"}
@@ -0,0 +1,50 @@
1
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
2
+ import { invalidateBlazeQueries } from './imperative.js';
3
+ /**
4
+ * `useMutation` whose success path keeps the Blaze Cache mirror in step.
5
+ *
6
+ * Apart from the optional {@link BlazeMutationOptions.invalidate} — TanStack-side
7
+ * invalidation plus mirror purge through {@link invalidateBlazeQueries} — this is
8
+ * `useMutation` verbatim: same generics, same result type, same per-call callbacks, and
9
+ * your hook-level callbacks run first when both are present.
10
+ *
11
+ * ```tsx
12
+ * function AddTodo({ userId }: { userId: string }) {
13
+ * const addTodo = useBlazeMutation(cache, {
14
+ * mutationFn: (text: string) => api.addTodo(userId, text),
15
+ * invalidate: [
16
+ * { queryKey: ['todos', userId] },
17
+ * { tags: ['todos', `user:${userId}`] },
18
+ * ],
19
+ * onSuccess: () => toast('Added'),
20
+ * });
21
+ * // addTodo.mutate('Buy milk') — 'Added' toasts, then both filters invalidate
22
+ * }
23
+ * ```
24
+ *
25
+ * Requires a `QueryClientProvider` above it, exactly like `useMutation`.
26
+ */
27
+ export function useBlazeMutation(cache, options) {
28
+ const client = useQueryClient();
29
+ // Without `invalidate` there is nothing to compose — pass the caller's options through
30
+ // untouched, so `onSuccess` semantics are byte-for-byte TanStack's.
31
+ if (options.invalidate === undefined) {
32
+ return useMutation(options, client);
33
+ }
34
+ const { invalidate, onSuccess, ...mutationOptions } = options;
35
+ return useMutation({
36
+ ...mutationOptions,
37
+ onSuccess: async (data, variables, onMutateResult, context) => {
38
+ // Caller first: their `onSuccess` writes/navigation happen before invalidation,
39
+ // and if it throws, no invalidation runs — mirroring how TanStack treats a
40
+ // throwing `onSuccess` by abandoning the rest of its success chain.
41
+ if (onSuccess)
42
+ await onSuccess(data, variables, onMutateResult, context);
43
+ const filters = Array.isArray(invalidate) ? invalidate : [invalidate];
44
+ for (const filter of filters) {
45
+ await invalidateBlazeQueries(cache, client, filter);
46
+ }
47
+ },
48
+ }, client);
49
+ }
50
+ //# sourceMappingURL=useBlazeMutation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlazeMutation.js","sourceRoot":"","sources":["../src/useBlazeMutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAGpE,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAmCzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAM9B,KAA0B,EAC1B,OAAkE;IAElE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,uFAAuF;IACvF,oEAAoE;IACpE,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QACrC,OAAO,WAAW,CAAsC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC3E,CAAC;IAED,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,CAAC;IAE9D,OAAO,WAAW,CAChB;QACE,GAAG,eAAe;QAClB,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,EAAE,EAAE;YAC5D,gFAAgF;YAChF,2EAA2E;YAC3E,oEAAoE;YACpE,IAAI,SAAS;gBAAE,MAAM,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;YACzE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;YACtE,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;gBAC7B,MAAM,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;KACF,EACD,MAAM,CACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { QueriesOptions, QueriesResults, QueryClient } from '@tanstack/react-query';
2
+ import type { BlazeQueryCacheLike } from './types.js';
3
+ /**
4
+ * The options {@link useBlazeQueries} accepts: TanStack's own `useQueries` shape — a
5
+ * `queries` array plus the optional `combine` — with each query seeded from the Blaze
6
+ * Cache first.
7
+ *
8
+ * The generics mirror `useQueries`' so per-query inference (each `queryFn`'s result,
9
+ * each `select`, and `combine`'s output) survives the round trip: a tuple of queries
10
+ * destructure into individually typed results, exactly as with `useQueries`.
11
+ */
12
+ export interface UseBlazeQueriesOptions<T extends Array<any>, TCombinedResult = QueriesResults<T>> {
13
+ /** Query option objects — the same shapes `useQueries` takes, seeded from the mirror. */
14
+ queries: readonly [...QueriesOptions<T>];
15
+ /** Combine the results into a single value, as `useQueries` does. */
16
+ combine?: (result: QueriesResults<T>) => TCombinedResult;
17
+ }
18
+ /**
19
+ * The render-time data path for {@link useBlazeQueries}: seed every query's options
20
+ * from the Blaze Cache, then hand the array to TanStack.
21
+ *
22
+ * Exposed as a plain function (like {@link seedBlazeQueryOptions}) so the per-query
23
+ * seeding can be exercised without a DOM renderer. Each entry goes through
24
+ * {@link seedBlazeQueryOptions} unchanged — the same authority rules, the same
25
+ * `peek`-first lookup, the same custom-hasher handling — so one cold entry in the
26
+ * mirror never blocks the others from seeding.
27
+ */
28
+ export declare function seedBlazeQueriesOptions<T extends Array<any>, TCombinedResult = QueriesResults<T>>(cache: BlazeQueryCacheLike, client: QueryClient, options: UseBlazeQueriesOptions<T, TCombinedResult>): UseBlazeQueriesOptions<T, TCombinedResult>;
29
+ /**
30
+ * `useQueries` where every query is seeded from the Blaze Cache.
31
+ *
32
+ * The parallel equivalent of {@link useBlazeQuery}: each query in the array gets the
33
+ * instant-first-paint treatment independently — a cached entry becomes `initialData`
34
+ * with its original `dataUpdatedAt`, an uncached one fetches cold. Results and typing
35
+ * match `useQueries` exactly, including `combine`.
36
+ *
37
+ * ```tsx
38
+ * const [profile, settings, notifications] = useBlazeQueries(cache, {
39
+ * queries: [
40
+ * { queryKey: ['profile', userId], queryFn: () => api.profile(userId) },
41
+ * { queryKey: ['settings'], queryFn: api.settings },
42
+ * { queryKey: ['notifications', userId], queryFn: () => api.notifications(userId) },
43
+ * ],
44
+ * });
45
+ * ```
46
+ *
47
+ * Requires a `QueryClientProvider` above it, exactly like `useQueries`.
48
+ */
49
+ export declare function useBlazeQueries<T extends Array<any>, TCombinedResult = QueriesResults<T>>(cache: BlazeQueryCacheLike, options: UseBlazeQueriesOptions<T, TCombinedResult>): TCombinedResult;
50
+ //# sourceMappingURL=useBlazeQueries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlazeQueries.d.ts","sourceRoot":"","sources":["../src/useBlazeQueries.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtD;;;;;;;;GAQG;AACH,MAAM,WAAW,sBAAsB,CACrC,CAAC,SAAS,KAAK,CAAC,GAAG,CAAC,EACpB,eAAe,GAAG,cAAc,CAAC,CAAC,CAAC;IAEnC,yFAAyF;IACzF,OAAO,EAAE,SAAS,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IACzC,qEAAqE;IACrE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,eAAe,CAAC;CAC1D;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CACrC,CAAC,SAAS,KAAK,CAAC,GAAG,CAAC,EACpB,eAAe,GAAG,cAAc,CAAC,CAAC,CAAC,EAEnC,KAAK,EAAE,mBAAmB,EAC1B,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,sBAAsB,CAAC,CAAC,EAAE,eAAe,CAAC,GAClD,sBAAsB,CAAC,CAAC,EAAE,eAAe,CAAC,CAU5C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,KAAK,CAAC,GAAG,CAAC,EAAE,eAAe,GAAG,cAAc,CAAC,CAAC,CAAC,EACvF,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,sBAAsB,CAAC,CAAC,EAAE,eAAe,CAAC,GAClD,eAAe,CAIjB"}
@@ -0,0 +1,46 @@
1
+ import { useQueries, useQueryClient } from '@tanstack/react-query';
2
+ import { seedBlazeQueryOptions } from './useBlazeQuery.js';
3
+ /**
4
+ * The render-time data path for {@link useBlazeQueries}: seed every query's options
5
+ * from the Blaze Cache, then hand the array to TanStack.
6
+ *
7
+ * Exposed as a plain function (like {@link seedBlazeQueryOptions}) so the per-query
8
+ * seeding can be exercised without a DOM renderer. Each entry goes through
9
+ * {@link seedBlazeQueryOptions} unchanged — the same authority rules, the same
10
+ * `peek`-first lookup, the same custom-hasher handling — so one cold entry in the
11
+ * mirror never blocks the others from seeding.
12
+ */
13
+ export function seedBlazeQueriesOptions(cache, client, options) {
14
+ return {
15
+ ...options,
16
+ // Per-entry seeding is type-preserving, but `T` is generic here so TS only sees the
17
+ // elements' constraint and refuses the direct cast to the conditional
18
+ // `QueriesOptions<T>` shape — the `unknown` bridge is the honest description.
19
+ queries: options.queries.map((query) => seedBlazeQueryOptions(cache, client, query)),
20
+ };
21
+ }
22
+ /**
23
+ * `useQueries` where every query is seeded from the Blaze Cache.
24
+ *
25
+ * The parallel equivalent of {@link useBlazeQuery}: each query in the array gets the
26
+ * instant-first-paint treatment independently — a cached entry becomes `initialData`
27
+ * with its original `dataUpdatedAt`, an uncached one fetches cold. Results and typing
28
+ * match `useQueries` exactly, including `combine`.
29
+ *
30
+ * ```tsx
31
+ * const [profile, settings, notifications] = useBlazeQueries(cache, {
32
+ * queries: [
33
+ * { queryKey: ['profile', userId], queryFn: () => api.profile(userId) },
34
+ * { queryKey: ['settings'], queryFn: api.settings },
35
+ * { queryKey: ['notifications', userId], queryFn: () => api.notifications(userId) },
36
+ * ],
37
+ * });
38
+ * ```
39
+ *
40
+ * Requires a `QueryClientProvider` above it, exactly like `useQueries`.
41
+ */
42
+ export function useBlazeQueries(cache, options) {
43
+ const client = useQueryClient();
44
+ return useQueries(seedBlazeQueriesOptions(cache, client, options), client);
45
+ }
46
+ //# sourceMappingURL=useBlazeQueries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlazeQueries.js","sourceRoot":"","sources":["../src/useBlazeQueries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAOnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAsB3D;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CAIrC,KAA0B,EAC1B,MAAmB,EACnB,OAAmD;IAEnD,OAAO;QACL,GAAG,OAAO;QACV,oFAAoF;QACpF,sEAAsE;QACtE,8EAA8E;QAC9E,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACrC,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CACwB;KACtE,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,eAAe,CAC7B,KAA0B,EAC1B,OAAmD;IAEnD,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,UAAU,CAAqB,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;AACjG,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import type { DefaultError, QueryClient, QueryKey, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
2
- import type { BlazeQueryCacheLike } from './types.js';
2
+ import type { BlazeQueryCacheLike, BlazeQueryRecord } from './types.js';
3
3
  /**
4
4
  * The options {@link useBlazeQuery} hands to `useQuery`: the caller's options with the Blaze
5
5
  * Cache mirror seeded in, or the caller's options returned untouched.
@@ -18,6 +18,16 @@ import type { BlazeQueryCacheLike } from './types.js';
18
18
  * stale, so the background refetch happens exactly as it would without the cache.
19
19
  */
20
20
  export declare function seedBlazeQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(cache: BlazeQueryCacheLike, client: QueryClient, options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>): UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>;
21
+ /**
22
+ * The mirror lookup every seed function shares: resolve the query's hash the way TanStack
23
+ * will file it, then read the record back **without** touching cache statistics.
24
+ *
25
+ * Kept separate from the seeding rules so {@link seedBlazeQueryOptions},
26
+ * {@link seedBlazeInfiniteQueryOptions} and {@link seedBlazeQueriesOptions} cannot drift
27
+ * from each other — a custom `queryKeyHashFn` or a `peek`-less cache behaves identically
28
+ * in all three.
29
+ */
30
+ export declare function readBlazeRecord(cache: BlazeQueryCacheLike, client: QueryClient, options: Pick<UseQueryOptions<any, any, any, any>, 'queryKey' | 'queryHash' | 'queryKeyHashFn'>): BlazeQueryRecord | undefined;
21
31
  /**
22
32
  * `useQuery` that starts from the Blaze Cache.
23
33
  *
@@ -1 +1 @@
1
- {"version":3,"file":"useBlazeQuery.d.ts","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,eAAe,EACf,cAAc,EACf,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,KAAK,EAAE,mBAAmB,EAC1B,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC/D,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CA0BzD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAC3B,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC/D,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAM/B"}
1
+ {"version":3,"file":"useBlazeQuery.d.ts","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,eAAe,EACf,cAAc,EACf,MAAM,uBAAuB,CAAC;AAI/B,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAExE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,KAAK,EAAE,mBAAmB,EAC1B,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC/D,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAiBzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,mBAAmB,EAC1B,MAAM,EAAE,WAAW,EAInB,OAAO,EAAE,IAAI,CACX,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EACnC,UAAU,GAAG,WAAW,GAAG,gBAAgB,CAC5C,GACA,gBAAgB,GAAG,SAAS,CAmB9B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAC3B,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC/D,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAM/B"}
@@ -1,5 +1,6 @@
1
1
  import { useQuery, useQueryClient } from '@tanstack/react-query';
2
2
  import { resolveQueryHash } from './hash.js';
3
+ import { isMirrorRecord } from './record.js';
3
4
  /**
4
5
  * The options {@link useBlazeQuery} hands to `useQuery`: the caller's options with the Blaze
5
6
  * Cache mirror seeded in, or the caller's options returned untouched.
@@ -23,15 +24,7 @@ export function seedBlazeQueryOptions(cache, client, options) {
23
24
  if (options.initialData !== undefined || options.initialDataUpdatedAt !== undefined) {
24
25
  return options;
25
26
  }
26
- // Sync writes under `query.queryHash`, which honors a configured `queryKeyHashFn`; resolve
27
- // the hash the same way TanStack does, or a custom hasher points the lookup at a key the
28
- // mirror never used.
29
- const hash = resolveQueryHash(client, options.queryKey, options);
30
- // `peek` first: `get` marks the entry used for SIEVE and counts a hit/miss, so a render
31
- // would mutate global cache state — StrictMode double-renders and discarded concurrent
32
- // renders included — and skew the stats panel. `get` stays the fallback for caches that
33
- // predate `peek` (it is optional on `BlazeQueryCacheLike` for their sake).
34
- const record = cache.peek ? cache.peek(hash) : cache.get(hash);
27
+ const record = readBlazeRecord(cache, client, options);
35
28
  if (record === undefined)
36
29
  return options;
37
30
  // `initialData` is declared against `TData` in one overload and `TQueryFnData` in another;
@@ -42,6 +35,37 @@ export function seedBlazeQueryOptions(cache, client, options) {
42
35
  initialDataUpdatedAt: record.dataUpdatedAt,
43
36
  };
44
37
  }
38
+ /**
39
+ * The mirror lookup every seed function shares: resolve the query's hash the way TanStack
40
+ * will file it, then read the record back **without** touching cache statistics.
41
+ *
42
+ * Kept separate from the seeding rules so {@link seedBlazeQueryOptions},
43
+ * {@link seedBlazeInfiniteQueryOptions} and {@link seedBlazeQueriesOptions} cannot drift
44
+ * from each other — a custom `queryKeyHashFn` or a `peek`-less cache behaves identically
45
+ * in all three.
46
+ */
47
+ export function readBlazeRecord(cache, client,
48
+ // Deliberately `any`-typed generics: callers pass options generic in `TQueryKey`, and a
49
+ // `readonly unknown[]`-constrained pick rejects `QueryKeyHashFunction<TQueryKey>` under
50
+ // contravariance even though every concrete instantiation fits.
51
+ options) {
52
+ // Sync writes under `query.queryHash`, which honors a configured `queryKeyHashFn`; resolve
53
+ // the hash the same way TanStack does, or a custom hasher points the lookup at a key the
54
+ // mirror never used.
55
+ const hash = resolveQueryHash(client, options.queryKey, options);
56
+ // `peek` first: `get` marks the entry used for SIEVE and counts a hit/miss, so a render
57
+ // would mutate global cache state — StrictMode double-renders and discarded concurrent
58
+ // renders included — and skew the stats panel. `get` stays the fallback for caches that
59
+ // predate `peek` (it is optional on `BlazeQueryCacheLike` for their sake).
60
+ const record = cache.peek ? cache.peek(hash) : cache.get(hash);
61
+ // Shape gate: on a cache shared with `@blaze-cache/axios`, `@blaze-cache/fetch` or
62
+ // `@blaze-cache/swr`, a forced key collision can park a foreign value under this query's
63
+ // hash. Seeding it would hand an HTTP body (or a raw fetcher result) to TanStack as
64
+ // `initialData` — with no `dataUpdatedAt` to date it, so a configured `staleTime` would
65
+ // be ignored too. Only a record this package could have written is mirror data; anything
66
+ // else reads as the miss it is. See {@link isMirrorRecord}.
67
+ return isMirrorRecord(record) ? record : undefined;
68
+ }
45
69
  /**
46
70
  * `useQuery` that starts from the Blaze Cache.
47
71
  *
@@ -1 +1 @@
1
- {"version":3,"file":"useBlazeQuery.js","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AASjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAG7C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,qBAAqB,CAMnC,KAA0B,EAC1B,MAAmB,EACnB,OAAgE;IAEhE,0FAA0F;IAC1F,gFAAgF;IAChF,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,OAAO,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;QACpF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,2FAA2F;IAC3F,yFAAyF;IACzF,qBAAqB;IACrB,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAEjE,wFAAwF;IACxF,uFAAuF;IACvF,wFAAwF;IACxF,2EAA2E;IAC3E,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IAEzC,2FAA2F;IAC3F,uFAAuF;IACvF,OAAO;QACL,GAAG,OAAO;QACV,WAAW,EAAE,MAAM,CAAC,IAAoB;QACxC,oBAAoB,EAAE,MAAM,CAAC,aAAa;KACgB,CAAC;AAC/D,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,aAAa,CAM3B,KAA0B,EAC1B,OAAgE;IAEhE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,QAAQ,CACb,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAC9C,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"useBlazeQuery.js","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AASjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,qBAAqB,CAMnC,KAA0B,EAC1B,MAAmB,EACnB,OAAgE;IAEhE,0FAA0F;IAC1F,gFAAgF;IAChF,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,OAAO,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;QACpF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,MAAM,GAAG,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IAEzC,2FAA2F;IAC3F,uFAAuF;IACvF,OAAO;QACL,GAAG,OAAO;QACV,WAAW,EAAE,MAAM,CAAC,IAAoB;QACxC,oBAAoB,EAAE,MAAM,CAAC,aAAa;KACgB,CAAC;AAC/D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAC7B,KAA0B,EAC1B,MAAmB;AACnB,wFAAwF;AACxF,wFAAwF;AACxF,gEAAgE;AAChE,OAGC;IAED,2FAA2F;IAC3F,yFAAyF;IACzF,qBAAqB;IACrB,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAEjE,wFAAwF;IACxF,uFAAuF;IACvF,wFAAwF;IACxF,2EAA2E;IAC3E,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAE/D,mFAAmF;IACnF,yFAAyF;IACzF,oFAAoF;IACpF,wFAAwF;IACxF,yFAAyF;IACzF,4DAA4D;IAC5D,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACrD,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,aAAa,CAM3B,KAA0B,EAC1B,OAAgE;IAEhE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,QAAQ,CACb,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAC9C,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaze-cache/react-query",
3
- "version": "0.2.1-beta",
3
+ "version": "0.2.5",
4
4
  "description": "Persist TanStack Query v5 in WebAssembly — instant first paint after reload while stale-while-revalidate still works",
5
5
  "keywords": [
6
6
  "react-query",
@@ -38,7 +38,7 @@
38
38
  "dist"
39
39
  ],
40
40
  "dependencies": {
41
- "@blaze-cache/core": "0.2.1-beta"
41
+ "@blaze-cache/core": "0.2.5"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@tanstack/react-query": "^5.0.0"