Skip to content

Commit dc67c60

Browse files
authored
Initial data staletime (#55)
* Fix commented test * Query should not run on mount with initial data and non zero stale time
1 parent 258752e commit dc67c60

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

Diff for: packages/mst-query/src/MstQueryHandler.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ export class QueryObserver {
104104
if (this.isQuery) {
105105
options.isMounted = this.isMounted;
106106

107-
this.query.__MstQueryHandler.queryWhenChanged(options);
108-
}
107+
if (!this.isMounted && !this.query.__MstQueryHandler.isFetched && options.initialData) {
108+
this.query.__MstQueryHandler.hydrate(options);
109+
}
109110

110-
if (!this.isMounted && !this.query.__MstQueryHandler.isFetched && options.initialData) {
111-
this.query.__MstQueryHandler.setData(options.initialData);
111+
this.query.__MstQueryHandler.queryWhenChanged(options);
112112
}
113113

114114
if (!this.isMounted) {
@@ -134,7 +134,7 @@ export function onMutate<T extends Instance<MutationReturnType>>(
134134
unprotect(root);
135135
callback(data, self);
136136
protect(root);
137-
}
137+
},
138138
});
139139
}
140140

@@ -148,7 +148,7 @@ export function onQueryMore<T extends Instance<QueryReturnType>>(
148148
unprotect(root);
149149
callback(data, self);
150150
protect(root);
151-
}
151+
},
152152
});
153153
}
154154

@@ -193,6 +193,7 @@ export class MstQueryHandler {
193193
isFetchingMore: observable,
194194
isFetched: observable,
195195
error: observable,
196+
hydrate: action.bound,
196197
setData: action.bound,
197198
setResult: action.bound,
198199
setError: action.bound,
@@ -254,17 +255,19 @@ export class MstQueryHandler {
254255
}
255256

256257
if (!options.isMounted) {
257-
const notInitialized = !this.isFetched && !this.isLoading;
258+
const notInitialized = !this.isFetched && !this.isLoading && !this.model.data;
258259
if (notInitialized) {
259260
return this.model.query(options);
260261
}
261262

262263
const now = new Date();
263264
const cachedAt = this.cachedAt?.getTime() ?? now.getTime();
264265
const isStale = now.getTime() - cachedAt >= (options.staleTime ?? 0);
265-
if (isStale) {
266-
return this.model.query(options);
266+
if (!isStale) {
267+
return;
267268
}
269+
270+
return this.model.query(options);
268271
}
269272

270273
if (!options.isRequestEqual) {
@@ -512,6 +515,17 @@ export class MstQueryHandler {
512515
return this.model.data;
513516
}
514517

518+
hydrate(options: any) {
519+
const { initialData, request, pagination } = options;
520+
521+
this.setVariables({ request, pagination });
522+
523+
this.isLoading = false;
524+
525+
this.setData(initialData);
526+
this.cachedAt = new Date();
527+
}
528+
515529
onAfterCreate() {
516530
this.queryClient.queryStore.setQuery(this.model);
517531
}

Diff for: packages/mst-query/tests/mstQuery.test.tsx

+42-10
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ test('useMutation', async () => {
182182

183183
const button = await findByTestId('add');
184184
fireEvent.click(button);
185-
// expect(q.listQuery.data?.items[4].id).toBe('temp');
186-
// expect(q.listQuery.data?.items.length).toBe(5);
187-
// await wait(0);
188-
// expect(q.listQuery.data?.items[4].id!).toBe('add-test');
189-
// expect(q.listQuery.data?.items.length).toBe(5);
185+
expect(q.listQuery.data?.items[4].id).toBe('temp');
186+
expect(q.listQuery.data?.items.length).toBe(5);
187+
await wait(0);
188+
expect(q.listQuery.data?.items[4].id!).toBe('add-test');
189+
expect(q.listQuery.data?.items.length).toBe(5);
190190

191-
// expect(loadingStates).toStrictEqual([false, true, false]);
191+
expect(loadingStates).toStrictEqual([false, true, false]);
192192

193193
sub();
194194
});
@@ -711,7 +711,7 @@ test('subscription query', async () => {
711711

712712
test('volatile query', async () => {
713713
const { render } = setup();
714-
714+
715715
const text = 'testing';
716716

717717
let renders = 0;
@@ -821,7 +821,7 @@ test('safeReference', async () => {
821821

822822
expect(q.safeReferenceQuery.data?.items.length).toBe(4);
823823

824-
q.removeItemMutation.mutate({ request: { id: 'test' }});
824+
q.removeItemMutation.mutate({ request: { id: 'test' } });
825825

826826
await wait(0);
827827

@@ -847,12 +847,44 @@ test('change query in useQuery', async () => {
847847

848848
render(<Comp />);
849849
await wait(0);
850-
850+
851851
query.set(q.itemQuery2);
852852
await wait(10);
853-
853+
854854
expect(query.get().data).not.toBe(null);
855855

856856
configureMobx({ enforceActions: 'observed' });
857857
});
858858

859+
test('useQuery should not run when initialData is passed and staleTime is larger than 0', async () => {
860+
const { render, q } = setup();
861+
862+
configureMobx({ enforceActions: 'never' });
863+
864+
let id = observable.box('test');
865+
const initialData = await api.getItem({ request: { id: id.get() } });
866+
867+
const loadingStates: boolean[] = [];
868+
const Comp = observer(() => {
869+
const { query, isLoading } = useQuery(q.itemQuery, {
870+
initialData,
871+
request: { id: id.get() },
872+
staleTime: 10,
873+
});
874+
loadingStates.push(isLoading);
875+
return <div></div>;
876+
});
877+
render(<Comp />);
878+
879+
await wait(0);
880+
881+
expect(loadingStates).toEqual([false, false]);
882+
expect(q.itemQuery.data?.id).toBe('test');
883+
884+
id.set('different-test');
885+
await wait(0);
886+
expect(q.itemQuery.data?.id).toBe('different-test');
887+
expect(q.itemQuery.variables.request?.id).toBe('different-test');
888+
889+
configureMobx({ enforceActions: 'observed' });
890+
});

0 commit comments

Comments
 (0)