Skip to content

Commit e59b136

Browse files
committed
fix: critical bug with enableOnDemand behaviour (enabled false was ignored)
1 parent 6fa478e commit e59b136

File tree

4 files changed

+101
-85
lines changed

4 files changed

+101
-85
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
},
3737
"peerDependencies": {
3838
"disposer-util": "^1.0.9",
39-
"mobx": ">=6.12.4"
39+
"mobx": "^6.12.4"
4040
},
4141
"dependencies": {
42-
"linked-abort-controller": ">=1.0.2",
42+
"linked-abort-controller": "^1.0.2",
4343
"@tanstack/query-core": "^5.66.0"
4444
},
4545
"devDependencies": {
@@ -51,7 +51,7 @@
5151
"@vitest/coverage-istanbul": "2.1.6",
5252
"eslint": "8.57.0",
5353
"js2me-eslint-config": "1.0.6",
54-
"js2me-exports-post-build-script": "2.0.17",
54+
"js2me-exports-post-build-script": "2.0.18",
5555
"jsdom": "25.0.1",
5656
"rimraf": "6.0.1",
5757
"typescript": "5.4.5",

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mobx-inifinite-query.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export class MobxInfiniteQuery<
5959

6060
private isEnabledOnResultDemand: boolean;
6161

62-
private isEnabledHolded = false;
62+
private isStaticDisabled;
63+
6364
/**
6465
* This parameter is responsible for holding the enabled value,
6566
* in cases where the "enableOnDemand" option is enabled
@@ -84,6 +85,7 @@ export class MobxInfiniteQuery<
8485
const {
8586
queryClient,
8687
queryKey: queryKeyOrDynamicQueryKey,
88+
options: getDynamicOptions,
8789
...restOptions
8890
} = config;
8991
this.abortController = new LinkedAbortController(config.abortSignal);
@@ -114,11 +116,22 @@ export class MobxInfiniteQuery<
114116

115117
makeObservable(this);
116118

117-
this.options = this.createOptions({
118-
// ...(this.queryClient.getDefaultOptions().queries as any),
119+
this.isStaticDisabled =
120+
restOptions.enabled === false ||
121+
this.queryClient.getDefaultOptions().queries?.enabled === false;
122+
123+
this.options = this.queryClient.defaultQueryOptions({
119124
...restOptions,
120-
...config.options?.(this),
121-
});
125+
...getDynamicOptions?.(this),
126+
} as any) as MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>;
127+
128+
this.options.structuralSharing = this.options.structuralSharing ?? false;
129+
130+
this.processOptions(this.options);
131+
132+
if (this.isStaticDisabled) {
133+
this.holdedEnabledOption = undefined;
134+
}
122135

123136
if (typeof queryKeyOrDynamicQueryKey === 'function') {
124137
this.options.queryKey = queryKeyOrDynamicQueryKey();
@@ -153,8 +166,8 @@ export class MobxInfiniteQuery<
153166
this.updateResult,
154167
);
155168

156-
if (config.options) {
157-
reaction(() => config.options!(this), this.update, {
169+
if (getDynamicOptions) {
170+
reaction(() => getDynamicOptions(this), this.update, {
158171
signal: this.abortController.signal,
159172
});
160173
}
@@ -164,7 +177,7 @@ export class MobxInfiniteQuery<
164177
() => this.isResultRequsted,
165178
(isRequested) => {
166179
if (isRequested) {
167-
this.update(config.options ? config.options(this) : {});
180+
this.update(getDynamicOptions?.(this) ?? {});
168181
}
169182
},
170183
{
@@ -228,52 +241,47 @@ export class MobxInfiniteQuery<
228241
return this.queryObserver.fetchPreviousPage(options);
229242
}
230243

231-
private createOptions(
244+
update(
232245
optionsUpdate:
233246
| Partial<MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>>
234247
| MobxInfiniteQueryUpdateOptions<TData, TError, TQueryKey, TPageParam>
235248
| MobxInfiniteQueryDynamicOptions<TData, TError, TQueryKey, TPageParam>,
236249
) {
237-
const options = this.queryClient.defaultQueryOptions({
250+
if (this.abortController.signal.aborted) {
251+
return;
252+
}
253+
254+
const nextOptions = {
238255
...this.options,
239256
...optionsUpdate,
240-
} as any) as MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>;
257+
} as MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>;
258+
259+
this.processOptions(nextOptions);
260+
261+
this.options = nextOptions;
262+
263+
this.queryObserver.setOptions(this.options);
264+
}
265+
266+
private processOptions = (
267+
options: MobxInfiniteQueryOptions<TData, TError, TQueryKey, TPageParam>,
268+
) => {
269+
options.queryHash = this.createQueryHash(options.queryKey, options);
241270

242271
// If the on-demand query mode is enabled (when using the result property)
243272
// then, if the user does not request the result, the queries should not be executed
244273
// to do this, we hold the original value of the enabled option
245274
// and set enabled to false until the user requests the result (this.isResultRequsted)
246275
if (this.isEnabledOnResultDemand) {
247276
if (this.isResultRequsted) {
248-
if (this.isEnabledHolded) {
249-
options.enabled = this.holdedEnabledOption;
250-
this.isEnabledHolded = false;
251-
this.holdedEnabledOption = undefined;
252-
}
277+
options.enabled = this.holdedEnabledOption;
278+
this.holdedEnabledOption = undefined;
253279
} else {
254280
this.holdedEnabledOption = options.enabled;
255281
options.enabled = false;
256-
this.isEnabledHolded = true;
257282
}
258283
}
259-
260-
options.structuralSharing = options.structuralSharing ?? false;
261-
options.queryHash = this.createQueryHash(options.queryKey, options);
262-
263-
return options;
264-
}
265-
266-
update(
267-
options:
268-
| MobxInfiniteQueryUpdateOptions<TData, TError, TQueryKey, TPageParam>
269-
| MobxInfiniteQueryDynamicOptions<TData, TError, TQueryKey, TPageParam>,
270-
) {
271-
if (this.abortController.signal.aborted) {
272-
return;
273-
}
274-
this.options = this.createOptions(options);
275-
this.queryObserver.setOptions(this.options);
276-
}
284+
};
277285

278286
public get result() {
279287
if (!this.isResultRequsted) {

src/mobx-query.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export class MobxQuery<
4747

4848
private isEnabledOnResultDemand: boolean;
4949

50-
private isEnabledHolded = false;
50+
private isStaticDisabled;
51+
5152
/**
5253
* This parameter is responsible for holding the enabled value,
5354
* in cases where the "enableOnDemand" option is enabled
@@ -64,6 +65,7 @@ export class MobxQuery<
6465
const {
6566
queryClient,
6667
queryKey: queryKeyOrDynamicQueryKey,
68+
options: getDynamicOptions,
6769
...restOptions
6870
} = config;
6971
this.abortController = new LinkedAbortController(config.abortSignal);
@@ -94,11 +96,22 @@ export class MobxQuery<
9496

9597
makeObservable(this);
9698

97-
this.options = this.createOptions({
98-
// ...(this.queryClient.getDefaultOptions().queries as any),
99+
this.isStaticDisabled =
100+
restOptions.enabled === false ||
101+
this.queryClient.getDefaultOptions().queries?.enabled === false;
102+
103+
this.options = this.queryClient.defaultQueryOptions({
99104
...restOptions,
100-
...config.options?.(this),
101-
});
105+
...getDynamicOptions?.(this),
106+
} as any);
107+
108+
this.options.structuralSharing = this.options.structuralSharing ?? false;
109+
110+
this.processOptions(this.options);
111+
112+
if (this.isStaticDisabled) {
113+
this.holdedEnabledOption = undefined;
114+
}
102115

103116
if (typeof queryKeyOrDynamicQueryKey === 'function') {
104117
this.options.queryKey = queryKeyOrDynamicQueryKey();
@@ -133,8 +146,8 @@ export class MobxQuery<
133146
this.updateResult,
134147
);
135148

136-
if (config.options) {
137-
reaction(() => config.options!(this), this.update, {
149+
if (getDynamicOptions) {
150+
reaction(() => getDynamicOptions(this), this.update, {
138151
signal: this.abortController.signal,
139152
});
140153
}
@@ -144,7 +157,7 @@ export class MobxQuery<
144157
() => this.isResultRequsted,
145158
(isRequested) => {
146159
if (isRequested) {
147-
this.update(config.options ? config.options(this) : {});
160+
this.update(getDynamicOptions?.(this) ?? {});
148161
}
149162
},
150163
{
@@ -193,52 +206,47 @@ export class MobxQuery<
193206
);
194207
}
195208

196-
private createOptions(
209+
update(
197210
optionsUpdate:
198211
| Partial<MobxQueryOptions<TData, TError, TQueryKey>>
199212
| MobxQueryUpdateOptions<TData, TError, TQueryKey>
200213
| MobxQueryDynamicOptions<TData, TError, TQueryKey>,
201214
) {
202-
const options = this.queryClient.defaultQueryOptions({
215+
if (this.abortController.signal.aborted) {
216+
return;
217+
}
218+
219+
const nextOptions = {
203220
...this.options,
204221
...optionsUpdate,
205-
} as any) as MobxQueryOptions<TData, TError, TQueryKey>;
222+
};
223+
224+
this.processOptions(nextOptions);
225+
226+
this.options = nextOptions;
227+
228+
this.queryObserver.setOptions(this.options);
229+
}
230+
231+
private processOptions = (
232+
options: MobxQueryOptions<TData, TError, TQueryKey>,
233+
) => {
234+
options.queryHash = this.createQueryHash(options.queryKey, options);
206235

207236
// If the on-demand query mode is enabled (when using the result property)
208237
// then, if the user does not request the result, the queries should not be executed
209238
// to do this, we hold the original value of the enabled option
210239
// and set enabled to false until the user requests the result (this.isResultRequsted)
211240
if (this.isEnabledOnResultDemand) {
212241
if (this.isResultRequsted) {
213-
if (this.isEnabledHolded) {
214-
options.enabled = this.holdedEnabledOption;
215-
this.isEnabledHolded = false;
216-
this.holdedEnabledOption = undefined;
217-
}
242+
options.enabled = this.holdedEnabledOption;
243+
this.holdedEnabledOption = undefined;
218244
} else {
219245
this.holdedEnabledOption = options.enabled;
220246
options.enabled = false;
221-
this.isEnabledHolded = true;
222247
}
223248
}
224-
225-
options.structuralSharing = options.structuralSharing ?? false;
226-
options.queryHash = this.createQueryHash(options.queryKey, options);
227-
228-
return options;
229-
}
230-
231-
update(
232-
options:
233-
| MobxQueryUpdateOptions<TData, TError, TQueryKey>
234-
| MobxQueryDynamicOptions<TData, TError, TQueryKey>,
235-
) {
236-
if (this.abortController.signal.aborted) {
237-
return;
238-
}
239-
this.options = this.createOptions(options);
240-
this.queryObserver.setOptions(this.options);
241-
}
249+
};
242250

243251
public get result() {
244252
if (!this.isResultRequsted) {

0 commit comments

Comments
 (0)