/** @returns Debounced function that runs only after waitTime(ms) has elapsed since the last time it was last invoked. */
const debounce = (fn, waitTime) => {
    let timeout;
    let lastArgs;
    let lastThis;
    function debounced(...args) {
        lastArgs = args;
        lastThis = this;
        window.clearTimeout(timeout);
        timeout = window.setTimeout(() => {
            timeout = undefined;
            fn.apply(lastThis, lastArgs);
        }, waitTime);
    }
    debounced.cancel = () => {
        window.clearTimeout(timeout);
        timeout = undefined;
    };
    debounced.flush = () => {
        if (timeout !== undefined) {
            window.clearTimeout(timeout);
            timeout = undefined;
            fn.apply(lastThis, lastArgs);
        }
    };
    return debounced;
};
export default debounce;
//# sourceMappingURL=debounce.js.map