UNPKG

meet-hour-react-interval-hook

Version:

React hook for using self-correcting setInterval, augmented by management methods (start, stop, isActive) and also using React Class Based Component

3 lines (2 loc) 15.9 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).useInterval={},e.react)}(this,function(e,t){var r="default"in t?t.default:t;function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function o(e,t,r){return e(r={path:t,exports:{},require:function(e,t){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}()}},r.exports),r.exports}var a="function"==typeof Symbol&&Symbol.for,i=a?Symbol.for("react.element"):60103,c=a?Symbol.for("react.portal"):60106,u=a?Symbol.for("react.fragment"):60107,f=a?Symbol.for("react.strict_mode"):60108,s=a?Symbol.for("react.profiler"):60114,l=a?Symbol.for("react.provider"):60109,p=a?Symbol.for("react.context"):60110,y=a?Symbol.for("react.async_mode"):60111,d=a?Symbol.for("react.concurrent_mode"):60111,m=a?Symbol.for("react.forward_ref"):60112,b=a?Symbol.for("react.suspense"):60113,v=a?Symbol.for("react.suspense_list"):60120,h=a?Symbol.for("react.memo"):60115,g=a?Symbol.for("react.lazy"):60116,S=a?Symbol.for("react.block"):60121,O=a?Symbol.for("react.fundamental"):60117,w=a?Symbol.for("react.responder"):60118,E=a?Symbol.for("react.scope"):60119;function $(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case i:switch(e=e.type){case y:case d:case u:case s:case f:case b:return e;default:switch(e=e&&e.$$typeof){case p:case m:case g:case h:case l:return e;default:return t}}case c:return t}}}function j(e){return $(e)===d}var x={AsyncMode:y,ConcurrentMode:d,ContextConsumer:p,ContextProvider:l,Element:i,ForwardRef:m,Fragment:u,Lazy:g,Memo:h,Portal:c,Profiler:s,StrictMode:f,Suspense:b,isAsyncMode:function(e){return j(e)||$(e)===y},isConcurrentMode:j,isContextConsumer:function(e){return $(e)===p},isContextProvider:function(e){return $(e)===l},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===i},isForwardRef:function(e){return $(e)===m},isFragment:function(e){return $(e)===u},isLazy:function(e){return $(e)===g},isMemo:function(e){return $(e)===h},isPortal:function(e){return $(e)===c},isProfiler:function(e){return $(e)===s},isStrictMode:function(e){return $(e)===f},isSuspense:function(e){return $(e)===b},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===u||e===d||e===s||e===f||e===b||e===v||"object"==typeof e&&null!==e&&(e.$$typeof===g||e.$$typeof===h||e.$$typeof===l||e.$$typeof===p||e.$$typeof===m||e.$$typeof===O||e.$$typeof===w||e.$$typeof===E||e.$$typeof===S)},typeOf:$},T=o(function(e,t){"production"!==process.env.NODE_ENV&&function(){var e="function"==typeof Symbol&&Symbol.for,r=e?Symbol.for("react.element"):60103,n=e?Symbol.for("react.portal"):60106,o=e?Symbol.for("react.fragment"):60107,a=e?Symbol.for("react.strict_mode"):60108,i=e?Symbol.for("react.profiler"):60114,c=e?Symbol.for("react.provider"):60109,u=e?Symbol.for("react.context"):60110,f=e?Symbol.for("react.async_mode"):60111,s=e?Symbol.for("react.concurrent_mode"):60111,l=e?Symbol.for("react.forward_ref"):60112,p=e?Symbol.for("react.suspense"):60113,y=e?Symbol.for("react.suspense_list"):60120,d=e?Symbol.for("react.memo"):60115,m=e?Symbol.for("react.lazy"):60116,b=e?Symbol.for("react.block"):60121,v=e?Symbol.for("react.fundamental"):60117,h=e?Symbol.for("react.responder"):60118,g=e?Symbol.for("react.scope"):60119;function S(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case r:var y=e.type;switch(y){case f:case s:case o:case i:case a:case p:return y;default:var b=y&&y.$$typeof;switch(b){case u:case l:case m:case d:case c:return b;default:return t}}case n:return t}}}var O=s,w=u,E=c,$=r,j=l,x=o,T=m,P=d,k=n,C=i,_=a,I=p,R=!1;function N(e){return S(e)===s}t.AsyncMode=f,t.ConcurrentMode=O,t.ContextConsumer=w,t.ContextProvider=E,t.Element=$,t.ForwardRef=j,t.Fragment=x,t.Lazy=T,t.Memo=P,t.Portal=k,t.Profiler=C,t.StrictMode=_,t.Suspense=I,t.isAsyncMode=function(e){return R||(R=!0,console.warn("The ReactIs.isAsyncMode() alias has been deprecated, and will be removed in React 17+. Update your code to use ReactIs.isConcurrentMode() instead. It has the exact same API.")),N(e)||S(e)===f},t.isConcurrentMode=N,t.isContextConsumer=function(e){return S(e)===u},t.isContextProvider=function(e){return S(e)===c},t.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===r},t.isForwardRef=function(e){return S(e)===l},t.isFragment=function(e){return S(e)===o},t.isLazy=function(e){return S(e)===m},t.isMemo=function(e){return S(e)===d},t.isPortal=function(e){return S(e)===n},t.isProfiler=function(e){return S(e)===i},t.isStrictMode=function(e){return S(e)===a},t.isSuspense=function(e){return S(e)===p},t.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===o||e===s||e===i||e===a||e===p||e===y||"object"==typeof e&&null!==e&&(e.$$typeof===m||e.$$typeof===d||e.$$typeof===c||e.$$typeof===u||e.$$typeof===l||e.$$typeof===v||e.$$typeof===h||e.$$typeof===g||e.$$typeof===b)},t.typeOf=S}()}),P=o(function(e){e.exports="production"===process.env.NODE_ENV?x:T}),k=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;function I(e){if(null==e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}var R=function(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},r=0;r<10;r++)t["_"+String.fromCharCode(r)]=r;if("0123456789"!==Object.getOwnPropertyNames(t).map(function(e){return t[e]}).join(""))return!1;var n={};return"abcdefghijklmnopqrst".split("").forEach(function(e){n[e]=e}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},n)).join("")}catch(e){return!1}}()?Object.assign:function(e,t){for(var r,n,o=I(e),a=1;a<arguments.length;a++){for(var i in r=Object(arguments[a]))C.call(r,i)&&(o[i]=r[i]);if(k){n=k(r);for(var c=0;c<n.length;c++)_.call(r,n[c])&&(o[n[c]]=r[n[c]])}}return o},N="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",M=function(){};if("production"!==process.env.NODE_ENV){var A=N,D={},V=Function.call.bind(Object.prototype.hasOwnProperty);M=function(e){var t="Warning: "+e;"undefined"!=typeof console&&console.error(t);try{throw new Error(t)}catch(e){}}}function F(e,t,r,n,o){if("production"!==process.env.NODE_ENV)for(var a in e)if(V(e,a)){var i;try{if("function"!=typeof e[a]){var c=Error((n||"React class")+": "+r+" type `"+a+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[a]+"`.");throw c.name="Invariant Violation",c}i=e[a](t,a,n,r,null,A)}catch(e){i=e}if(!i||i instanceof Error||M((n||"React class")+": type specification of "+r+" `"+a+"` is invalid; the type checker function must return `null` or an `Error` but returned a "+typeof i+". You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument)."),i instanceof Error&&!(i.message in D)){D[i.message]=!0;var u=o?o():"";M("Failed "+r+" type: "+i.message+(null!=u?u:""))}}}F.resetWarningCache=function(){"production"!==process.env.NODE_ENV&&(D={})};var q=F,W=Function.call.bind(Object.prototype.hasOwnProperty),z=function(){};function U(){return null}function L(){}function J(){}"production"!==process.env.NODE_ENV&&(z=function(e){var t="Warning: "+e;"undefined"!=typeof console&&console.error(t);try{throw new Error(t)}catch(e){}}),J.resetWarningCache=L;var B=o(function(e){e.exports="production"!==process.env.NODE_ENV?function(e,t){var r="function"==typeof Symbol&&Symbol.iterator,n={array:c("array"),bool:c("boolean"),func:c("function"),number:c("number"),object:c("object"),string:c("string"),symbol:c("symbol"),any:i(U),arrayOf:function(e){return i(function(t,r,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside arrayOf.");var c=t[r];if(!Array.isArray(c))return new a("Invalid "+o+" `"+i+"` of type `"+f(c)+"` supplied to `"+n+"`, expected an array.");for(var u=0;u<c.length;u++){var s=e(c,u,n,o,i+"["+u+"]",N);if(s instanceof Error)return s}return null})},element:i(function(t,r,n,o,i){var c=t[r];return e(c)?null:new a("Invalid "+o+" `"+i+"` of type `"+f(c)+"` supplied to `"+n+"`, expected a single ReactElement.")}),elementType:i(function(e,t,r,n,o){var i=e[t];return P.isValidElementType(i)?null:new a("Invalid "+n+" `"+o+"` of type `"+f(i)+"` supplied to `"+r+"`, expected a single ReactElement type.")}),instanceOf:function(e){return i(function(t,r,n,o,i){var c;return t[r]instanceof e?null:new a("Invalid "+o+" `"+i+"` of type `"+((c=t[r]).constructor&&c.constructor.name?c.constructor.name:"<<anonymous>>")+"` supplied to `"+n+"`, expected instance of `"+(e.name||"<<anonymous>>")+"`.")})},node:i(function(e,t,r,n,o){return u(e[t])?null:new a("Invalid "+n+" `"+o+"` supplied to `"+r+"`, expected a ReactNode.")}),objectOf:function(e){return i(function(t,r,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside objectOf.");var c=t[r],u=f(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+n+"`, expected an object.");for(var s in c)if(W(c,s)){var l=e(c,s,n,o,i+"."+s,N);if(l instanceof Error)return l}return null})},oneOf:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&z(arguments.length>1?"Invalid arguments supplied to oneOf, expected an array, got "+arguments.length+" arguments. A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).":"Invalid argument supplied to oneOf, expected an array."),U;function t(t,r,n,i,c){for(var u=t[r],f=0;f<e.length;f++)if(o(u,e[f]))return null;var l=JSON.stringify(e,function(e,t){return"symbol"===s(t)?String(t):t});return new a("Invalid "+i+" `"+c+"` of value `"+String(u)+"` supplied to `"+n+"`, expected one of "+l+".")}return i(t)},oneOfType:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&z("Invalid argument supplied to oneOfType, expected an instance of array."),U;for(var t=0;t<e.length;t++){var r=e[t];if("function"!=typeof r)return z("Invalid argument supplied to oneOfType. Expected an array of check functions, but received "+l(r)+" at index "+t+"."),U}return i(function(t,r,n,o,i){for(var c=0;c<e.length;c++)if(null==(0,e[c])(t,r,n,o,i,N))return null;return new a("Invalid "+o+" `"+i+"` supplied to `"+n+"`.")})},shape:function(e){return i(function(t,r,n,o,i){var c=t[r],u=f(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+n+"`, expected `object`.");for(var s in e){var l=e[s];if(l){var p=l(c,s,n,o,i+"."+s,N);if(p)return p}}return null})},exact:function(e){return i(function(t,r,n,o,i){var c=t[r],u=f(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+n+"`, expected `object`.");var s=R({},t[r],e);for(var l in s){var p=e[l];if(!p)return new a("Invalid "+o+" `"+i+"` key `"+l+"` supplied to `"+n+"`.\nBad object: "+JSON.stringify(t[r],null," ")+"\nValid keys: "+JSON.stringify(Object.keys(e),null," "));var y=p(c,l,n,o,i+"."+l,N);if(y)return y}return null})}};function o(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function a(e){this.message=e,this.stack=""}function i(e){function t(t,r,n,o,i,c,u){if(o=o||"<<anonymous>>",c=c||n,u!==N){var f=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use `PropTypes.checkPropTypes()` to call them. Read more at http://fb.me/use-check-prop-types");throw f.name="Invariant Violation",f}return null==r[n]?t?new a(null===r[n]?"The "+i+" `"+c+"` is marked as required in `"+o+"`, but its value is `null`.":"The "+i+" `"+c+"` is marked as required in `"+o+"`, but its value is `undefined`."):null:e(r,n,o,i,c)}process;var r=t.bind(null,!1);return r.isRequired=t.bind(null,!0),r}function c(e){return i(function(t,r,n,o,i,c){var u=t[r];return f(u)!==e?new a("Invalid "+o+" `"+i+"` of type `"+s(u)+"` supplied to `"+n+"`, expected `"+e+"`."):null})}function u(t){switch(typeof t){case"number":case"string":case"undefined":return!0;case"boolean":return!t;case"object":if(Array.isArray(t))return t.every(u);if(null===t||e(t))return!0;var n=function(e){var t=e&&(r&&e[r]||e["@@iterator"]);if("function"==typeof t)return t}(t);if(!n)return!1;var o,a=n.call(t);if(n!==t.entries){for(;!(o=a.next()).done;)if(!u(o.value))return!1}else for(;!(o=a.next()).done;){var i=o.value;if(i&&!u(i[1]))return!1}return!0;default:return!1}}function f(e){var t=typeof e;return Array.isArray(e)?"array":e instanceof RegExp?"object":function(e,t){return"symbol"===e||!!t&&("Symbol"===t["@@toStringTag"]||"function"==typeof Symbol&&t instanceof Symbol)}(t,e)?"symbol":t}function s(e){if(null==e)return""+e;var t=f(e);if("object"===t){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return t}function l(e){var t=s(e);switch(t){case"array":case"object":return"an "+t;case"boolean":case"date":case"regexp":return"a "+t;default:return t}}return a.prototype=Error.prototype,n.checkPropTypes=q,n.resetWarningCache=q.resetWarningCache,n.PropTypes=n,n}(P.isElement):function(){function e(e,t,r,n,o,a){if(a!==N){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function t(){return e}e.isRequired=e;var r={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,elementType:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t,checkPropTypes:J,resetWarningCache:L};return r.PropTypes=r,r}()}),Y=function(e){var t,r;function o(t){var r;return(r=e.call(this,t)||this).callback=function(){if(r.timer){var e=r.props.callback;"function"==typeof e&&e(),r.start()}},r.start=function(){r.stop(),r.timer=setTimeout(r.callback,r.props.timeout)},r.stop=function(){r.timer&&clearTimeout(r.timer),r.timer=null},r.render=function(){return!1},r.timer=null,r}r=e,(t=o).prototype=Object.create(r.prototype),t.prototype.constructor=t,n(t,r);var a=o.prototype;return a.componentDidMount=function(){this.props.enabled&&this.start()},a.shouldComponentUpdate=function(e){var t=this.props;return t.timeout!==e.timeout||t.callback!==e.callback||t.enabled!==e.enabled},a.componentDidUpdate=function(e){var t=this.props,r=t.enabled;r===e.enabled&&t.timeout===e.timeout||(r?this.start():this.stop())},a.componentWillUnmount=function(){this.stop()},o}(r.Component);Y.defaultProps={enabled:!1,timeout:1e3},Y.propTypes={callback:B.func.isRequired,enabled:B.bool,timeout:B.number};var H=function(){};e.ReactInterval=Y,e.useInterval=function(e,r,n){void 0===r&&(r=1e3);var o=void 0===n?{}:n,a=o.onFinish,i=void 0===a?H:a,c=o.autoStart,u=void 0===c||c,f=o.immediate,s=void 0!==f&&f,l=o.selfCorrecting,p=void 0===l||l,y=t.useRef(),d=t.useRef(!1),m=t.useRef(null),b=t.useRef(e),v=t.useCallback(function(){var e=m.current||0;if(p){var t=Date.now()-e,n=1+(t>0?Math.floor(t/r):0);m.current=e+r*n,h(Math.max(r-t,1)),b.current(n)}else h(r),b.current()},[r]),h=t.useCallback(function(e){void 0!==y.current&&clearTimeout(y.current),d.current?y.current=setTimeout(v,e):console.debug("Trying to set interval timeout on inactive timer, this is no-op and probably indicates bug in your code.")},[v,d]),g=t.useCallback(function(){var e=d.current;d.current=!0,null===m.current&&(m.current=Date.now()+r),s&&!e&&(m.current-=r,v()),h(r)},[v,r,s,h]),S=t.useCallback(function(e){void 0===e&&(e=!0);var t=d.current;void 0!==y.current&&clearTimeout(y.current),d.current=!1,y.current=void 0,m.current=null,t&&e&&i()},[i]),O=t.useCallback(function(){return d.current},[]);return t.useEffect(function(){b.current=e},[e]),t.useEffect(function(){return u&&g(),S},[]),{start:g,stop:S,isActive:O}}}); //# sourceMappingURL=index.umd.js.map