dbsd_kczx/src/hooks/core/useLockFn.ts

20 lines
441 B
TypeScript
Raw Normal View History

2021-10-20 14:32:09 +08:00
import { ref, unref } from 'vue';
export function useLockFn<P extends any[] = any[], V extends any = any>(
fn: (...args: P) => Promise<V>
) {
const lockRef = ref(false);
return async function (...args: P) {
if (unref(lockRef)) return;
lockRef.value = true;
try {
const ret = await fn(...args);
lockRef.value = false;
return ret;
} catch (e) {
lockRef.value = false;
throw e;
}
};
}