Skip to content

Commit 26cf941

Browse files
committed
fix: critical bug with enableOnDemand behaviour
1 parent 50f94dd commit 26cf941

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

src/mobx-inifinite-query.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ export class MobxInfiniteQuery<
263263
this.queryObserver.setOptions(this.options);
264264
}
265265

266+
private isEnableHolded = false;
267+
268+
private enableHolder = () => false;
269+
266270
private processOptions = (
267271
options: MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>,
268272
) => {
@@ -273,12 +277,22 @@ export class MobxInfiniteQuery<
273277
// to do this, we hold the original value of the enabled option
274278
// and set enabled to false until the user requests the result (this.isResultRequsted)
275279
if (this.isEnabledOnResultDemand) {
280+
if (this.isEnableHolded && options.enabled !== this.enableHolder) {
281+
this.holdedEnabledOption = options.enabled;
282+
}
283+
276284
if (this.isResultRequsted) {
277-
options.enabled = this.holdedEnabledOption;
278-
this.holdedEnabledOption = undefined;
285+
if (this.isEnableHolded) {
286+
options.enabled =
287+
this.holdedEnabledOption === this.enableHolder
288+
? undefined
289+
: this.holdedEnabledOption;
290+
this.isEnableHolded = false;
291+
}
279292
} else {
293+
this.isEnableHolded = true;
280294
this.holdedEnabledOption = options.enabled;
281-
options.enabled = false;
295+
options.enabled = this.enableHolder;
282296
}
283297
}
284298
};

src/mobx-query.test.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ describe('MobxQuery', () => {
514514
const mobxQuery = new MobxQueryMock({
515515
queryFn: () => 10,
516516
enableOnDemand: true,
517-
enabled: () => false,
517+
enabled: function getEnabledFromUnitTest() {
518+
return false;
519+
},
518520
});
519521

520522
mobxQuery.result.data;
@@ -599,7 +601,48 @@ describe('MobxQuery', () => {
599601

600602
expect(mobxQuery.spies.queryFn).toBeCalledTimes(0);
601603

602-
valueBox.set('value');
604+
runInAction(() => {
605+
valueBox.set('value');
606+
});
607+
608+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
609+
610+
mobxQuery.dispose();
611+
});
612+
613+
it('should enable query from dynamic options ONLY AFTER result is requested (multiple observable updates)', () => {
614+
const valueBox = observable.box<string | null | undefined>();
615+
616+
const mobxQuery = new MobxQueryMock({
617+
queryFn: () => 100,
618+
enableOnDemand: true,
619+
options: () => {
620+
const value = valueBox.get();
621+
return {
622+
queryKey: ['values', value] as const,
623+
enabled: value === 'kek',
624+
};
625+
},
626+
});
627+
628+
mobxQuery.result.data;
629+
mobxQuery.result.isLoading;
630+
631+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(0);
632+
633+
runInAction(() => {
634+
valueBox.set(null);
635+
});
636+
637+
runInAction(() => {
638+
valueBox.set('faslse');
639+
});
640+
641+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(0);
642+
643+
runInAction(() => {
644+
valueBox.set('kek');
645+
});
603646

604647
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
605648

src/mobx-query.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class MobxQuery<
111111

112112
if (this.isStaticDisabled) {
113113
this.holdedEnabledOption = undefined;
114+
console.info('hold(3)', `${this.holdedEnabledOption}`);
114115
}
115116

116117
if (typeof queryKeyOrDynamicQueryKey === 'function') {
@@ -228,6 +229,10 @@ export class MobxQuery<
228229
this.queryObserver.setOptions(this.options);
229230
}
230231

232+
private isEnableHolded = false;
233+
234+
private enableHolder = () => false;
235+
231236
private processOptions = (
232237
options: MobxQueryOptions<TData, TError, TQueryKey>,
233238
) => {
@@ -238,12 +243,22 @@ export class MobxQuery<
238243
// to do this, we hold the original value of the enabled option
239244
// and set enabled to false until the user requests the result (this.isResultRequsted)
240245
if (this.isEnabledOnResultDemand) {
246+
if (this.isEnableHolded && options.enabled !== this.enableHolder) {
247+
this.holdedEnabledOption = options.enabled;
248+
}
249+
241250
if (this.isResultRequsted) {
242-
options.enabled = this.holdedEnabledOption;
243-
this.holdedEnabledOption = undefined;
251+
if (this.isEnableHolded) {
252+
options.enabled =
253+
this.holdedEnabledOption === this.enableHolder
254+
? undefined
255+
: this.holdedEnabledOption;
256+
this.isEnableHolded = false;
257+
}
244258
} else {
259+
this.isEnableHolded = true;
245260
this.holdedEnabledOption = options.enabled;
246-
options.enabled = false;
261+
options.enabled = this.enableHolder;
247262
}
248263
}
249264
};

0 commit comments

Comments
 (0)