Skip to content

Commit 5ed6dd0

Browse files
committed
fix: critical bug with not working default query client options - (enabled option); fix: enableOnDemand more predictable (it enabled the disabled query when result is requested)
1 parent 5b46e62 commit 5ed6dd0

File tree

3 files changed

+84
-20
lines changed

3 files changed

+84
-20
lines changed

src/mobx-inifinite-query.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export class MobxInfiniteQuery<
5959

6060
private isEnabledOnResultDemand: boolean;
6161

62-
private _originEnabled: MobxInfiniteQueryOptions<
62+
private isEnabledHolded = false;
63+
/**
64+
* This parameter is responsible for holding the enabled value,
65+
* in cases where the "enableOnDemand" option is enabled
66+
*/
67+
private holdedEnabledOption: MobxInfiniteQueryOptions<
6368
TData,
6469
TError,
6570
TQueryKey,
@@ -212,7 +217,7 @@ export class MobxInfiniteQuery<
212217
return false;
213218
}
214219

215-
return this._originEnabled;
220+
return this.holdedEnabledOption;
216221
}
217222

218223
fetchNextPage(options?: FetchNextPageOptions | undefined) {
@@ -233,11 +238,28 @@ export class MobxInfiniteQuery<
233238
...this.options,
234239
...optionsUpdate,
235240
} as any) as MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>;
236-
if ('enabled' in optionsUpdate) {
237-
this._originEnabled = options.enabled;
241+
242+
// If the on-demand query mode is enabled (when using the result property)
243+
// then, if the user does not request the result, the queries should not be executed
244+
// to do this, we hold the original value of the enabled option
245+
// and set enabled to false until the user requests the result (this.isResultRequsted)
246+
if (this.isEnabledOnResultDemand) {
247+
if (options.enabled !== false) {
248+
this.holdedEnabledOption = options.enabled;
249+
}
250+
251+
if (this.isResultRequsted) {
252+
if (this.isEnabledHolded) {
253+
options.enabled = this.holdedEnabledOption;
254+
this.isEnabledHolded = false;
255+
}
256+
} else {
257+
options.enabled = false;
258+
this.isEnabledHolded = true;
259+
}
238260
}
261+
239262
options.structuralSharing = options.structuralSharing ?? false;
240-
options.enabled = this.checkIsEnabled();
241263
options.queryHash = this.createQueryHash(options.queryKey, options);
242264

243265
return options;

src/mobx-query.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe('MobxQuery', () => {
197197
});
198198

199199
describe('"enabled" reactive parameter', () => {
200-
it.skip('should work', async () => {
200+
it('should be DISABLED from default query options (from query client)', async () => {
201201
const queryClient = new MobxQueryClient({
202202
defaultOptions: {
203203
queries: {
@@ -494,7 +494,7 @@ describe('MobxQuery', () => {
494494
mobxQuery.dispose();
495495
});
496496

497-
it('should not call query event if result is requested (reason: "enabled": false)', async () => {
497+
it('should call query event if result is requested (reason: "enableOnDemand": true)', async () => {
498498
const mobxQuery = new MobxQueryMock({
499499
queryFn: () => 10,
500500
enableOnDemand: true,
@@ -505,7 +505,7 @@ describe('MobxQuery', () => {
505505

506506
await when(() => !mobxQuery._rawResult.isLoading);
507507

508-
expect(mobxQuery.spies.queryFn).toBeCalledTimes(0);
508+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
509509

510510
mobxQuery.dispose();
511511
});
@@ -555,6 +555,30 @@ describe('MobxQuery', () => {
555555

556556
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
557557

558+
mobxQuery.dispose();
559+
});
560+
it('should call query if result is requested (with "enabled" false from default query client options)', async () => {
561+
const queryClient = new MobxQueryClient({
562+
defaultOptions: {
563+
queries: {
564+
enabled: false,
565+
},
566+
},
567+
});
568+
const mobxQuery = new MobxQueryMock(
569+
{
570+
queryKey: ['test', 0 as number] as const,
571+
queryFn: () => 100,
572+
enableOnDemand: true,
573+
},
574+
queryClient,
575+
);
576+
577+
mobxQuery.result.data;
578+
mobxQuery.result.isLoading;
579+
580+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
581+
558582
mobxQuery.dispose();
559583
});
560584
});

src/mobx-query.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ export class MobxQuery<
4747

4848
private isEnabledOnResultDemand: boolean;
4949

50-
private _originEnabled: MobxQueryOptions<TData, TError, TQueryKey>['enabled'];
50+
private isEnabledHolded = false;
51+
/**
52+
* This parameter is responsible for holding the enabled value,
53+
* in cases where the "enableOnDemand" option is enabled
54+
*/
55+
private holdedEnabledOption: MobxQueryOptions<
56+
TData,
57+
TError,
58+
TQueryKey
59+
>['enabled'];
5160
private _observerSubscription?: VoidFunction;
5261
private hooks?: MobxQueryClientHooks;
5362

@@ -184,14 +193,6 @@ export class MobxQuery<
184193
);
185194
}
186195

187-
private checkIsEnabled() {
188-
if (this.isEnabledOnResultDemand && !this.isResultRequsted) {
189-
return false;
190-
}
191-
192-
return this._originEnabled;
193-
}
194-
195196
private createOptions(
196197
optionsUpdate:
197198
| Partial<MobxQueryOptions<TData, TError, TQueryKey>>
@@ -202,11 +203,28 @@ export class MobxQuery<
202203
...this.options,
203204
...optionsUpdate,
204205
} as any) as MobxQueryOptions<TData, TError, TQueryKey>;
205-
if ('enabled' in optionsUpdate) {
206-
this._originEnabled = options.enabled;
206+
207+
// If the on-demand query mode is enabled (when using the result property)
208+
// then, if the user does not request the result, the queries should not be executed
209+
// to do this, we hold the original value of the enabled option
210+
// and set enabled to false until the user requests the result (this.isResultRequsted)
211+
if (this.isEnabledOnResultDemand) {
212+
if (options.enabled !== false) {
213+
this.holdedEnabledOption = options.enabled;
214+
}
215+
216+
if (this.isResultRequsted) {
217+
if (this.isEnabledHolded) {
218+
options.enabled = this.holdedEnabledOption;
219+
this.isEnabledHolded = false;
220+
}
221+
} else {
222+
options.enabled = false;
223+
this.isEnabledHolded = true;
224+
}
207225
}
226+
208227
options.structuralSharing = options.structuralSharing ?? false;
209-
options.enabled = this.checkIsEnabled();
210228
options.queryHash = this.createQueryHash(options.queryKey, options);
211229

212230
return options;

0 commit comments

Comments
 (0)