Skip to content

Commit 4b78582

Browse files
committed
improve sync tags option
1 parent 2152c80 commit 4b78582

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

examples/vite/src/App.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ await setupWorker(...getMockHandlers()).start({
1313
quiet: true,
1414
});
1515

16-
const builder = new HttpQueryBuilder({
17-
syncChannel: new BroadcastChannel('react-query-builder'),
18-
})
19-
.withClient(queryClient)
20-
.withBaseUrl(baseUrl)
21-
.withTagTypes<{
22-
post: PostData;
23-
posts: PostData[];
24-
comments: CommentData;
25-
refreshable: unknown;
26-
}>();
16+
const builder = new HttpQueryBuilder().withClient(queryClient).withBaseUrl(baseUrl).withTagTypes<{
17+
post: PostData;
18+
posts: PostData[];
19+
comments: CommentData;
20+
refreshable: unknown;
21+
}>();
2722

2823
const resetMutation = builder.withPath('/reset').withMethod('post').withUpdates('*');
2924

packages/react-query-builder/src/builder/HttpQueryBuilder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class HttpQueryBuilder<
8080
declare withError: <TError$>() => HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta, TData, TError$, TTags, TFlags, TKey>;
8181
declare withClient: (
8282
queryClient: QueryClient,
83+
opts?: { syncTagsWithOtherTabs?: boolean },
8384
) => HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta, TData, TError, TTags, TFlags | 'withClient', TKey>;
8485

8586
declare withPagination: (

packages/react-query-builder/src/builder/QueryBuilder.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ export class QueryBuilder<
7474
static tagCacheId = 0;
7575

7676
withTags(...tags: QueryTagOption<TVars, TData, TError, QueryTagObject<TTags>>[]): this {
77-
if (this.config.queryClient) {
78-
this.config.syncChannel?.addEventListener('message', (event) => {
79-
const { type, data } = event.data;
80-
if (type === 'invalidate') {
81-
const queryClient = this.config.queryClient;
82-
if (queryClient) operateOnTags({ queryClient, tags: data });
83-
}
84-
});
85-
}
86-
8777
return this.withMiddleware(createTagMiddleware(tags.flat(), QueryBuilder.tagCacheId++)) as unknown as this;
8878
}
8979

@@ -97,8 +87,21 @@ export class QueryBuilder<
9787
return this as any;
9888
}
9989

100-
withClient(queryClient: QueryClient): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'withClient'> {
101-
return this.withConfig({ queryClient }) as any;
90+
withClient(
91+
queryClient: QueryClient,
92+
{ syncTagsWithOtherTabs = true }: { syncTagsWithOtherTabs?: boolean } = {},
93+
): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'withClient'> {
94+
let syncChannel: BroadcastChannel | undefined = undefined;
95+
96+
if (syncTagsWithOtherTabs) {
97+
syncChannel = new BroadcastChannel('react-query-builder-tags');
98+
syncChannel.addEventListener('message', (event) => {
99+
const { type, data } = event.data;
100+
if (type === 'invalidate') operateOnTags({ queryClient, tags: data });
101+
});
102+
}
103+
104+
return this.withConfig({ queryClient, syncChannel }) as any;
102105
}
103106

104107
withPagination(

0 commit comments

Comments
 (0)