ivt
Version:
Ivt Components Library
74 lines (71 loc) • 3.42 kB
JavaScript
import { useRef, useMemo, useEffect } from 'react';
function c(e, u, c, i) {
var l = this, a = useRef(null), o = useRef(0), f = useRef(0), v = useRef(null), d = useRef([]), m = useRef(), s = useRef(), g = useRef(e), h = useRef(true), x = useRef(), E = useRef();
g.current = e;
var b = "undefined" != typeof window, p = !u && 0 !== u && b;
if ("function" != typeof e) throw new TypeError("Expected a function");
u = +u || 0;
var y = !!(c = c || {}).leading, w = !("trailing" in c) || !!c.trailing, O = !!c.flushOnExit && w, F = "maxWait" in c, L = "debounceOnServer" in c && !!c.debounceOnServer, A = F ? Math.max(+c.maxWait || 0, u) : null, D = useMemo(function() {
var r = function(r) {
var n = d.current, t = m.current;
return d.current = m.current = null, o.current = r, f.current = f.current || r, s.current = g.current.apply(t, n);
}, n = function(r, n) {
p && cancelAnimationFrame(v.current), v.current = p ? requestAnimationFrame(r) : setTimeout(r, n);
}, t = function(r) {
if (!h.current) return false;
var n = r - a.current;
return !a.current || n >= u || n < 0 || F && r - o.current >= A;
}, e = function(n) {
return v.current = null, w && d.current ? r(n) : (d.current = m.current = null, s.current);
}, c = function r() {
var c = Date.now();
if (y && f.current === o.current && D(), t(c)) return e(c);
if (h.current) {
var i = u - (c - a.current), l = F ? Math.min(i, A - (c - o.current)) : i;
n(r, l);
}
}, D = function() {
}, S = function() {
if (b || L) {
var e, i = Date.now(), f = t(i);
if (d.current = [].slice.call(arguments), m.current = l, a.current = i, O && !x.current && (x.current = function() {
var r;
"hidden" === (null == (r = global.document) ? void 0 : r.visibilityState) && E.current.flush();
}, null == (e = global.document) || null == e.addEventListener || e.addEventListener("visibilitychange", x.current)), f) {
if (!v.current && h.current) return o.current = a.current, n(c, u), y ? r(a.current) : s.current;
if (F) return n(c, u), r(a.current);
}
return v.current || n(c, u), s.current;
}
};
return S.cancel = function() {
var r = v.current;
r && (p ? cancelAnimationFrame(v.current) : clearTimeout(v.current)), o.current = 0, d.current = a.current = m.current = v.current = null;
}, S.isPending = function() {
return !!v.current;
}, S.flush = function() {
return v.current ? e(Date.now()) : s.current;
}, S;
}, [
y,
F,
u,
A,
w,
O,
p,
b,
L,
i
]);
return E.current = D, useEffect(function() {
return h.current = true, function() {
var r;
O && E.current.flush(), x.current && (null == (r = global.document) || null == r.removeEventListener || r.removeEventListener("visibilitychange", x.current), x.current = null), h.current = false;
};
}, [
O
]), D;
}
export { c };
//# sourceMappingURL=index.module-Czm-Rcez.mjs.map