useLockFn.ts 437 B

1234567891011121314151617
  1. import { ref, unref } from 'vue';
  2. export function useLockFn<P extends any[] = any[], V extends any = any>(fn: (...args: P) => Promise<V>) {
  3. const lockRef = ref(false);
  4. return async function (...args: P) {
  5. if (unref(lockRef)) return;
  6. lockRef.value = true;
  7. try {
  8. const ret = await fn(...args);
  9. lockRef.value = false;
  10. return ret;
  11. } catch (e) {
  12. lockRef.value = false;
  13. throw e;
  14. }
  15. };
  16. }