@tempots/std
Version:
Std library for TypeScript. Natural complement to the Tempo libraries.
66 lines (65 loc) • 1.55 kB
JavaScript
const T = (n, e) => {
const t = setTimeout(n, e);
return () => clearTimeout(t);
}, x = (n, e) => {
const t = setInterval(n, e);
return () => clearInterval(t);
};
function F(n, e, t = {}) {
const { noTrailing: a = !1, noLeading: l = !1, debounceMode: o } = t;
let i, f = !1, c = 0;
function m() {
i && clearTimeout(i);
}
function A(u) {
const { upcomingOnly: s = !1 } = u || {};
m(), f = !s;
}
function d(...u) {
if (f) return;
const s = this, p = Date.now() - c;
function r() {
c = Date.now(), e.apply(s, u);
}
function v() {
i = void 0;
}
!l && o && !i && r(), m(), o === void 0 && p > n ? l ? (c = Date.now(), a || (i = setTimeout(o ? v : r, n))) : r() : a || (i = setTimeout(
o ? v : r,
o === void 0 ? Math.max(0, n - p) : n
));
}
return d.cancel = A, d;
}
function I(n, e, { atBegin: t = !1 } = {}) {
return F(n, e, { debounceMode: t !== !1 });
}
const g = (n) => {
let e = null;
const t = (l) => {
e = null, n(l);
};
return (() => {
e == null && (e = requestAnimationFrame(t));
})(), () => {
e != null && (cancelAnimationFrame(e), e = null);
};
}, w = (n) => {
let e = null;
const t = (l) => {
e = requestAnimationFrame(t), n(l);
};
return (() => {
e == null && (e = requestAnimationFrame(t));
})(), () => {
e != null && (cancelAnimationFrame(e), e = null);
};
};
export {
I as debounce,
T as delayed,
g as delayedAnimationFrame,
x as interval,
w as intervalAnimationFrame,
F as throttle
};