
(function() {
    if (window.__impulseScreenBudgetInited) return;
    window.__impulseScreenBudgetInited = true;

    var SETTLE_MS = 150;
    var body = document.body;
    if (!body) return;
    var timer = 0;

    function poke() {
        if (!body.classList.contains('is-scrolling')) {
            body.classList.add('is-scrolling');
        }
        if (timer) clearTimeout(timer);
        timer = setTimeout(function() {
            body.classList.remove('is-scrolling');
            timer = 0;
        }, SETTLE_MS);
    }

    var opts = { passive: true, capture: true };
    window.addEventListener('scroll',    poke, opts);
    window.addEventListener('wheel',     poke, opts);
    window.addEventListener('touchmove', poke, opts);

    var moObs = new MutationObserver(function(records) {
        for (var i = 0; i < records.length; i++) {
            var t = records[i].target;
            if (t && t.classList && (
                t.classList.contains('impulse-screen-entering') ||
                t.classList.contains('impulse-screen-exiting')  ||
                t.classList.contains('impulse-screen-animating')
            )) {
                poke();
                return;
            }
        }
    });
    moObs.observe(document.documentElement, {
        subtree:        true,
        attributes:     true,
        attributeFilter: ['class']
    });
})();
