replace useDebouncedRef with @vueuse/useDebounce

This commit is contained in:
geoffrey45
2022-09-10 18:36:30 +03:00
parent 5770a66d67
commit eb774eeaeb
3 changed files with 4 additions and 61 deletions
-2
View File
@@ -2,7 +2,6 @@ import focusElem from "./useFocusElem";
import putCommas from "./usePutCommas";
import useVisibility from "./useVisibility";
import formatSeconds from "./useFormatSeconds";
import useDebouncedRef from "./useDebouncedRef";
import createSubPaths from "./useCreateSubPaths";
import { readLocalStorage, writeLocalStorage } from "./useLocalStorage";
import useFuse from "./useFuse";
@@ -11,7 +10,6 @@ export {
readLocalStorage,
writeLocalStorage,
createSubPaths,
useDebouncedRef,
focusElem,
useVisibility,
formatSeconds,
-56
View File
@@ -1,56 +0,0 @@
import { customRef, Ref, ref } from "vue";
/**
* Debounces a function
*
* @param {*} fn The function to debounce
* @param {*} delay The delay in milliseconds
* @param {*} immediate whether to debounce immediately
*/
const debounce = (
fn: (...params: any) => void,
delay: number = 0,
immediate: any = false
) => {
let timeout: any;
return (...args: any) => {
if (immediate && !timeout) fn(...args);
clearTimeout(timeout);
timeout = setTimeout(() => {
fn(...args);
}, delay);
};
};
/**
* Emits the ref updated value after the given delay.
*
* @param {*} initialValue The default value of the ref
* @param {*} delay The delay in milliseconds
* @param {*} immediate Whether to call the function immediately
* @returns {Object} The ref and a function to call to update the ref
*/
const useDebouncedRef = (
initialValue: string,
delay: number,
immediate = false
) => {
const state = ref(initialValue);
return customRef((track, trigger) => ({
get() {
track();
return state.value;
},
set: debounce(
(value) => {
state.value = value;
trigger();
},
delay,
immediate
),
}));
};
export default useDebouncedRef;