export interface DelayedFunction void> { readonly schedule: (...args: Parameters) => void; readonly cancel: () => void; } export const DelayedFunction = void>(fun: T, delay: number): DelayedFunction => { let ref: number | null = null; const schedule = (...args: Parameters): void => { ref = setTimeout(() => { fun.apply(null, args); ref = null; }, delay); }; const cancel = (): void => { if (ref !== null) { clearTimeout(ref); ref = null; } }; return { cancel, schedule }; };