Skip to content

Commit abb41d6

Browse files
committed
feat: add start() method
1 parent 7bf6946 commit abb41d6

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ const query = new MobxQuery({
9393
});
9494
```
9595

96+
#### method `start(params)`
97+
98+
Enable query if it is disabled then fetch the query.
99+
96100
#### method `update()`
97101

98102
Update options for query (Uses [QueryObserver](https://tanstack.com/query/latest/docs/reference/QueriesObserver).setOptions)

src/mobx-query.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@tanstack/query-core';
1010
import { LinkedAbortController } from 'linked-abort-controller';
1111
import { observable, reaction, runInAction, when } from 'mobx';
12-
import { describe, expect, it, vi } from 'vitest';
12+
import { describe, expect, it, test, vi } from 'vitest';
1313
import { waitAsync } from 'yammies/async';
1414

1515
import { MobxQuery } from './mobx-query';
@@ -536,6 +536,26 @@ describe('MobxQuery', () => {
536536
});
537537
});
538538

539+
describe('"start" method', () => {
540+
test('should call once queryFn', async () => {
541+
const querySpyFn = vi.fn();
542+
const mobxQuery = new MobxQueryMock({
543+
queryKey: ['test'],
544+
queryFn: querySpyFn,
545+
enabled: false,
546+
});
547+
548+
await mobxQuery.start();
549+
550+
await when(() => !mobxQuery._rawResult.isLoading);
551+
552+
expect(mobxQuery.result.isFetched).toBeTruthy();
553+
expect(querySpyFn).toBeCalledTimes(1);
554+
555+
mobxQuery.dispose();
556+
});
557+
});
558+
539559
describe('scenarios', () => {
540560
it('query with refetchInterval(number) should be stopped after inner abort', async () => {
541561
const query = new MobxQueryMock({

src/mobx-query.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
MobxQueryInvalidateParams,
2626
MobxQueryOptions,
2727
MobxQueryResetParams,
28+
MobxQueryStartParams,
2829
MobxQueryUpdateOptions,
2930
} from './mobx-query.types';
3031

@@ -313,6 +314,15 @@ export class MobxQuery<
313314
this.abortController.abort();
314315
}
315316

317+
async start(params?: MobxQueryStartParams<TData, TError, TQueryKey>) {
318+
const options: MobxQueryUpdateOptions<TData, TError, TQueryKey> = {
319+
enabled: true,
320+
...params,
321+
};
322+
this.update(options);
323+
return await this.refetch();
324+
}
325+
316326
/**
317327
* @deprecated use `destroy`
318328
*/

src/mobx-query.types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ export interface MobxQueryConfig<
120120
) => MobxQueryDynamicOptions<TData, TError, TQueryKey>;
121121
}
122122

123+
export interface MobxQueryStartParams<
124+
TData,
125+
TError = DefaultError,
126+
TQueryKey extends QueryKey = QueryKey,
127+
> extends Partial<
128+
Omit<MobxQueryUpdateOptions<TData, TError, TQueryKey>, 'enabled'>
129+
> {}
130+
123131
export type InferQuery<
124132
T extends MobxQueryConfig<any, any, any> | MobxQuery<any, any, any>,
125133
TInferValue extends 'data' | 'key' | 'error' | 'query' | 'config',

0 commit comments

Comments
 (0)