Skip to content

Commit b626ab4

Browse files
committed
refactor: query features and mutation features inside queries/mutations
1 parent fd3f28c commit b626ab4

File tree

4 files changed

+95
-138
lines changed

4 files changed

+95
-138
lines changed

.changeset/red-groups-tie.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+
rework query/mutation features code (refactor and clean code inside queries and mutations)

src/inifinite-query.ts

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
InfiniteQueryUpdateOptionsAllVariants,
3838
} from './inifinite-query.types';
3939
import { Query } from './query';
40-
import { QueryClient } from './query-client';
4140
import { AnyQueryClient, QueryClientHooks } from './query-client.types';
4241
import { QueryFeatures } from './query.types';
4342

@@ -242,9 +241,9 @@ export class InfiniteQuery<
242241
TPageParam
243242
>;
244243

245-
private isEnabledOnResultDemand: boolean;
246244
isResultRequsted: boolean;
247-
protected isLazy?: boolean;
245+
246+
protected features: QueryFeatures;
248247

249248
/**
250249
* This parameter is responsible for holding the enabled value,
@@ -259,9 +258,10 @@ export class InfiniteQuery<
259258
>['enabled'];
260259
private _observerSubscription?: VoidFunction;
261260
private hooks?: QueryClientHooks;
261+
262262
protected errorListeners: InfiniteQueryErrorListener<TError>[];
263263
protected doneListeners: InfiniteQueryDoneListener<TData>[];
264-
protected cumulativeQueryHash: boolean;
264+
265265
protected cumulativeQueryKeyHashesSet: Set<string>;
266266

267267
constructor(
@@ -325,31 +325,30 @@ export class InfiniteQuery<
325325
this.queryClient = queryClient;
326326
this._result = undefined as any;
327327
this.isResultRequsted = false;
328-
this.isEnabledOnResultDemand = config.enableOnDemand ?? false;
328+
329329
this.errorListeners = [];
330330
this.doneListeners = [];
331-
this.hooks =
332-
'hooks' in this.queryClient ? this.queryClient.hooks : undefined;
333-
this.isLazy = this.config.lazy;
334-
this.cumulativeQueryHash = !!config.cumulativeQueryHash;
335331

336-
let transformError: QueryFeatures['transformError'] = config.transformError;
332+
this.features = {
333+
cumulativeQueryHash: config.cumulativeQueryHash,
334+
enableOnDemand: config.enableOnDemand,
335+
lazy: config.lazy,
336+
resetOnDestroy: config.resetOnDestroy,
337+
removeOnDestroy: config.removeOnDestroy,
338+
transformError: config.transformError,
339+
dynamicOptionsUpdateDelay: config.dynamicOptionsUpdateDelay,
340+
};
337341

338342
if ('queryFeatures' in queryClient) {
339-
if (this.config.lazy === undefined) {
340-
this.isLazy = queryClient.queryFeatures.lazy ?? false;
341-
}
342-
if (config.enableOnDemand === undefined) {
343-
this.isEnabledOnResultDemand =
344-
queryClient.queryFeatures.enableOnDemand ?? false;
345-
}
346-
if (config.cumulativeQueryHash === undefined) {
347-
this.cumulativeQueryHash =
348-
queryClient.queryFeatures.cumulativeQueryHash ?? false;
349-
}
350-
if (!transformError) {
351-
transformError = queryClient.queryFeatures.transformError;
352-
}
343+
this.features.lazy ??= queryClient.queryFeatures.lazy;
344+
this.features.enableOnDemand ??=
345+
queryClient.queryFeatures.enableOnDemand ??
346+
queryClient.queryFeatures.resetOnDispose;
347+
this.features.cumulativeQueryHash ??=
348+
queryClient.queryFeatures.cumulativeQueryHash;
349+
this.features.transformError ??= queryClient.queryFeatures.transformError;
350+
351+
this.hooks = queryClient.hooks;
353352
}
354353

355354
observable.deep(this, '_result');
@@ -363,9 +362,9 @@ export class InfiniteQuery<
363362

364363
originalQueryProperties.forEach((property) => {
365364
if (this[property]) return;
366-
if (property === 'error' && transformError) {
365+
if (property === 'error' && this.features.transformError) {
367366
Object.defineProperty(this, property, {
368-
get: () => transformError(this.result[property]),
367+
get: () => this.features.transformError!(this.result[property]),
369368
});
370369
} else {
371370
Object.defineProperty(this, property, {
@@ -432,7 +431,7 @@ export class InfiniteQuery<
432431
// @ts-expect-error
433432
this.updateResult(this.queryObserver.getOptimisticResult(this.options));
434433

435-
if (this.isLazy) {
434+
if (this.features.lazy) {
436435
const cleanup = lazyObserve({
437436
context: this,
438437
property: '_result',
@@ -566,7 +565,7 @@ export class InfiniteQuery<
566565
}
567566

568567
public get result() {
569-
if (this.isEnabledOnResultDemand && !this.isResultRequsted) {
568+
if (this.features.enableOnDemand && !this.isResultRequsted) {
570569
runInAction(() => {
571570
this.isResultRequsted = true;
572571
});
@@ -628,27 +627,11 @@ export class InfiniteQuery<
628627
this.queryObserver.destroy();
629628
this.isResultRequsted = false;
630629

631-
let isNeedToReset =
632-
this.config.resetOnDestroy || this.config.resetOnDispose;
633-
let isNeedToRemove = this.config.removeOnDestroy;
634-
635-
if (this.queryClient instanceof QueryClient) {
636-
if (isNeedToReset === undefined) {
637-
isNeedToReset =
638-
this.queryClient.queryFeatures.resetOnDestroy ||
639-
this.queryClient.queryFeatures.resetOnDispose;
640-
}
641-
642-
if (isNeedToRemove === undefined) {
643-
isNeedToRemove = this.queryClient.queryFeatures.removeOnDestroy;
644-
}
645-
}
646-
647-
if (isNeedToReset) {
630+
if (this.features.resetOnDestroy) {
648631
this.reset();
649632
}
650633

651-
if (isNeedToRemove) {
634+
if (this.features.removeOnDestroy) {
652635
this.remove();
653636
}
654637

src/mutation.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export class Mutation<
5858

5959
result: MutationObserverResult<TData, TError, TVariables, TContext>;
6060

61-
protected isLazy?: boolean;
62-
protected isResetOnDestroy?: MutationFeatures['resetOnDestroy'];
61+
protected features: MutationFeatures;
6362

6463
protected settledListeners: MutationSettledListener<
6564
TData,
@@ -129,44 +128,29 @@ export class Mutation<
129128
constructor(
130129
protected config: MutationConfig<TData, TVariables, TError, TContext>,
131130
) {
132-
const {
133-
queryClient,
134-
invalidateQueries,
135-
invalidateByKey: providedInvalidateByKey,
136-
mutationFn,
137-
...restOptions
138-
} = config;
131+
const { queryClient, invalidateQueries, mutationFn, ...restOptions } =
132+
config;
139133
this.abortController = new LinkedAbortController(config.abortSignal);
140134
this.queryClient = queryClient;
141135
this.result = undefined as any;
142-
this.isLazy = this.config.lazy;
136+
this.features = {
137+
invalidateByKey: config.invalidateByKey,
138+
lazy: config.lazy,
139+
resetOnDestroy: config.resetOnDestroy ?? config.resetOnDestroy,
140+
transformError: config.transformError,
141+
};
143142
this.settledListeners = [];
144143
this.errorListeners = [];
145144
this.doneListeners = [];
146-
this.isResetOnDestroy =
147-
this.config.resetOnDestroy ?? this.config.resetOnDispose;
148-
149-
let transformError: MutationFeatures['transformError'] =
150-
config.transformError;
151-
152-
let invalidateByKey: MutationFeatures['invalidateByKey'] =
153-
providedInvalidateByKey;
154145

155146
if ('mutationFeatures' in queryClient) {
156-
if (providedInvalidateByKey === undefined) {
157-
invalidateByKey = queryClient.mutationFeatures.invalidateByKey;
158-
}
159-
if (this.config.lazy === undefined) {
160-
this.isLazy = queryClient.mutationFeatures.lazy;
161-
}
162-
if (this.isResetOnDestroy === undefined) {
163-
this.isResetOnDestroy =
164-
queryClient.mutationFeatures.resetOnDestroy ??
165-
queryClient.mutationFeatures.resetOnDispose;
166-
}
167-
if (!transformError) {
168-
transformError = queryClient.queryFeatures.transformError;
169-
}
147+
this.features.invalidateByKey ??=
148+
queryClient.mutationFeatures.invalidateByKey;
149+
this.features.lazy ??= queryClient.mutationFeatures.lazy;
150+
this.features.resetOnDestroy ??=
151+
queryClient.mutationFeatures.resetOnDestroy ??
152+
queryClient.mutationFeatures.resetOnDispose;
153+
this.features.transformError ??= queryClient.queryFeatures.transformError;
170154

171155
this.hooks = queryClient.hooks;
172156
}
@@ -177,9 +161,9 @@ export class Mutation<
177161
this.start = this.start.bind(this);
178162

179163
originalMutationProperties.forEach((property) => {
180-
if (property === 'error' && transformError) {
164+
if (property === 'error' && this.features.transformError) {
181165
Object.defineProperty(this, property, {
182-
get: () => transformError(this.result[property]),
166+
get: () => this.features.transformError!(this.result[property]),
183167
});
184168
} else {
185169
Object.defineProperty(this, property, {
@@ -207,7 +191,7 @@ export class Mutation<
207191

208192
this.updateResult(this.mutationObserver.getCurrentResult());
209193

210-
if (this.isLazy) {
194+
if (this.features.lazy) {
211195
const cleanup = lazyObserve({
212196
context: this,
213197
property: 'result',
@@ -269,10 +253,12 @@ export class Mutation<
269253
});
270254
}
271255

272-
if (invalidateByKey && this.mutationOptions.mutationKey) {
256+
if (this.features.invalidateByKey && this.mutationOptions.mutationKey) {
273257
this.onDone(() => {
274258
this.queryClient.invalidateQueries({
275-
...(invalidateByKey === true ? {} : invalidateByKey),
259+
...(this.features.invalidateByKey === true
260+
? {}
261+
: this.features.invalidateByKey),
276262
queryKey: this.mutationOptions.mutationKey,
277263
});
278264
});
@@ -286,7 +272,7 @@ export class Mutation<
286272
variables: TVariables,
287273
options?: MutationOptions<TData, TError, TVariables, TContext>,
288274
) {
289-
if (this.isLazy) {
275+
if (this.features.lazy) {
290276
let error: any;
291277

292278
try {
@@ -365,7 +351,7 @@ export class Mutation<
365351
this.errorListeners = [];
366352
this.settledListeners = [];
367353

368-
if (this.isResetOnDestroy) {
354+
if (this.features.resetOnDestroy) {
369355
this.reset();
370356
}
371357

0 commit comments

Comments
 (0)