2
2
computed ,
3
3
inject ,
4
4
onUnmounted ,
5
+ reactive ,
5
6
ref ,
6
- shallowReactive ,
7
7
watch ,
8
8
watchEffect ,
9
9
} from 'vue' ;
@@ -32,6 +32,7 @@ import {
32
32
import { getCache , setCache } from './utils/cache' ;
33
33
import limitTrigger from './utils/limitTrigger' ;
34
34
import subscriber from './utils/listener' ;
35
+ import { UnWrapRefObject } from './utils/types' ;
35
36
36
37
export type BaseResult < R , P extends unknown [ ] > = Omit <
37
38
QueryState < R , P > ,
@@ -40,8 +41,12 @@ export type BaseResult<R, P extends unknown[]> = Omit<
40
41
run : ( ...arg : P ) => InnerRunReturn < R > ;
41
42
} ;
42
43
44
+ export type UnWrapState < R , P extends unknown [ ] > = UnWrapRefObject <
45
+ InnerQueryState < R , P >
46
+ > ;
47
+
43
48
export type Queries < R , P extends unknown [ ] > = {
44
- [ key : string ] : InnerQueryState < R , P > ;
49
+ [ key : string ] : UnWrapState < R , P > ;
45
50
} ;
46
51
47
52
const QUERY_DEFAULT_KEY = '__QUERY_DEFAULT_KEY__' ;
@@ -147,8 +152,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
147
152
const error = ref < Error > ( ) ;
148
153
const params = ref < P > ( ) ;
149
154
150
- const queries = shallowReactive < Queries < R , P > > ( {
151
- [ QUERY_DEFAULT_KEY ] : createQuery ( query , config ) ,
155
+ const queries = < Queries < R , P > > reactive ( {
156
+ [ QUERY_DEFAULT_KEY ] : reactive ( createQuery ( query , config ) ) ,
152
157
} ) ;
153
158
154
159
const latestQueriesKey = ref ( QUERY_DEFAULT_KEY ) ;
@@ -159,10 +164,10 @@ function useAsyncQuery<R, P extends unknown[], FR>(
159
164
// TODO: 需要探索一下有没有更优的处理方法
160
165
watchEffect (
161
166
( ) => {
162
- loading . value = latestQuery . value . loading . value ;
163
- data . value = latestQuery . value . data . value ;
164
- error . value = latestQuery . value . error . value ;
165
- params . value = latestQuery . value . params . value ;
167
+ loading . value = latestQuery . value . loading ;
168
+ data . value = latestQuery . value . data ;
169
+ error . value = latestQuery . value . error ;
170
+ params . value = latestQuery . value . params ;
166
171
} ,
167
172
{
168
173
flush : 'sync' ,
@@ -177,12 +182,14 @@ function useAsyncQuery<R, P extends unknown[], FR>(
177
182
Object . keys ( cache . data . queries ) . forEach ( key => {
178
183
const cacheQuery = cache . data . queries ! [ key ] ;
179
184
180
- queries [ key ] = createQuery ( query , config , {
181
- loading : cacheQuery . loading ,
182
- params : cacheQuery . params ,
183
- data : cacheQuery . data ,
184
- error : cacheQuery . error ,
185
- } ) ;
185
+ queries [ key ] = < UnWrapState < R , P > > reactive (
186
+ createQuery ( query , config , {
187
+ loading : cacheQuery . loading ,
188
+ params : cacheQuery . params ,
189
+ data : cacheQuery . data ,
190
+ error : cacheQuery . error ,
191
+ } ) ,
192
+ ) ;
186
193
} ) ;
187
194
/* istanbul ignore else */
188
195
if ( cache . data . latestQueriesKey ) {
@@ -202,7 +209,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
202
209
const newKey = queryKey ?.( ...args ) ?? QUERY_DEFAULT_KEY ;
203
210
204
211
if ( ! queries [ newKey ] ) {
205
- queries [ newKey ] = createQuery ( query , config ) ;
212
+ queries [ newKey ] = < UnWrapState < R , P > > reactive ( createQuery ( query , config ) ) ;
206
213
}
207
214
208
215
latestQueriesKey . value = newKey ;
@@ -292,7 +299,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
292
299
unsubscribeList . forEach ( unsubscribe => unsubscribe ( ) ) ;
293
300
} ) ;
294
301
295
- const queryState = {
302
+ return {
296
303
loading,
297
304
data,
298
305
error,
@@ -302,9 +309,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
302
309
mutate : latestQuery . value . mutate ,
303
310
run,
304
311
queries,
305
- } as QueryState < R , P > ;
306
-
307
- return queryState ;
312
+ } ;
308
313
}
309
314
310
315
export default useAsyncQuery ;
0 commit comments