Skip to content

Commit 91da4e2

Browse files
committed
feat: add cumulativeQueryHash option
1 parent 6f6d76e commit 91da4e2

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.changeset/easy-readers-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
added `cumulativeQueryHash` option for queries

src/inifinite-query.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ export class InfiniteQuery<
261261
private hooks?: QueryClientHooks;
262262
protected errorListeners: InfiniteQueryErrorListener<TError>[];
263263
protected doneListeners: InfiniteQueryDoneListener<TData>[];
264+
protected cumulativeQueryHash: boolean;
265+
protected cumulativeQueryKeyHashesSet: Set<string>;
264266

265267
constructor(
266268
config: InfiniteQueryConfig<
@@ -310,6 +312,7 @@ export class InfiniteQuery<
310312
config = args[0];
311313
getDynamicOptions = args[0].options;
312314
}
315+
this.cumulativeQueryKeyHashesSet = new Set();
313316

314317
const { queryKey: queryKeyOrDynamicQueryKey, ...restOptions } = config;
315318

@@ -328,6 +331,8 @@ export class InfiniteQuery<
328331
this.hooks =
329332
'hooks' in this.queryClient ? this.queryClient.hooks : undefined;
330333
this.isLazy = this.config.lazy;
334+
this.cumulativeQueryHash = !!config.cumulativeQueryHash;
335+
331336
let transformError: QueryFeatures['transformError'] = config.transformError;
332337

333338
if ('queryFeatures' in queryClient) {
@@ -338,6 +343,10 @@ export class InfiniteQuery<
338343
this.isEnabledOnResultDemand =
339344
queryClient.queryFeatures.enableOnDemand ?? false;
340345
}
346+
if (config.cumulativeQueryHash === undefined) {
347+
this.cumulativeQueryHash =
348+
queryClient.queryFeatures.cumulativeQueryHash ?? false;
349+
}
341350
if (!transformError) {
342351
transformError = queryClient.queryFeatures.transformError;
343352
}
@@ -645,6 +654,9 @@ export class InfiniteQuery<
645654
}
646655

647656
delete this._observerSubscription;
657+
658+
this.cumulativeQueryKeyHashesSet.clear();
659+
648660
this.hooks?.onInfiniteQueryDestroy?.(this);
649661
}
650662

src/query.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export class Query<
113113
protected errorListeners: QueryErrorListener<TError>[];
114114
protected doneListeners: QueryDoneListener<TData>[];
115115

116+
protected cumulativeQueryHash: boolean;
117+
protected cumulativeQueryKeyHashesSet: Set<string>;
118+
116119
protected config: QueryConfig<
117120
TQueryFnData,
118121
TError,
@@ -266,6 +269,7 @@ export class Query<
266269
config = args[0];
267270
getDynamicOptions = args[0].options;
268271
}
272+
this.cumulativeQueryKeyHashesSet = new Set();
269273

270274
const { queryKey: queryKeyOrDynamicQueryKey, ...restOptions } = config;
271275

@@ -284,6 +288,8 @@ export class Query<
284288
this.hooks =
285289
'hooks' in this.queryClient ? this.queryClient.hooks : undefined;
286290
this.isLazy = this.config.lazy;
291+
this.cumulativeQueryHash = !!config.cumulativeQueryHash;
292+
287293
let transformError: QueryFeatures['transformError'] = config.transformError;
288294

289295
if ('queryFeatures' in queryClient) {
@@ -294,6 +300,10 @@ export class Query<
294300
this.isEnabledOnResultDemand =
295301
queryClient.queryFeatures.enableOnDemand ?? false;
296302
}
303+
if (config.cumulativeQueryHash === undefined) {
304+
this.cumulativeQueryHash =
305+
queryClient.queryFeatures.cumulativeQueryHash ?? false;
306+
}
297307
if (!transformError) {
298308
transformError = queryClient.queryFeatures.transformError;
299309
}
@@ -530,6 +540,10 @@ export class Query<
530540
) {
531541
options.queryHash = this.createQueryHash(options.queryKey, options);
532542

543+
if (this.cumulativeQueryHash) {
544+
this.cumulativeQueryKeyHashesSet.add(options.queryHash);
545+
}
546+
533547
// If the on-demand query mode is enabled (when using the result property)
534548
// then, if the user does not request the result, the queries should not be executed
535549
// to do this, we hold the original value of the enabled option
@@ -579,6 +593,14 @@ export class Query<
579593
}
580594

581595
async reset(params?: QueryResetParams, options?: ResetOptions) {
596+
if (this.cumulativeQueryHash) {
597+
return await this.queryClient.resetQueries({
598+
predicate: (query) =>
599+
this.cumulativeQueryKeyHashesSet.has(query.options.queryHash!),
600+
...params,
601+
});
602+
}
603+
582604
return await this.queryClient.resetQueries(
583605
{
584606
queryKey: this.options.queryKey,
@@ -590,6 +612,14 @@ export class Query<
590612
}
591613

592614
remove(params?: QueryRemoveParams) {
615+
if (this.cumulativeQueryHash) {
616+
return this.queryClient.removeQueries({
617+
predicate: (query) =>
618+
this.cumulativeQueryKeyHashesSet.has(query.options.queryHash!),
619+
...params,
620+
});
621+
}
622+
593623
return this.queryClient.removeQueries({
594624
queryKey: this.options.queryKey,
595625
exact: true,
@@ -657,6 +687,8 @@ export class Query<
657687

658688
delete this._observerSubscription;
659689

690+
this.cumulativeQueryKeyHashesSet.clear();
691+
660692
this.hooks?.onQueryDestroy?.(this);
661693
}
662694

src/query.types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export interface QueryInvalidateParams
1919
export type MobxQueryInvalidateParams = QueryInvalidateParams;
2020

2121
export interface QueryResetParams
22-
extends Partial<Omit<QueryFilters, 'queryKey' | 'exact'>> {}
22+
extends Partial<Omit<QueryFilters, 'queryKey' | 'exact' | 'predicate'>> {}
2323

2424
export interface QueryRemoveParams
25-
extends Partial<Omit<QueryFilters, 'queryKey' | 'exact'>> {}
25+
extends Partial<Omit<QueryFilters, 'queryKey' | 'exact' | 'predicate'>> {}
2626

2727
/**
2828
* @deprecated ⚠️ use `QueryResetParams`. This type will be removed in next major release
@@ -139,6 +139,12 @@ export interface QueryFeatures {
139139
*/
140140
lazy?: boolean;
141141
transformError?: (error: any) => any;
142+
143+
/**
144+
* Cumulative all query key hashes when user update options inside query
145+
* When destroy happened all accumulated query keys will be removed (if removeOnDestroy is true), and reseted (if resetOnDestroy is true)
146+
*/
147+
cumulativeQueryHash?: boolean;
142148
}
143149

144150
/**

0 commit comments

Comments
 (0)