"use strict";
// http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
Object.defineProperty(exports, "__esModule", { value: true });
exports.debounce = debounce;
exports.animateDebounce = animateDebounce;
function debounce(system, func, threshold = 100, execAsap = false) {
    let timeout;
    return function debounced(...args) {
        const { api: { window: { setTimeout, clearTimeout }, }, } = system;
        // @ts-ignore
        let obj = this;
        function delayed() {
            if (!execAsap) {
                func.apply(obj, args);
            }
            timeout = null;
        }
        if (timeout) {
            clearTimeout(timeout);
        }
        else if (execAsap) {
            func.apply(obj, args);
        }
        timeout = setTimeout(delayed, threshold);
    };
}
function animateDebounce(func, execAsap = false) {
    let frame;
    return function debounced() {
        // @ts-ignore
        let obj = this;
        let args = arguments;
        function delayed() {
            if (!execAsap) {
                func.apply(obj, args);
            }
            frame = null;
        }
        if (frame) {
            cancelAnimationFrame(frame);
        }
        else if (execAsap) {
            func.apply(obj, args);
        }
        frame = requestAnimationFrame(delayed);
    };
}
//# sourceMappingURL=debounce.js.map