@better-hooks/performance
Version:
3 lines (2 loc) • 3.79 kB
JavaScript
import{useRef as n,useCallback as t,useEffect as r,useState as e,useMemo as u,useLayoutEffect as c}from"react";import{useForceUpdate as o,useDidUpdate as i,useDidMount as l}from"@better-hooks/lifecycle";var f=function(e){var u=(e||{}).delay,c=void 0===u?400:u,i=n(!1),l=n(null),f=o(),a=t((function(){i.current&&f()}),[f]),v=function(){null!==l.current&&clearTimeout(l.current),l.current=null},d=t((function(n,t){var r=null!=t?t:c;v(),l.current=setTimeout((function(){l.current=null,n(),a()}),r),a()}),[c,a]);return r((function(){return v}),[]),{get active(){return i.current=!0,!!l.current},debounce:d,reset:v}},a=function(n,t,e){var u=f(t).debounce;r((function(){u(n)}),e)};function v(n,t){var r="function"==typeof Symbol&&n[Symbol.iterator];if(!r)return n;var e,u,c=r.call(n),o=[];try{for(;(void 0===t||t-- >0)&&!(e=c.next()).done;)o.push(e.value)}catch(n){u={error:n}}finally{try{e&&!e.done&&(r=c.return)&&r.call(c)}finally{if(u)throw u.error}}return o}var d=function(n,t){var r=f(t).debounce,u=v(e(n),2),c=u[0],o=u[1];return[c,function(n){return r((function(){return o(n)}))}]},y=function(n){var t=Object.prototype.toString.call(n);return Array.isArray(n)?!n.length:"object"==typeof n&&null!==n&&"[object Object]"===t&&!Object.keys(n).length},m=function(n,t){var r,e=Object.prototype.toString.call(n),u=Object.prototype.toString.call(t),c=typeof n,o=typeof t,i=function(n){return c===n&&o===n};return e===u&&(null===n&&null===t||(!!(i("number")&&Number.isNaN(n)&&Number.isNaN(t))||(!(!y(n)||!y(t))||(Array.isArray(n)&&Array.isArray(t)?n.length===t.length&&!n.some((function(n,r){return!m(n,t[r])})):i("object")&&(e===(r="[object Object]")&&u===r)?Object.keys(n).length===Object.keys(t).length&&!Object.entries(n).some((function(n){var r=v(n,2),e=r[0],u=r[1];return!m(u,t[e])})):n instanceof Date&&t instanceof Date?+n==+t:n===t))))},s=function(t,e){return r(t,(o=n(c=e),i=n(0),m(c,o.current)||(o.current=c,i.current+=1),u((function(){return o.current}),[i.current])));var c,o,i},b=function(t,r,u){var c=u||{},o=c.immediate,f=void 0===o||o,a=c.disabled,d=void 0!==a&&a,y=v(e(null),2),m=y[0],s=y[1],b=n(t),p=function(){m&&clearInterval(m),s(null)},h=function(){p(),!r&&0!==r||d||s(setInterval((function(){return b.current()}),r))};return i((function(){b.current=t}),[t]),l((function(){if(b.current=t,f)return h(),p})),i((function(){return b.current=t,h(),p}),[r]),{active:null!==m,start:h,stop:p}},p=function(t){var e=t||{},u=e.interval,c=void 0===u?200:u,i=e.timeout,l=void 0===i?200:i,f=n(0),a=n(!0),v=n(!1),d=n(null),y=o(),m=function(){a.current&&(a.current=!1),v.current&&y()},s=function(){null!==d.current&&clearTimeout(d.current),d.current=null};return r((function(){return s}),[]),{get active(){return v.current=!0,!!d.current},throttle:function(n,t){var r,e,u=function(){f.current=Date.now(),n(),m()},o=null!==(r=null==t?void 0:t.interval)&&void 0!==r?r:c,i=null!==(e=null==t?void 0:t.timeout)&&void 0!==e?e:l,v=Date.now()>=f.current+o;a.current&&m(),d.current&&s(),v?u():i&&(d.current=setTimeout((function(){d.current=null,a.current=!0,u()}),i))},reset:s}},h=function(n,t,e){var u=p(t).throttle;r((function(){u(n)}),e)},j=function(n,t){var r=p(t).throttle,u=v(e(n),2),c=u[0],o=u[1];return[c,function(n){return r((function(){return o(n)}))}]},g=function(t,e){var u=n(t);w((function(){u.current=t}),[t]),r((function(){if(e||0===e){var n=setTimeout((function(){return u.current()}),e);return function(){return clearTimeout(n)}}}),[e])},w=!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?c:r;export{f as useDebounce,a as useDebounceEffect,d as useDebounceState,s as useDeepEffect,b as useInterval,w as useIsomorphicEffect,p as useThrottle,h as useThrottleEffect,j as useThrottleState,g as useTimeout};
//# sourceMappingURL=index.esm.js.map