@better-typed/react-performance-hooks
Version:
React window hooks
3 lines (2 loc) • 1.04 kB
JavaScript
import{useRef as r}from"react";import{useForceUpdate as t,useWillUnmount as n}from"@better-typed/react-lifecycle-hooks";var e=function(e){void 0===e&&(e=400);var u=r(!1),c=r(null),o=t(),l=function(){u.current&&o()},i=function(){null!==c.current&&clearTimeout(c.current),c.current=null};return n(i),{get active(){return u.current=!0,!!c.current},debounce:function(r,t){var n=null!=t?t:e;i(),c.current=setTimeout((function(){c.current=null,r(),l()}),n),l()},reset:i}},u=function(e){void 0===e&&(e=200);var u=r(0),c=r(!0),o=r(!1),l=r(null),i=t(),f=function(){c.current&&(c.current=!1),o.current&&i()},a=function(){null!==l.current&&clearTimeout(l.current),l.current=null};return n(a),{get active(){return o.current=!0,!!l.current},throttle:function(r,t){var n=function(){u.current=Date.now(),r(),f()},o=null!=t?t:e,i=Date.now()>=u.current+o;c.current&&f(),l.current&&a(),i?n():l.current=setTimeout((function(){l.current=null,c.current=!0,n()}),o)},reset:a}};export{e as useDebounce,u as useThrottle};
//# sourceMappingURL=index.esm.js.map