Skip to content

Commit b64216b

Browse files
committed
feat(useRequest): add reload function to clear the queries list
1 parent ce71b43 commit b64216b

File tree

2 files changed

+162
-4
lines changed

2 files changed

+162
-4
lines changed

Diff for: src/__tests__/index.test.tsx

+140
Original file line numberDiff line numberDiff line change
@@ -2243,4 +2243,144 @@ describe('useRequest', () => {
22432243
expect(wrapperB.find('#D').text()).toBe('false');
22442244
expect(wrapperB.find('#E').text()).toBe('false');
22452245
});
2246+
2247+
test('reload should work: case 1', async () => {
2248+
const wrapper = shallowMount(
2249+
defineComponent({
2250+
setup() {
2251+
const { run, reload, data } = useRequest(request, {
2252+
defaultParams: ['hello'],
2253+
});
2254+
return () => (
2255+
<div>
2256+
<button class="run" onClick={() => run('hi there')}></button>
2257+
<button class="reload" onClick={() => reload()}></button>
2258+
<div class="data">{data.value}</div>
2259+
</div>
2260+
);
2261+
},
2262+
}),
2263+
);
2264+
2265+
const dataEl = wrapper.find('.data');
2266+
const runEl = wrapper.find('.run');
2267+
const reloadEl = wrapper.find('.reload');
2268+
2269+
await waitForTime(1000);
2270+
expect(dataEl.text()).toBe('hello');
2271+
2272+
await runEl.trigger('click');
2273+
await waitForTime(1000);
2274+
expect(dataEl.text()).toEqual('hi there');
2275+
2276+
await reloadEl.trigger('click');
2277+
await waitForTime(1000);
2278+
expect(dataEl.text()).toEqual('hello');
2279+
2280+
await runEl.trigger('click');
2281+
await waitForTime(1000);
2282+
expect(dataEl.text()).toEqual('hi there');
2283+
});
2284+
2285+
test('reload should work: case 2', async () => {
2286+
const users = [
2287+
{ id: '1', username: 'A' },
2288+
{ id: '2', username: 'B' },
2289+
{ id: '3', username: 'C' },
2290+
];
2291+
2292+
const wrapper = shallowMount(
2293+
defineComponent({
2294+
setup() {
2295+
const { run, queries, data, loading, reload } = useRequest(request, {
2296+
manual: true,
2297+
refreshOnWindowFocus: true,
2298+
queryKey: id => id,
2299+
});
2300+
2301+
return () => (
2302+
<div>
2303+
<div id="data">{data.value}</div>
2304+
<div id="loading">{loading.value.toString()}</div>
2305+
<div id="reload" onClick={() => reload()} />
2306+
<ul>
2307+
{users.map(item => (
2308+
<li
2309+
key={item.id}
2310+
id={item.username}
2311+
onClick={() => run(item.id)}
2312+
>
2313+
{queries[item.id]?.loading
2314+
? 'loading'
2315+
: queries[item.id]?.data}
2316+
</li>
2317+
))}
2318+
</ul>
2319+
</div>
2320+
);
2321+
},
2322+
}),
2323+
);
2324+
2325+
const dataEl = wrapper.find('#data');
2326+
const loadingEl = wrapper.find('#loading');
2327+
const reloadEl = wrapper.find('#reload');
2328+
2329+
expect(FOCUS_LISTENER.size).toBe(1);
2330+
expect(VISIBLE_LISTENER.size).toBe(2);
2331+
expect(RECONNECT_LISTENER.size).toBe(1);
2332+
2333+
for (let i = 0; i < users.length; i++) {
2334+
const userName = users[i].username;
2335+
const currentId = users[i].id;
2336+
2337+
await wrapper.find(`#${userName}`).trigger('click');
2338+
expect(wrapper.find(`#${userName}`).text()).toBe('loading');
2339+
2340+
expect(dataEl.text()).toBe('');
2341+
expect(loadingEl.text()).toBe('true');
2342+
2343+
await waitForTime(1000);
2344+
expect(wrapper.find(`#${userName}`).text()).toBe(currentId);
2345+
2346+
expect(dataEl.text()).toBe(currentId);
2347+
expect(loadingEl.text()).toBe('false');
2348+
}
2349+
2350+
expect(FOCUS_LISTENER.size).toBe(4);
2351+
expect(VISIBLE_LISTENER.size).toBe(8);
2352+
expect(RECONNECT_LISTENER.size).toBe(4);
2353+
2354+
await reloadEl.trigger('click');
2355+
for (let i = 0; i < users.length; i++) {
2356+
const userName = users[i].username;
2357+
expect(wrapper.find(`#${userName}`).text()).toBe('');
2358+
expect(dataEl.text()).toBe('');
2359+
}
2360+
2361+
expect(FOCUS_LISTENER.size).toBe(1);
2362+
expect(VISIBLE_LISTENER.size).toBe(2);
2363+
expect(RECONNECT_LISTENER.size).toBe(1);
2364+
2365+
for (let i = 0; i < users.length; i++) {
2366+
const userName = users[i].username;
2367+
const currentId = users[i].id;
2368+
2369+
await wrapper.find(`#${userName}`).trigger('click');
2370+
expect(wrapper.find(`#${userName}`).text()).toBe('loading');
2371+
2372+
expect(dataEl.text()).toBe('');
2373+
expect(loadingEl.text()).toBe('true');
2374+
2375+
await waitForTime(1000);
2376+
expect(wrapper.find(`#${userName}`).text()).toBe(currentId);
2377+
2378+
expect(dataEl.text()).toBe(currentId);
2379+
expect(loadingEl.text()).toBe('false');
2380+
}
2381+
2382+
expect(FOCUS_LISTENER.size).toBe(4);
2383+
expect(VISIBLE_LISTENER.size).toBe(8);
2384+
expect(RECONNECT_LISTENER.size).toBe(4);
2385+
});
22462386
});

Diff for: src/useRequest.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,42 @@ import useAsyncQuery, { BaseResult } from './core/useAsyncQuery';
33
import generateService from './core/utils/generateService';
44
import { IService } from './core/utils/types';
55

6+
export interface RequestResult<R, P extends unknown[]>
7+
extends Omit<BaseResult<R, P>, 'reset'> {
8+
reload: () => void;
9+
}
610
function useRequest<R, P extends unknown[] = any>(
711
service: IService<R, P>,
8-
): BaseResult<R, P>;
12+
): RequestResult<R, P>;
913
function useRequest<R, P extends unknown[] = any, FR = any>(
1014
service: IService<R, P>,
1115
options: FormatOptions<R, P, FR>,
12-
): BaseResult<FR, P>;
16+
): RequestResult<FR, P>;
1317
function useRequest<R, P extends unknown[] = any>(
1418
service: IService<R, P>,
1519
options: BaseOptions<R, P>,
16-
): BaseResult<R, P>;
20+
): RequestResult<R, P>;
1721
function useRequest<R, P extends unknown[], FR>(
1822
service: IService<R, P>,
1923
options?: MixinOptions<R, P, FR>,
2024
) {
2125
const promiseQuery = generateService(service);
26+
const { reset, run, ...rest } = useAsyncQuery<R, P, FR>(
27+
promiseQuery,
28+
(options ?? {}) as any,
29+
);
30+
31+
const reload = () => {
32+
const { defaultParams = ([] as unknown) as P, manual } = options!;
33+
reset();
34+
!manual && run(...defaultParams);
35+
};
2236

23-
return useAsyncQuery<R, P, FR>(promiseQuery, (options ?? {}) as any);
37+
return {
38+
reload,
39+
run,
40+
...rest,
41+
};
2442
}
2543

2644
export default useRequest;

0 commit comments

Comments
 (0)