@tempots/std
Version:
Std library for TypeScript. Natural complement to the Tempo libraries.
39 lines (38 loc) • 994 B
JavaScript
const x = (e, n) => {
const t = setTimeout(e, n);
return () => clearTimeout(t);
}, D = (e, n) => {
const t = setInterval(e, n);
return () => clearInterval(t);
}, w = (e, n, t = {}) => {
const { noTrailing: r = !1, noLeading: u = !1, debounceMode: c } = t;
let o, f = !1, l = 0;
function d() {
o && clearTimeout(o);
}
function g(s) {
const { upcomingOnly: a = !1 } = s || {};
d(), f = !a;
}
function m(...s) {
if (f) return;
const a = this, p = Date.now() - l;
function i() {
l = Date.now(), n.apply(a, s);
}
function T() {
o = void 0;
}
!u && c && !o && i(), d(), c === void 0 && p > e ? u ? (l = Date.now(), r || (o = setTimeout(c ? T : i, e))) : i() : r || (o = setTimeout(
c ? T : i,
c === void 0 ? e - p : e
));
}
return m.cancel = g, m;
}, b = (e, n, { atBegin: t = !1 } = {}) => w(e, n, { debounceMode: t !== !1 });
export {
b as debounce,
x as delayed,
D as interval,
w as throttle
};