mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-27 19:16:53 -05:00
feat: add useAsync and implement it on every async mutation method
This commit is contained in:
43
src/composables/useAsync.ts
Normal file
43
src/composables/useAsync.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { useAsyncState } from '@vueuse/core';
|
||||
|
||||
export type UseAsyncOptions<TResult, TArgs extends any[] = []> = {
|
||||
handler(...args: TArgs): Promise<TResult>;
|
||||
onSuccess?: (data: TResult) => void;
|
||||
onError?: (error: any) => void;
|
||||
allowParallel?: boolean;
|
||||
};
|
||||
|
||||
export type UseAsyncResult<TResult, TArgs extends any[] = []> = {
|
||||
state: Ref<TResult | null>;
|
||||
isLoading: Ref<boolean>;
|
||||
error: Ref<unknown>;
|
||||
execute: (...args: TArgs) => Promise<void>;
|
||||
};
|
||||
|
||||
export function useAsync<TResult, TArgs extends any[] = []>(ops: UseAsyncOptions<TResult, TArgs>): UseAsyncResult<TResult, TArgs> {
|
||||
const preventDuringLoading = !ops.allowParallel;
|
||||
const output = useAsyncState<TResult | null, TArgs, true>(ops.handler, null, {
|
||||
immediate: false,
|
||||
onSuccess(data) {
|
||||
ops.onSuccess?.(data as any);
|
||||
},
|
||||
onError(err) {
|
||||
ops.onError?.(err);
|
||||
}
|
||||
});
|
||||
|
||||
async function execute(...args: TArgs) {
|
||||
if (preventDuringLoading && output.isLoading.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
await output.executeImmediate(...args);
|
||||
}
|
||||
|
||||
return {
|
||||
state: output.state,
|
||||
isLoading: output.isLoading,
|
||||
error: output.error,
|
||||
execute
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user