@empathyco/x-components
Version:
Empathy X Components
24 lines (21 loc) • 803 B
JavaScript
import { onBeforeUnmount } from 'vue';
import { debounce } from '../utils/debounce.js';
/**
* Composable which wraps the function passed as parameter into a debounced function and returns it.
* It also cancels the debounced function when component is unmounted.
*
* @param fn - Function to be debounced.
* @param debounceTimeInMs - Time of debounce in ms.
* @param debounceOptions - The options for the debounce strategy.
* @returns Debounced function obtained from `fn` parameter.
* @public
*/
function useDebounce(fn, debounceTimeInMs, debounceOptions = {}) {
const debouncedFn = debounce(fn, debounceTimeInMs, debounceOptions);
onBeforeUnmount(() => {
debouncedFn.cancel();
});
return debouncedFn;
}
export { useDebounce };
//# sourceMappingURL=use-debounce.js.map