Skip to content

Commit 89e7526

Browse files
committed
separate tag operations to another class
1 parent bb8e7c6 commit 89e7526

File tree

5 files changed

+61
-43
lines changed

5 files changed

+61
-43
lines changed

examples/vite/src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ function App() {
9090

9191
const deleteErrors = deletePostMutation.useMutationState(undefined, { status: 'error' }, (x) => x.state.variables?.params.id);
9292

93-
const refresh = () => builder.client.operateTags({ tags: 'refreshable', operation: 'reset' });
93+
const reload = () => builder.tags.reset({ tags: 'refreshable' });
9494

9595
if (postId != null) return <PostPage postId={postId} onBack={() => setPostId(null)} />;
9696

9797
return (
9898
<>
99-
<button onClick={refresh} disabled={posts.isFetching}>
100-
Refresh
99+
<button onClick={reload} disabled={posts.isFetching}>
100+
Reload
101101
</button>
102102

103103
<button onClick={() => reset.mutateAsync({})} disabled={reset.isPending}>

src/builder/QueryBuilderClient.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import type {
99
SetDataOptions,
1010
} from '@tanstack/react-query';
1111
import { MutationObserver } from '@tanstack/react-query';
12-
import { operateOnTags } from '../tags/operateOnTags';
13-
import { QueryTagContext, QueryUpdateTag, TagOperationOptions } from '../tags/types';
14-
import { updateTags } from '../tags/updateTags';
15-
import { WithOptional } from '../type-utils';
1612
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
1713
import { BuilderConfig } from './types';
1814

@@ -86,22 +82,6 @@ export class QueryBuilderClient<
8682
const observer = new MutationObserver<TData, TError, TVars>(client, options);
8783
return observer.mutate(vars, options).finally(() => observer.reset());
8884
};
89-
90-
readonly operateTags = ({ tags = [], operation = 'invalidate', filters, options }: TagOperationOptions<TTags>) =>
91-
operateOnTags({ queryClient: this.builder.config.queryClient!, tags, operation }, filters, options);
92-
93-
/**
94-
* This function can be used to operate on queries based on tags.
95-
*/
96-
readonly updateTags = ({
97-
tags = [],
98-
ctx: { client = this.builder.config.queryClient, ...ctx },
99-
optimistic = false,
100-
}: {
101-
tags: readonly QueryUpdateTag<TVars, TData, TError, TTags>[];
102-
ctx: WithOptional<QueryTagContext<TVars, TData, TError>, 'client'>;
103-
optimistic?: boolean;
104-
}) => updateTags({ tags, queryClient: client!, ctx: { client: client!, ...ctx }, optimistic });
10585
}
10686

10787
type SetDataUpdater<T> = T | undefined | ((oldData: T | undefined) => T | undefined);

src/builder/QueryBuilderFrozen.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ import {
2828
useSuspenseQueries,
2929
useSuspenseQuery,
3030
} from '@tanstack/react-query';
31-
import { operateOnTags } from '../tags/operateOnTags';
32-
import { TagOperationOptions } from '../tags/types';
3331
import { FunctionType, TODO, WithRequired } from '../type-utils';
3432
import { QueryBuilderClient } from './QueryBuilderClient';
33+
import { QueryBuilderTagsManager } from './QueryBuilderTagsManager';
3534
import { BuilderOptions, mergeBuilderOptions } from './options';
3635
import { BuilderConfig, BuilderQueriesResult } from './types';
3736
import { areKeysEqual, getRandomKey, mergeMutationOptions, mergeVars } from './utils';
@@ -301,25 +300,13 @@ export class QueryBuilderFrozen<
301300

302301
//#endregion
303302

304-
/**
305-
* This hook returns a function that can be used to operate on queries based on tags.
306-
* It also returns the mutation object that can be used to track the state of the operation.
307-
* See `operateOnTags` for more information.
308-
*/
309-
useTagOperation(opts?: TagOperationOptions<TTags> | void) {
310-
const queryClient = useQueryClient();
311-
const mutationFn: MutationFunction<unknown, TagOperationOptions<TTags> | void> = (
312-
{ tags = [], operation = 'invalidate', filters, options } = opts || {},
313-
) => operateOnTags({ queryClient, tags, operation }, filters, options);
314-
315-
const mutation = useMutation({ mutationFn });
316-
const operate = mutation.mutateAsync;
317-
318-
return [operate, mutation] as const;
319-
}
320-
321303
private _client?: QueryBuilderClient<TVars, TData, TError, TKey, TTags>;
322304
get client() {
323305
return (this._client ??= new QueryBuilderClient(this));
324306
}
307+
308+
private _tags?: QueryBuilderTagsManager<TVars, TData, TError, TKey, TTags>;
309+
get tags() {
310+
return (this._tags ??= new QueryBuilderTagsManager(this));
311+
}
325312
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { MutationFunction, useMutation, useQueryClient } from '@tanstack/react-query';
2+
import { operateOnTags } from '../tags/operateOnTags';
3+
import { QueryTagContext, QueryUpdateTag, TagOperationOptions } from '../tags/types';
4+
import { updateTags } from '../tags/updateTags';
5+
import { WithOptional } from '../type-utils';
6+
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
7+
import { BuilderConfig } from './types';
8+
9+
export class QueryBuilderTagsManager<TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>> {
10+
private declare _options: BuilderConfig<TVars, TData, TError, TKey>['options'];
11+
constructor(private builder: QueryBuilderFrozen<TVars, TData, TError, TKey, TTags>) {}
12+
13+
/**
14+
* This hook returns a function that can be used to operate on queries based on tags.
15+
* It also returns the mutation object that can be used to track the state of the operation.
16+
*/
17+
useOperation(opts?: TagOperationOptions<TTags> | void) {
18+
const queryClient = useQueryClient(this.builder.config.queryClient);
19+
const mutationFn: MutationFunction<unknown, TagOperationOptions<TTags> | void> = (
20+
{ tags = [], operation = 'invalidate', filters, options } = opts || {},
21+
) => operateOnTags({ queryClient, tags, operation }, filters, options);
22+
23+
const mutation = useMutation({ mutationFn });
24+
const operate = mutation.mutateAsync;
25+
26+
return [operate, mutation] as const;
27+
}
28+
29+
readonly operate = ({ tags = [], operation = 'invalidate', filters, options }: TagOperationOptions<TTags>) =>
30+
operateOnTags({ queryClient: this.builder.config.queryClient!, tags, operation }, filters, options);
31+
32+
readonly cancel = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'cancel' });
33+
readonly invalidate = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'invalidate' });
34+
readonly refetch = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'refetch' });
35+
readonly remove = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'remove' });
36+
readonly reset = (args: Omit<TagOperationOptions<TTags>, 'operation'>) => this.operate({ ...args, operation: 'reset' });
37+
38+
/**
39+
* This function can be used to update the queries in cache based on given tags.
40+
*/
41+
readonly update = ({
42+
tags = [],
43+
optimistic = false,
44+
client = this.builder.config.queryClient,
45+
...ctx
46+
}: {
47+
tags: readonly QueryUpdateTag<TVars, TData, TError, TTags>[];
48+
optimistic?: boolean;
49+
} & WithOptional<QueryTagContext<TVars, TData, TError>, 'client'>) =>
50+
updateTags({ tags, queryClient: client!, ctx: { client: client!, ...ctx }, optimistic });
51+
}

src/http/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function httpRequest<TData = unknown>(options: HttpRequestOptions)
5050

5151
const resolvedHeaders = new Headers(headers as HeadersInit);
5252

53-
const resolvedCredentials: RequestInit['credentials'] = resolveCredentials(credentials);
53+
const resolvedCredentials = resolveCredentials(credentials);
5454

5555
const request = new Request(finalUrl, {
5656
method,

0 commit comments

Comments
 (0)