Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 0c2bf5d

Browse files
committed
feat: add refresh and params to the hooks service
1 parent cf18d8c commit 0c2bf5d

File tree

1 file changed

+14
-1
lines changed
  • packages/console-utils/xconsole-service/src/hooks

1 file changed

+14
-1
lines changed

packages/console-utils/xconsole-service/src/hooks/useAsync.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ export interface ReturnValue<P, T> {
7575
error?: Error;
7676
data?: T;
7777
cancel: noop;
78+
/**当次执行的 service 的参数数组*/
79+
params?: any[];
7880
run: promiseReturn<P, T | undefined>;
81+
/**使用上一次的 params,重新调用 run */
82+
refresh: () => void;
7983
timer: {
8084
stop: noop;
8185
resume: noop;
@@ -92,6 +96,8 @@ export default function useAsync<P = any, Result = any>(
9296
loading: false,
9397
cancel: noop,
9498
run: promiseReturn,
99+
params: [],
100+
refresh:noop,
95101
timer: {
96102
stop: noop,
97103
resume: promiseReturn,
@@ -113,7 +119,7 @@ export default function useAsync<P = any, Result = any>(
113119
const run = useCallback((...args: any[]): Promise<Result | undefined> => {
114120
// 确保不会返回被取消的结果
115121
const runCount = count.current;
116-
set((s) => ({ ...s, loading: true }));
122+
set((s) => ({ ...s, loading: true,params:args }));
117123
return fn(...args)
118124
.then((data) => {
119125
if (runCount === count.current) {
@@ -139,6 +145,11 @@ export default function useAsync<P = any, Result = any>(
139145
});
140146
}, deps);
141147

148+
149+
const refresh = useCallback(() => {
150+
run(state.params||[]);
151+
}, []);
152+
142153
const stop = useCallback(() => {
143154
count.current += 1;
144155
// 清除计时器
@@ -248,7 +259,9 @@ export default function useAsync<P = any, Result = any>(
248259
loading: state.loading,
249260
error: state.error,
250261
data: state.data,
262+
params:state.params,
251263
cancel,
264+
refresh,
252265
run: options.manual && options.pollingInterval ? start : reload,
253266
timer: {
254267
stop,

0 commit comments

Comments
 (0)