Skip to content

Commit 9e60799

Browse files
committed
fix: bug with not reactive options #64
1 parent 9aef5b1 commit 9e60799

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": patch
3+
---
4+
5+
fix bug with not reactive options in queries sometimes (#64)

src/inifinite-query.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ export class InfiniteQuery<
470470
reaction(getAllDynamicOptions, this.update, {
471471
delay: this.config.dynamicOptionsUpdateDelay,
472472
signal: config.abortSignal,
473+
fireImmediately: true,
473474
equals: this.features.dynamicOptionsComparer,
474475
});
475476
}

src/query.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
vi,
3232
} from 'vitest';
3333
import { sleep } from 'yummies/async';
34-
34+
import type { Maybe } from 'yummies/types';
3535
import { createQuery } from './preset/index.js';
3636
import { Query } from './query.js';
3737
import type {
@@ -3898,4 +3898,26 @@ describe('Query', () => {
38983898
// expectTypeOf(userQuery.error).toEqualTypeOf<Error>();
38993899
// }
39003900
});
3901+
3902+
it('bug #64 (options is not reactive sometimes)', () => {
3903+
const isEnabled = observable.box(false);
3904+
let query: Maybe<Query>;
3905+
3906+
const queryFn = vi.fn(() => Promise.resolve({ status: 'success' }));
3907+
3908+
runInAction(() => {
3909+
query = createQuery(queryFn, {
3910+
enableOnDemand: true,
3911+
options: () => ({
3912+
enabled: isEnabled.get(),
3913+
}),
3914+
});
3915+
3916+
isEnabled.set(true);
3917+
});
3918+
3919+
query?.result.data; // this won't trigger fetching, which is unexpected
3920+
3921+
expect(queryFn).toBeCalledTimes(1);
3922+
});
39013923
});

src/query.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ export class Query<
462462
reaction(getAllDynamicOptions, this.update, {
463463
delay: this.features.dynamicOptionsUpdateDelay,
464464
signal: config.abortSignal,
465+
fireImmediately: true,
465466
equals: this.features.dynamicOptionsComparer,
466467
});
467468
}

0 commit comments

Comments
 (0)