1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| import { ref, unref, watch } from 'vue';
|
| import type { UseRequestPlugin } from '../types';
|
| // support refreshDeps & ready
| const useAutoRunPlugin: UseRequestPlugin<any, any[]> = (
| fetchInstance,
| { manual, ready = true, defaultParams = [], refreshDeps = [], refreshDepsAction },
| ) => {
| const hasAutoRun = ref(false);
|
| watch(
| () => unref(ready),
| (readyVal) => {
| if (!unref(manual) && readyVal) {
| hasAutoRun.value = true;
| fetchInstance.run(...defaultParams);
| }
| },
| );
|
| if (refreshDeps.length) {
| watch(refreshDeps, () => {
| if (hasAutoRun.value) {
| return;
| }
| if (!manual) {
| if (refreshDepsAction) {
| refreshDepsAction();
| } else {
| fetchInstance.refresh();
| }
| }
| });
| }
|
| return {
| onBefore: () => {
| if (!unref(ready)) {
| return { stopNow: true };
| }
| },
| };
| };
|
| useAutoRunPlugin.onInit = ({ ready = true, manual }) => {
| return {
| loading: !unref(manual) && unref(ready),
| };
| };
|
| export default useAutoRunPlugin;
|
|