Skip to content

Commit 135e76f

Browse files
committed
feat: add onBefore and onAfter hooks #42
1 parent 5034f2c commit 135e76f

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/__tests__/index.test.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -2463,4 +2463,35 @@ describe('useRequest', () => {
24632463
expect(VISIBLE_LISTENER.size).toBe(8);
24642464
expect(RECONNECT_LISTENER.size).toBe(4);
24652465
});
2466+
2467+
test('onBefore and onAfter hooks can use', async () => {
2468+
const onBefore = jest.fn();
2469+
const onAfter = jest.fn();
2470+
2471+
const wrapper = shallowMount(
2472+
defineComponent({
2473+
setup() {
2474+
const { data, run } = useRequest(request, {
2475+
onBefore,
2476+
onAfter,
2477+
});
2478+
2479+
return () => (
2480+
<button onClick={() => run()}>{`data:${data.value}`}</button>
2481+
);
2482+
},
2483+
}),
2484+
);
2485+
expect(onBefore).toHaveBeenCalledTimes(1);
2486+
expect(onAfter).toHaveBeenCalledTimes(0);
2487+
await waitForTime(100);
2488+
expect(onBefore).toHaveBeenCalledTimes(1);
2489+
expect(onAfter).toHaveBeenCalledTimes(0);
2490+
await waitForTime(800);
2491+
expect(onBefore).toHaveBeenCalledTimes(1);
2492+
expect(onAfter).toHaveBeenCalledTimes(0);
2493+
await waitForTime(100);
2494+
expect(onBefore).toHaveBeenCalledTimes(1);
2495+
expect(onAfter).toHaveBeenCalledTimes(1);
2496+
});
24662497
});

src/__tests__/load-more.test.tsx

+41
Original file line numberDiff line numberDiff line change
@@ -827,4 +827,45 @@ describe('useLoadMore', () => {
827827
expect(wrapper.find('#D').text()).toBe('1');
828828
expect(wrapper.find('#E').text()).toBe('5');
829829
});
830+
831+
test('onBefore and onAfter hooks can use in `useLoadMore`', async () => {
832+
const onBefore = jest.fn();
833+
const onAfter = jest.fn();
834+
835+
const wrapper = shallowMount(
836+
defineComponent({
837+
setup() {
838+
const { loadMore } = useLoadMore(normalRequest, {
839+
onBefore,
840+
onAfter,
841+
});
842+
return () => (
843+
<div>
844+
<div
845+
class="loadMore"
846+
onClick={() => {
847+
loadMore();
848+
}}
849+
/>
850+
</div>
851+
);
852+
},
853+
}),
854+
);
855+
856+
const loadMoreEl = wrapper.find('.loadMore');
857+
858+
expect(onBefore).toHaveBeenCalledTimes(1);
859+
expect(onAfter).toHaveBeenCalledTimes(0);
860+
await waitForTime(1000);
861+
expect(onBefore).toHaveBeenCalledTimes(1);
862+
expect(onAfter).toHaveBeenCalledTimes(1);
863+
864+
await loadMoreEl.trigger('click');
865+
expect(onBefore).toHaveBeenCalledTimes(2);
866+
expect(onAfter).toHaveBeenCalledTimes(1);
867+
await waitForTime(1000);
868+
expect(onBefore).toHaveBeenCalledTimes(2);
869+
expect(onAfter).toHaveBeenCalledTimes(2);
870+
});
830871
});

src/__tests__/pagination.test.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,43 @@ describe('usePagination', () => {
625625
expect(wrapper.find('#D').text()).toBe('1');
626626
expect(wrapper.find('#E').text()).toBe('5');
627627
});
628+
629+
test('onBefore and onAfter hooks can use in `usePagination`', async () => {
630+
const onBefore = jest.fn();
631+
const onAfter = jest.fn();
632+
633+
const wrapper = shallowMount(
634+
defineComponent({
635+
setup() {
636+
const { changeCurrent } = usePagination<NormalMockDataType>(
637+
normalApi,
638+
{
639+
onBefore,
640+
onAfter,
641+
},
642+
);
643+
return () => (
644+
<div>
645+
<button class="button" onClick={() => changeCurrent(1)} />
646+
</div>
647+
);
648+
},
649+
}),
650+
);
651+
652+
const buttonEl = wrapper.find('.button');
653+
654+
expect(onBefore).toHaveBeenCalledTimes(1);
655+
expect(onAfter).toHaveBeenCalledTimes(0);
656+
await waitForTime(1000);
657+
expect(onBefore).toHaveBeenCalledTimes(1);
658+
expect(onAfter).toHaveBeenCalledTimes(1);
659+
660+
await buttonEl.trigger('click');
661+
expect(onBefore).toHaveBeenCalledTimes(2);
662+
expect(onAfter).toHaveBeenCalledTimes(1);
663+
await waitForTime(1000);
664+
expect(onBefore).toHaveBeenCalledTimes(2);
665+
expect(onAfter).toHaveBeenCalledTimes(2);
666+
});
628667
});

src/core/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export type BaseOptions<R, P extends unknown[]> = GlobalOptions & {
5656
queryKey?: (...args: P) => string;
5757
onSuccess?: (data: R, params: P) => void;
5858
onError?: (error: Error, params: P) => void;
59+
onBefore?: (params: P) => void;
60+
onAfter?: (params: P) => void;
5961
};
6062

6163
const FRPlaceholderType = Symbol('FR');

src/core/createQuery.ts

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const createQuery = <R, P extends unknown[]>(
7878
formatResult,
7979
onSuccess,
8080
onError,
81+
onBefore,
82+
onAfter,
8183
} = config;
8284

8385
const retriedCount = ref(0);
@@ -193,6 +195,9 @@ const createQuery = <R, P extends unknown[]>(
193195
count.value += 1;
194196
const currentCount = count.value;
195197

198+
// onBefore hooks
199+
onBefore?.(args);
200+
196201
return query(...args)
197202
.then(res => {
198203
if (currentCount === count.value) {
@@ -238,6 +243,9 @@ const createQuery = <R, P extends unknown[]>(
238243

239244
// run for polling
240245
pollingTimer.value = polling(() => _run(...args));
246+
247+
// onAfter hooks
248+
onAfter?.(args);
241249
}
242250
});
243251
};

src/core/useAsyncQuery.ts

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
8484
formatResult,
8585
onSuccess,
8686
onError,
87+
onBefore,
88+
onAfter,
8789
} = { ...getGlobalOptions(), ...injectedGlobalOptions, ...options };
8890

8991
const stopPollingWhenHiddenOrOffline = ref(false);
@@ -134,6 +136,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
134136
formatResult,
135137
onSuccess,
136138
onError,
139+
onBefore,
140+
onAfter,
137141
} as Config<R, P>;
138142

139143
const loading = ref(false);

0 commit comments

Comments
 (0)