Skip to content

Commit 8f940a4

Browse files
committed
refactor!: queries changed from shallowReactive to reactive
1 parent 3694f3c commit 8f940a4

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/__tests__/index.test.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,7 @@ describe('useRequest', () => {
11571157
id={item.username}
11581158
onClick={() => run(item.id)}
11591159
>
1160-
{queries[item.id]?.loading.value
1161-
? 'loading'
1162-
: item.username}
1160+
{queries[item.id]?.loading ? 'loading' : item.username}
11631161
</li>
11641162
))}
11651163
</ul>
@@ -1211,7 +1209,7 @@ describe('useRequest', () => {
12111209
id={item.username}
12121210
onClick={() => run(item.id)}
12131211
>
1214-
{queries[item.id]?.loading.value ? 'loading' : item.username}
1212+
{queries[item.id]?.loading ? 'loading' : item.username}
12151213
</li>
12161214
))}
12171215
</ul>

src/core/useAsyncQuery.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {
22
computed,
33
inject,
44
onUnmounted,
5+
reactive,
56
ref,
6-
shallowReactive,
77
watch,
88
watchEffect,
99
} from 'vue';
@@ -32,6 +32,7 @@ import {
3232
import { getCache, setCache } from './utils/cache';
3333
import limitTrigger from './utils/limitTrigger';
3434
import subscriber from './utils/listener';
35+
import { UnWrapRefObject } from './utils/types';
3536

3637
export type BaseResult<R, P extends unknown[]> = Omit<
3738
QueryState<R, P>,
@@ -40,8 +41,12 @@ export type BaseResult<R, P extends unknown[]> = Omit<
4041
run: (...arg: P) => InnerRunReturn<R>;
4142
};
4243

44+
export type UnWrapState<R, P extends unknown[]> = UnWrapRefObject<
45+
InnerQueryState<R, P>
46+
>;
47+
4348
export type Queries<R, P extends unknown[]> = {
44-
[key: string]: InnerQueryState<R, P>;
49+
[key: string]: UnWrapState<R, P>;
4550
};
4651

4752
const QUERY_DEFAULT_KEY = '__QUERY_DEFAULT_KEY__';
@@ -147,8 +152,8 @@ function useAsyncQuery<R, P extends unknown[], FR>(
147152
const error = ref<Error>();
148153
const params = ref<P>();
149154

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)),
152157
});
153158

154159
const latestQueriesKey = ref(QUERY_DEFAULT_KEY);
@@ -159,10 +164,10 @@ function useAsyncQuery<R, P extends unknown[], FR>(
159164
// TODO: 需要探索一下有没有更优的处理方法
160165
watchEffect(
161166
() => {
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;
166171
},
167172
{
168173
flush: 'sync',
@@ -177,12 +182,14 @@ function useAsyncQuery<R, P extends unknown[], FR>(
177182
Object.keys(cache.data.queries).forEach(key => {
178183
const cacheQuery = cache.data.queries![key];
179184

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+
);
186193
});
187194
/* istanbul ignore else */
188195
if (cache.data.latestQueriesKey) {
@@ -202,7 +209,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
202209
const newKey = queryKey?.(...args) ?? QUERY_DEFAULT_KEY;
203210

204211
if (!queries[newKey]) {
205-
queries[newKey] = createQuery(query, config);
212+
queries[newKey] = <UnWrapState<R, P>>reactive(createQuery(query, config));
206213
}
207214

208215
latestQueriesKey.value = newKey;
@@ -292,7 +299,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
292299
unsubscribeList.forEach(unsubscribe => unsubscribe());
293300
});
294301

295-
const queryState = {
302+
return {
296303
loading,
297304
data,
298305
error,
@@ -302,9 +309,7 @@ function useAsyncQuery<R, P extends unknown[], FR>(
302309
mutate: latestQuery.value.mutate,
303310
run,
304311
queries,
305-
} as QueryState<R, P>;
306-
307-
return queryState;
312+
};
308313
}
309314

310315
export default useAsyncQuery;

0 commit comments

Comments
 (0)