@@ -5,140 +5,134 @@ import { CachedData } from '../utils/cache'
5
5
import * as cachePromise from '../utils/cachePromise'
6
6
import * as cacheSubscribe from '../utils/cacheSubscribe'
7
7
8
- const useCachePlugin : Plugin < any , any [ ] > = (
9
- fetchInstance ,
10
- {
11
- cacheKey,
12
- cacheTime = 5 * 60 * 1000 ,
13
- staleTime = 0 ,
14
- setCache : customSetCache ,
15
- getCache : customGetCache ,
16
- }
8
+ const useCachePlugin : Plugin < unknown , unknown [ ] > = (
9
+ fetchInstance ,
10
+ {
11
+ cacheKey,
12
+ cacheTime = 5 * 60 * 1000 ,
13
+ staleTime = 0 ,
14
+ setCache : customSetCache ,
15
+ getCache : customGetCache ,
16
+ } ,
17
17
) => {
18
- const unSubscribeRef = ref < ( ) => void > ( )
19
-
20
- const currentPromiseRef = ref < Promise < any > > ( )
21
-
22
- const _setCache = ( key : string , cachedData : CachedData ) => {
23
- if ( customSetCache ) {
24
- customSetCache ( cachedData )
25
- } else {
26
- cache . setCache ( key , cacheTime , cachedData )
27
- }
28
- cacheSubscribe . trigger ( key , cachedData . data )
29
- }
30
-
31
- const _getCache = ( key : string , params : any [ ] = [ ] ) => {
32
- if ( customGetCache ) {
33
- return customGetCache ( params )
34
- }
35
- return cache . getCache ( key )
36
- }
37
-
38
- watchEffect ( ( ) => {
39
- if ( ! cacheKey ) {
40
- return
41
- }
42
-
43
- // 获取初始化的data
44
- const cacheData = _getCache ( cacheKey )
45
- if ( cacheData && Object . hasOwnProperty . call ( cacheData , 'data' ) ) {
46
- fetchInstance . state . data = cacheData . data
47
- fetchInstance . state . params = cacheData . params
48
-
49
- console . log ( 'staleTime' , staleTime )
50
-
51
- if (
52
- staleTime === - 1 ||
53
- new Date ( ) . getTime ( ) - cacheData . time <= staleTime
54
- ) {
55
- fetchInstance . state . loading = false
56
- }
57
- }
58
-
59
- // 如果存在相同的cacheKey,触发更新
60
- unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , ( data ) => {
61
- fetchInstance . setState ( { data } )
62
- } )
63
- } )
64
-
65
- onUnmounted ( ( ) => {
66
- unSubscribeRef . value ?.( )
67
- } )
68
-
69
- if ( ! cacheKey ) {
70
- return { }
71
- }
72
-
73
- return {
74
- onBefore : ( params ) => {
75
- const cacheData = _getCache ( cacheKey , params )
76
-
77
- if ( ! cacheData || ! Object . hasOwnProperty . call ( cacheData , 'data' ) ) {
78
- return { }
79
- }
80
- // 数据是新鲜就停止请求
81
- if (
82
- staleTime === - 1 ||
83
- new Date ( ) . getTime ( ) - cacheData . time <= staleTime
84
- ) {
85
- console . log ( '停止请求' )
86
-
87
- return {
88
- loading : false ,
89
- data : cacheData ?. data ,
90
- returnNow : true ,
91
- }
92
- } else {
93
- // 数据不新鲜,则返回data,并且继续发送请求
94
- return {
95
- data : cacheData ?. data ,
96
- }
97
- }
98
- } ,
99
- onRequest : ( service , args ) => {
100
- let servicePromise = cachePromise . getCachePromise ( cacheKey )
101
- // 如果存在servicePromise,并且它没有被触发,则使用它
102
- if ( servicePromise && servicePromise !== currentPromiseRef . value ) {
103
- return { servicePromise }
104
- }
105
-
106
- servicePromise = service ( ...args )
107
- currentPromiseRef . value = servicePromise
108
- cachePromise . setCachePromise ( cacheKey , servicePromise )
109
- return { servicePromise }
110
- } ,
111
- onSuccess : ( data , params ) => {
112
- if ( cacheKey ) {
113
- // 取消更新,避免反复触发自己
114
- unSubscribeRef . value ?.( )
115
- _setCache ( cacheKey , {
116
- data,
117
- params,
118
- time : new Date ( ) . getTime ( ) ,
119
- } )
120
- // 触发器更新
121
- unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , ( d ) => {
122
- fetchInstance . setState ( { data : d } )
123
- } )
124
- }
125
- } ,
126
- onMutate : ( data ) => {
127
- if ( cacheKey ) {
128
- // 取消更新,避免反复触发自己
129
- unSubscribeRef . value ?.( )
130
- _setCache ( cacheKey , {
131
- data,
132
- params : fetchInstance . state . params ,
133
- time : new Date ( ) . getTime ( ) ,
134
- } )
135
- // 触发器更新
136
- unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , ( d ) => {
137
- fetchInstance . setState ( { data : d } )
138
- } )
139
- }
140
- } ,
141
- }
18
+ const unSubscribeRef = ref < ( ) => void > ( )
19
+
20
+ const currentPromiseRef = ref < Promise < any > > ( )
21
+
22
+ const _setCache = ( key : string , cachedData : CachedData ) => {
23
+ if ( customSetCache ) {
24
+ customSetCache ( cachedData )
25
+ } else {
26
+ cache . setCache ( key , cacheTime , cachedData )
27
+ }
28
+ cacheSubscribe . trigger ( key , cachedData . data )
29
+ }
30
+
31
+ const _getCache = ( key : string , params : any [ ] = [ ] ) => {
32
+ if ( customGetCache ) {
33
+ return customGetCache ( params )
34
+ }
35
+ return cache . getCache ( key )
36
+ }
37
+
38
+ watchEffect ( ( ) => {
39
+ if ( ! cacheKey ) {
40
+ return
41
+ }
42
+
43
+ // 获取初始化的data
44
+ const cacheData = _getCache ( cacheKey )
45
+ if ( cacheData && Object . hasOwnProperty . call ( cacheData , 'data' ) ) {
46
+ fetchInstance . state . data = cacheData . data
47
+ fetchInstance . state . params = cacheData . params
48
+
49
+ console . log ( 'staleTime' , staleTime )
50
+
51
+ if ( staleTime === - 1 || new Date ( ) . getTime ( ) - cacheData . time <= staleTime ) {
52
+ fetchInstance . state . loading = false
53
+ }
54
+ }
55
+
56
+ // 如果存在相同的cacheKey,触发更新
57
+ unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , data => {
58
+ fetchInstance . setState ( { data } )
59
+ } )
60
+ } )
61
+
62
+ onUnmounted ( ( ) => {
63
+ unSubscribeRef . value ?.( )
64
+ } )
65
+
66
+ if ( ! cacheKey ) {
67
+ return { }
68
+ }
69
+
70
+ return {
71
+ onBefore : params => {
72
+ const cacheData = _getCache ( cacheKey , params )
73
+
74
+ if ( ! cacheData || ! Object . hasOwnProperty . call ( cacheData , 'data' ) ) {
75
+ return { }
76
+ }
77
+ // 数据是新鲜就停止请求
78
+ if ( staleTime === - 1 || new Date ( ) . getTime ( ) - cacheData . time <= staleTime ) {
79
+ console . log ( '停止请求' )
80
+
81
+ return {
82
+ loading : false ,
83
+ data : cacheData ?. data ,
84
+ returnNow : true ,
85
+ }
86
+ } else {
87
+ // 数据不新鲜,则返回data,并且继续发送请求
88
+ return {
89
+ data : cacheData ?. data ,
90
+ }
91
+ }
92
+ } ,
93
+ onRequest : ( service , args ) => {
94
+ let servicePromise = cachePromise . getCachePromise ( cacheKey )
95
+ // 如果存在servicePromise,并且它没有被触发,则使用它
96
+ if ( servicePromise && servicePromise !== currentPromiseRef . value ) {
97
+ return { servicePromise }
98
+ }
99
+
100
+ servicePromise = service ( ...args )
101
+ currentPromiseRef . value = servicePromise
102
+ cachePromise . setCachePromise ( cacheKey , servicePromise )
103
+ return { servicePromise }
104
+ } ,
105
+ onSuccess : ( data , params ) => {
106
+ if ( cacheKey ) {
107
+ // 取消更新,避免反复触发自己
108
+ unSubscribeRef . value ?.( )
109
+ _setCache ( cacheKey , {
110
+ data,
111
+ params,
112
+ time : new Date ( ) . getTime ( ) ,
113
+ } )
114
+ // 触发器更新
115
+ unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , d => {
116
+ fetchInstance . setState ( { data : d } )
117
+ } )
118
+ }
119
+ } ,
120
+ onMutate : data => {
121
+ if ( cacheKey ) {
122
+ // 取消更新,避免反复触发自己
123
+ unSubscribeRef . value ?.( )
124
+ _setCache ( cacheKey , {
125
+ data,
126
+ params : fetchInstance . state . params ,
127
+ time : new Date ( ) . getTime ( ) ,
128
+ } )
129
+ // 触发器更新
130
+ unSubscribeRef . value = cacheSubscribe . subscribe ( cacheKey , d => {
131
+ fetchInstance . setState ( { data : d } )
132
+ } )
133
+ }
134
+ } ,
135
+ }
142
136
}
143
137
144
138
export default useCachePlugin
0 commit comments