UNPKG

@rooks/use-debounce

Version:
3 lines (2 loc) 2.23 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).useDebounce=e(t.React)}(this,function(t){"use strict";var e="Expected a function",n=NaN,r="[object Symbol]",o=/^\s+|\s+$/g,i=/^[-+]0x[0-9a-f]+$/i,u=/^0b[01]+$/i,f=/^0o[0-7]+$/i,c=parseInt,a="object"==typeof global&&global&&global.Object===Object&&global,s="object"==typeof self&&self&&self.Object===Object&&self,l=a||s||Function("return this")(),v=Object.prototype.toString,p=Math.max,b=Math.min,d=function(){return l.Date.now()};function y(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function m(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&v.call(t)==r}(t))return n;if(y(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=y(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(o,"");var a=u.test(t);return a||f.test(t)?c(t.slice(2),a?2:8):i.test(t)?n:+t}var j=function(t,n,r){var o,i,u,f,c,a,s=0,l=!1,v=!1,j=!0;if("function"!=typeof t)throw new TypeError(e);function g(e){var n=o,r=i;return o=i=void 0,s=e,f=t.apply(r,n)}function h(t){var e=t-a;return void 0===a||e>=n||e<0||v&&t-s>=u}function E(){var t=d();if(h(t))return x(t);c=setTimeout(E,function(t){var e=n-(t-a);return v?b(e,u-(t-s)):e}(t))}function x(t){return c=void 0,j&&o?g(t):(o=i=void 0,f)}function O(){var t=d(),e=h(t);if(o=arguments,i=this,a=t,e){if(void 0===c)return function(t){return s=t,c=setTimeout(E,n),l?g(t):f}(a);if(v)return c=setTimeout(E,n),g(a)}return void 0===c&&(c=setTimeout(E,n)),f}return n=m(n)||0,y(r)&&(l=!!r.leading,u=(v="maxWait"in r)?p(m(r.maxWait)||0,n):u,j="trailing"in r?!!r.trailing:j),O.cancel=function(){void 0!==c&&clearTimeout(c),s=0,o=a=i=c=void 0},O.flush=function(){return void 0===c?f:x(d())},O};return function(e,n,r){function o(t){return j(t,n,r)}const i=t.useRef(e),u=t.useRef(o(e));return t.useEffect(()=>{i.current=e}),t.useEffect(()=>{u.current=o((...t)=>{i.current(...t)})},[n,r]),function(...t){return t.forEach(t=>{!(t instanceof Event)&&t.nativeEvent instanceof Event&&t.persist()}),u.current(...t)}}}); //# sourceMappingURL=index.min.js.map