Skip to content

Commit ceb4302

Browse files
committed
feat: enhanced refreshDeps, now can be used like watch #166
1 parent fd6814c commit ceb4302

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

src/__tests__/index.test.tsx

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ describe('useRequest', () => {
774774
expect(wrapper.data).toBe('3');
775775
});
776776

777-
test('refreshDeps should work', async () => {
777+
test('refreshDeps should work: case 1', async () => {
778778
const wrapper = mount(
779779
defineComponent({
780780
template: '<div/>',
@@ -824,6 +824,76 @@ describe('useRequest', () => {
824824
}
825825
});
826826

827+
test('refreshDeps should work: case 2', async () => {
828+
const wrapper = mount(
829+
defineComponent({
830+
template: '<div/>',
831+
setup() {
832+
const refreshReactive = reactive({
833+
count: 0,
834+
});
835+
const { loading } = useRequest(request, {
836+
refreshDeps: () => refreshReactive.count,
837+
});
838+
839+
const handleUpdateReactive = () => {
840+
refreshReactive.count++;
841+
};
842+
843+
return {
844+
loading,
845+
handleUpdateReactive,
846+
};
847+
},
848+
}),
849+
);
850+
expect(wrapper.loading).toBe(true);
851+
await waitForTime(1000);
852+
expect(wrapper.loading).toBe(false);
853+
854+
for (let index = 0; index < 100; index++) {
855+
wrapper.handleUpdateReactive();
856+
await waitForTime(1);
857+
expect(wrapper.loading).toBe(true);
858+
await waitForTime(1000);
859+
expect(wrapper.loading).toBe(false);
860+
}
861+
});
862+
863+
test('refreshDeps should work: case 3', async () => {
864+
const wrapper = mount(
865+
defineComponent({
866+
template: '<div/>',
867+
setup() {
868+
const refreshRef = ref(0);
869+
const { loading } = useRequest(request, {
870+
refreshDeps: refreshRef,
871+
});
872+
873+
const handleUpdateRef = () => {
874+
refreshRef.value++;
875+
};
876+
877+
return {
878+
loading,
879+
handleUpdateRef,
880+
};
881+
},
882+
}),
883+
);
884+
expect(wrapper.loading).toBe(true);
885+
await waitForTime(1000);
886+
expect(wrapper.loading).toBe(false);
887+
888+
for (let index = 0; index < 100; index++) {
889+
wrapper.handleUpdateRef();
890+
await waitForTime(1);
891+
expect(wrapper.loading).toBe(true);
892+
await waitForTime(1000);
893+
expect(wrapper.loading).toBe(false);
894+
}
895+
});
896+
827897
test('manual = true, refreshDeps should work', async () => {
828898
const wrapper = mount(
829899
defineComponent({
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { watch } from 'vue-demi';
22

33
import { definePlugin } from '../definePlugin';
4+
import { isArray } from '../utils';
45

56
export default definePlugin(
6-
(queryInstance, { refreshDeps = [], refreshDepsAction, manual }) => {
7+
(queryInstance, { refreshDeps, refreshDepsAction, manual }) => {
8+
if (
9+
refreshDeps === undefined ||
10+
(isArray(refreshDeps) && refreshDeps.length === 0)
11+
)
12+
return {};
13+
const deps = isArray(refreshDeps) ? refreshDeps : [refreshDeps];
14+
715
// watch refreshDeps
8-
if (refreshDeps?.length) {
9-
watch(refreshDeps, () => {
10-
if (refreshDepsAction) {
11-
refreshDepsAction();
12-
} else {
13-
!manual && queryInstance.context.refresh();
14-
}
15-
});
16-
}
16+
watch(deps, () => {
17+
if (refreshDepsAction) {
18+
refreshDepsAction();
19+
} else {
20+
!manual && queryInstance.context.refresh();
21+
}
22+
});
1723
return {};
1824
},
1925
);

src/core/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export type Options<R, P extends unknown[]> = BaseOptions & {
7575
defaultParams?: P;
7676
ready?: Ref<boolean>;
7777
initialData?: R;
78-
refreshDeps?: WatchSource<any>[];
78+
refreshDeps?: WatchSource | WatchSource[];
7979
cacheKey?: string | ((params?: P) => string);
8080
refreshDepsAction?: () => void;
8181
onSuccess?: (data: R, params: P) => void;
@@ -91,9 +91,7 @@ export type PluginImplementType<R, P extends any[]> = {
9191
};
9292

9393
export type PluginType<R, P extends unknown[]> = {
94-
onBefore: (
95-
params: P,
96-
) => {
94+
onBefore: (params: P) => {
9795
isBreak?: Boolean;
9896
breakResult?: any;
9997
} | void;

0 commit comments

Comments
 (0)