export type Debouncer = ReturnType; export type DebouncerOptions = { debounceTime: number; }; // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function debouncer(callFn: (value: T) => unknown, { debounceTime = 60 }: Partial = {}) { let currentValue: T | undefined; const emitValue = (): void => { if (currentValue === undefined) { throw new Error("Tried to emit undefined value from debouncer. This should never be possible"); } const emittingValue = currentValue; currentValue = undefined; callFn(emittingValue); }; const updateValue = (newValue: T): void => { if (currentValue !== undefined) { currentValue = newValue; return; } currentValue = newValue; setTimeout(emitValue, debounceTime); }; return { updateValue }; }