Skip to content

Commit f6bf406

Browse files
committed
feat: make query ID accessible via context
1 parent 3a8b6b2 commit f6bf406

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

packages/react-async-proxy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
},
4646
"dependencies": {
4747
"@sindresorhus/is": "^7.1.0",
48+
"context": "^3.0.33",
4849
"nanoid": "^5.1.6",
4950
"object-code": "^1.3.4",
5051
"type-fest": "^5.1.0"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createCascade } from "context";
2+
import type { QueryFnContext } from "./types";
3+
4+
export const queryFnContext = createCascade<QueryFnContext>();
5+
6+
export const useQueryFnContext = () =>
7+
queryFnContext.use() as QueryFnContext | undefined;
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export { reactAsyncProxy } from "./reactAsyncProxy";
2-
export { type ReactAsyncProxy } from "./types";
2+
export {
3+
type ReactAsyncProxy,
4+
QueryFnContext,
5+
UseAsyncProxyReturn,
6+
} from "./types";
37
export * from "./maybeProxy";
8+
export { useQueryFnContext } from "./context";
9+
export { invalidateQueriesById } from "./invalidate";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { QueryClient } from "@tanstack/react-query";
2+
3+
export const invalidateQueriesById = (
4+
queryClient: QueryClient,
5+
queryId: string,
6+
) => {
7+
queryClient.invalidateQueries({
8+
predicate: (query) => query.meta?.id === queryId,
9+
});
10+
};

packages/react-async-proxy/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ export function isReactAsyncProxy<T>(
7373
something[useProxyMarker as keyof typeof something] === true
7474
);
7575
}
76+
77+
export interface QueryFnContext {
78+
id?: string;
79+
[key: string]: unknown;
80+
}

packages/react-async-proxy/src/useCallStack.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
useSuspenseQuery,
1818
} from "@tanstack/react-query";
1919
import { getModelQueryKey } from "./getModelQueryKey";
20+
import { queryFnContext } from "./context";
21+
import { invalidateQueriesById } from "./invalidate";
2022

2123
const useVoidQuery = () =>
2224
useQuery({
@@ -62,12 +64,16 @@ const useCallStackItem = <T>(
6264

6365
assert.function(modelProperty);
6466

65-
const modelFunction = modelProperty.bind(model);
6667
const queryId = nanoid();
68+
const modelFunction = queryFnContext.bind(
69+
{
70+
id: queryId,
71+
},
72+
modelProperty.bind(model),
73+
);
6774
const modelQueryKey = getModelQueryKey(model, propName, ...args);
6875

6976
const query = useSuspenseQuery<UseQueryReturnType<T>>({
70-
staleTime: Infinity,
7177
...options,
7278
queryKey: modelQueryKey,
7379
queryFn: async () => {
@@ -79,15 +85,11 @@ const useCallStackItem = <T>(
7985
},
8086
meta: {
8187
...options?.meta,
82-
queryId,
88+
id: queryId,
8389
},
8490
});
8591

86-
const refresh = () => {
87-
queryClient.invalidateQueries({
88-
predicate: (query) => query.meta?.queryId === queryId,
89-
});
90-
};
92+
const refresh = () => invalidateQueriesById(queryClient, queryId);
9193

9294
if (query.error) {
9395
throw query.error;

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)