@dvcol/svelte-utils
Version:
Svelte library for common utility functions and constants
21 lines (20 loc) • 531 B
JavaScript
import { debounce } from '@dvcol/common-utils/common/debounce';
/**
* Debounce a state and return a getter.
*
* @param getter a function that contains dependencies
* @param delay debounce delay (default 0)
* @param cb error callback
*
* return a getter function to be derived
*/
export function debounced(getter, delay = 0, cb) {
let current = $state();
const update = debounce((v) => {
current = v;
}, delay);
$effect(() => {
update(getter()).catch(cb);
});
return () => current;
}