1
- import {
2
- ref ,
3
- reactive ,
4
- toRefs ,
5
- onUnmounted ,
6
- inject ,
7
- UnwrapRef ,
8
- watchEffect ,
9
- computed ,
10
- isRef ,
11
- onMounted ,
12
- unref ,
13
- } from 'vue'
1
+ import { ref , reactive , toRefs , onUnmounted , inject , UnwrapRef , watchEffect , computed , isRef , onMounted , unref } from 'vue'
14
2
15
3
import Fetch from './Fetch'
16
4
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
@@ -29,18 +17,11 @@ function isUseRequestFetchState<TData, TParams extends any[]>(
29
17
return keys . filter ( i => [ 'data' , 'loading' , 'params' , 'error' ] . includes ( i ) ) . length === 4
30
18
}
31
19
32
- // function isUseRequestFetchStateKey<TData, TParams extends any[]>(
33
- // field: string,
34
- // state: unknown,
35
- // ): state is UseRequestFetchState<TData, TParams>[keyof UseRequestFetchState<TData, TParams>] {
36
- // return Boolean(['data', 'loading', 'params', 'error'].find(i => i === field))
37
- // }
38
-
39
20
function useRequestImplement < TData , TParams extends any [ ] > (
40
21
service : UseRequestService < TData , TParams > ,
41
22
options : UseRequestOptions < TData , TParams , any > = { } ,
42
23
plugins : UseRequestPlugin < TData , TParams > [ ] = [ ] ,
43
- ) : useRequestResult < TData , TParams > {
24
+ ) {
44
25
// global option
45
26
const USEREQUEST_GLOBAL_OPTIONS = inject < Record < string , any > > (
46
27
USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY ,
@@ -69,11 +50,8 @@ function useRequestImplement<TData, TParams extends any[]>(
69
50
error : undefined ,
70
51
} )
71
52
72
- const setState = ( currentState : unknown , field ?: keyof typeof state ) : void => {
53
+ const setState = ( currentState : unknown , field ?: keyof typeof state ) => {
73
54
if ( field ) {
74
- // if (isUseRequestFetchStateKey<UnwrapRef<TData>, UnwrapRef<TParams>>(field, currentState)) {
75
- // state[field] = currentState as any
76
- // }
77
55
state [ field ] = currentState as any
78
56
} else {
79
57
if ( isUseRequestFetchState < UnwrapRef < TData > , UnwrapRef < TParams > > ( currentState ) ) {
@@ -101,20 +79,12 @@ function useRequestImplement<TData, TParams extends any[]>(
101
79
return p ( fetchInstance , fetchOptions )
102
80
} )
103
81
104
- const readyComputed = computed ( ( ) => ( isRef ( ready ) ? ready . value : ready ) )
105
-
106
- // const isMount = ref(false)
82
+ const readyComputed = computed ( ( ) => isRef ( ready ) ? ready . value : ready )
107
83
108
84
watchEffect ( ( ) => {
109
85
if ( ! manual ) {
110
86
const params = fetchInstance . state . params || options . defaultParams || [ ]
111
- // if (readyComputed.value && !isMount.value) {
112
- // fetchInstance.run(...(params as TParams))
113
- // // 模拟首次mount
114
- // isMount.value = true
115
-
116
- // }
117
- // 自动收集依赖
87
+ // auto collect
118
88
if ( readyComputed . value && fetchInstance . options . refreshDeps === true && ! ! serviceRef . value ) {
119
89
fetchInstance . run ( ...( params as TParams ) )
120
90
}
0 commit comments