UNPKG

ivt

Version:

Ivt Components Library

69 lines (66 loc) 2.82 kB
import { useRef, useEffect, useMemo } from 'react'; function c(e, u, c, i) { var a = this, o = useRef(null), f = useRef(0), l = useRef(0), v = useRef(null), m = useRef([]), d = useRef(), g = useRef(), p = useRef(e), w = useRef(true); p.current = e; var s = "undefined" != typeof window, x = !u && 0 !== u && s; if ("function" != typeof e) throw new TypeError("Expected a function"); u = +u || 0; var h = !!(c = c || {}).leading, y = !("trailing" in c) || !!c.trailing, F = "maxWait" in c, A = "debounceOnServer" in c && !!c.debounceOnServer, D = F ? Math.max(+c.maxWait || 0, u) : null; useEffect(function() { return w.current = true, function() { w.current = false; }; }, []); var T = useMemo(function() { var r = function(r) { var n = m.current, t = d.current; return m.current = d.current = null, f.current = r, l.current = l.current || r, g.current = p.current.apply(t, n); }, n = function(r, n) { x && cancelAnimationFrame(v.current), v.current = x ? requestAnimationFrame(r) : setTimeout(r, n); }, t = function(r) { if (!w.current) return false; var n = r - o.current; return !o.current || n >= u || n < 0 || F && r - f.current >= D; }, e = function(n) { return v.current = null, y && m.current ? r(n) : (m.current = d.current = null, g.current); }, c = function r() { var c = Date.now(); if (h && l.current === f.current && T(), t(c)) return e(c); if (w.current) { var i = u - (c - o.current), a = F ? Math.min(i, D - (c - f.current)) : i; n(r, a); } }, T = function() { }, W = function() { if (s || A) { var e = Date.now(), i = t(e); if (m.current = [].slice.call(arguments), d.current = a, o.current = e, i) { if (!v.current && w.current) return f.current = o.current, n(c, u), h ? r(o.current) : g.current; if (F) return n(c, u), r(o.current); } return v.current || n(c, u), g.current; } }; return W.cancel = function() { var r = v.current; r && (x ? cancelAnimationFrame(v.current) : clearTimeout(v.current)), f.current = 0, m.current = o.current = d.current = v.current = null; }, W.isPending = function() { return !!v.current; }, W.flush = function() { return v.current ? e(Date.now()) : g.current; }, W; }, [ h, F, u, D, y, x, s, A, i ]); return T; } export { c }; //# sourceMappingURL=index.module-1-Lm1QYF.mjs.map