Skip to content

feat: add refresh and params to the hooks service #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/console-utils/xconsole-service/src/hooks/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export interface ReturnValue<P, T> {
error?: Error;
data?: T;
cancel: noop;
/**当次执行的 service 的参数数组*/
params?: any[];
run: promiseReturn<P, T | undefined>;
/**使用上一次的 params,重新调用 run */
refresh: () => void;
timer: {
stop: noop;
resume: noop;
Expand All @@ -92,6 +96,8 @@ export default function useAsync<P = any, Result = any>(
loading: false,
cancel: noop,
run: promiseReturn,
params: [],
refresh:noop,
timer: {
stop: noop,
resume: promiseReturn,
Expand All @@ -113,7 +119,7 @@ export default function useAsync<P = any, Result = any>(
const run = useCallback((...args: any[]): Promise<Result | undefined> => {
// 确保不会返回被取消的结果
const runCount = count.current;
set((s) => ({ ...s, loading: true }));
set((s) => ({ ...s, loading: true,params:args }));
return fn(...args)
.then((data) => {
if (runCount === count.current) {
Expand All @@ -139,6 +145,11 @@ export default function useAsync<P = any, Result = any>(
});
}, deps);


const refresh = useCallback(() => {
run(state.params||[]);
}, []);

const stop = useCallback(() => {
count.current += 1;
// 清除计时器
Expand Down Expand Up @@ -248,7 +259,9 @@ export default function useAsync<P = any, Result = any>(
loading: state.loading,
error: state.error,
data: state.data,
params:state.params,
cancel,
refresh,
run: options.manual && options.pollingInterval ? start : reload,
timer: {
stop,
Expand Down