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.8 kB
var e,r=require("react"),t=(e=r)&&"object"==typeof e&&"default"in e?e.default:e;function n(e,r){return(n=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function o(e,r,t){return e(t={path:r,exports:{},require:function(e,r){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}()}},t.exports),t.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 r=e.$$typeof;switch(r){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 r}}case c:return r}}}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,r){"production"!==process.env.NODE_ENV&&function(){var e="function"==typeof Symbol&&Symbol.for,t=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 r=e.$$typeof;switch(r){case t: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 r}}case n:return r}}}var O=s,w=u,E=c,$=t,j=l,x=o,T=m,P=d,k=n,C=i,_=a,I=p,R=!1;function N(e){return S(e)===s}r.AsyncMode=f,r.ConcurrentMode=O,r.ContextConsumer=w,r.ContextProvider=E,r.Element=$,r.ForwardRef=j,r.Fragment=x,r.Lazy=T,r.Memo=P,r.Portal=k,r.Profiler=C,r.StrictMode=_,r.Suspense=I,r.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},r.isConcurrentMode=N,r.isContextConsumer=function(e){return S(e)===u},r.isContextProvider=function(e){return S(e)===c},r.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===t},r.isForwardRef=function(e){return S(e)===l},r.isFragment=function(e){return S(e)===o},r.isLazy=function(e){return S(e)===m},r.isMemo=function(e){return S(e)===d},r.isPortal=function(e){return S(e)===n},r.isProfiler=function(e){return S(e)===i},r.isStrictMode=function(e){return S(e)===a},r.isSuspense=function(e){return S(e)===p},r.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)},r.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 r={},t=0;t<10;t++)r["_"+String.fromCharCode(t)]=t;if("0123456789"!==Object.getOwnPropertyNames(r).map(function(e){return r[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,r){for(var t,n,o=I(e),a=1;a<arguments.length;a++){for(var i in t=Object(arguments[a]))C.call(t,i)&&(o[i]=t[i]);if(k){n=k(t);for(var c=0;c<n.length;c++)_.call(t,n[c])&&(o[n[c]]=t[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 r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}}function F(e,r,t,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")+": "+t+" 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](r,a,n,t,null,A)}catch(e){i=e}if(!i||i instanceof Error||M((n||"React class")+": type specification of "+t+" `"+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 "+t+" 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 r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}),J.resetWarningCache=L;var B=o(function(e){e.exports="production"!==process.env.NODE_ENV?function(e,r){var t="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(r,t,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside arrayOf.");var c=r[t];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(r,t,n,o,i){var c=r[t];return e(c)?null:new a("Invalid "+o+" `"+i+"` of type `"+f(c)+"` supplied to `"+n+"`, expected a single ReactElement.")}),elementType:i(function(e,r,t,n,o){var i=e[r];return P.isValidElementType(i)?null:new a("Invalid "+n+" `"+o+"` of type `"+f(i)+"` supplied to `"+t+"`, expected a single ReactElement type.")}),instanceOf:function(e){return i(function(r,t,n,o,i){var c;return r[t]instanceof e?null:new a("Invalid "+o+" `"+i+"` of type `"+((c=r[t]).constructor&&c.constructor.name?c.constructor.name:"<<anonymous>>")+"` supplied to `"+n+"`, expected instance of `"+(e.name||"<<anonymous>>")+"`.")})},node:i(function(e,r,t,n,o){return u(e[r])?null:new a("Invalid "+n+" `"+o+"` supplied to `"+t+"`, expected a ReactNode.")}),objectOf:function(e){return i(function(r,t,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside objectOf.");var c=r[t],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 r(r,t,n,i,c){for(var u=r[t],f=0;f<e.length;f++)if(o(u,e[f]))return null;var l=JSON.stringify(e,function(e,r){return"symbol"===s(r)?String(r):r});return new a("Invalid "+i+" `"+c+"` of value `"+String(u)+"` supplied to `"+n+"`, expected one of "+l+".")}return i(r)},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 r=0;r<e.length;r++){var t=e[r];if("function"!=typeof t)return z("Invalid argument supplied to oneOfType. Expected an array of check functions, but received "+l(t)+" at index "+r+"."),U}return i(function(r,t,n,o,i){for(var c=0;c<e.length;c++)if(null==(0,e[c])(r,t,n,o,i,N))return null;return new a("Invalid "+o+" `"+i+"` supplied to `"+n+"`.")})},shape:function(e){return i(function(r,t,n,o,i){var c=r[t],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(r,t,n,o,i){var c=r[t],u=f(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+n+"`, expected `object`.");var s=R({},r[t],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(r[t],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,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}function a(e){this.message=e,this.stack=""}function i(e){function r(r,t,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==t[n]?r?new a(null===t[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(t,n,o,i,c)}process;var t=r.bind(null,!1);return t.isRequired=r.bind(null,!0),t}function c(e){return i(function(r,t,n,o,i,c){var u=r[t];return f(u)!==e?new a("Invalid "+o+" `"+i+"` of type `"+s(u)+"` supplied to `"+n+"`, expected `"+e+"`."):null})}function u(r){switch(typeof r){case"number":case"string":case"undefined":return!0;case"boolean":return!r;case"object":if(Array.isArray(r))return r.every(u);if(null===r||e(r))return!0;var n=function(e){var r=e&&(t&&e[t]||e["@@iterator"]);if("function"==typeof r)return r}(r);if(!n)return!1;var o,a=n.call(r);if(n!==r.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 r=typeof e;return Array.isArray(e)?"array":e instanceof RegExp?"object":function(e,r){return"symbol"===e||!!r&&("Symbol"===r["@@toStringTag"]||"function"==typeof Symbol&&r instanceof Symbol)}(r,e)?"symbol":r}function s(e){if(null==e)return""+e;var r=f(e);if("object"===r){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return r}function l(e){var r=s(e);switch(r){case"array":case"object":return"an "+r;case"boolean":case"date":case"regexp":return"a "+r;default:return r}}return a.prototype=Error.prototype,n.checkPropTypes=q,n.resetWarningCache=q.resetWarningCache,n.PropTypes=n,n}(P.isElement):function(){function e(e,r,t,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 r(){return e}e.isRequired=e;var t={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:r,element:e,elementType:e,instanceOf:r,node:e,objectOf:r,oneOf:r,oneOfType:r,shape:r,exact:r,checkPropTypes:J,resetWarningCache:L};return t.PropTypes=t,t}()}),Y=function(e){var r,t;function o(r){var t;return(t=e.call(this,r)||this).callback=function(){if(t.timer){var e=t.props.callback;"function"==typeof e&&e(),t.start()}},t.start=function(){t.stop(),t.timer=setTimeout(t.callback,t.props.timeout)},t.stop=function(){t.timer&&clearTimeout(t.timer),t.timer=null},t.render=function(){return!1},t.timer=null,t}t=e,(r=o).prototype=Object.create(t.prototype),r.prototype.constructor=r,n(r,t);var a=o.prototype;return a.componentDidMount=function(){this.props.enabled&&this.start()},a.shouldComponentUpdate=function(e){var r=this.props;return r.timeout!==e.timeout||r.callback!==e.callback||r.enabled!==e.enabled},a.componentDidUpdate=function(e){var r=this.props,t=r.enabled;t===e.enabled&&r.timeout===e.timeout||(t?this.start():this.stop())},a.componentWillUnmount=function(){this.stop()},o}(t.Component);Y.defaultProps={enabled:!1,timeout:1e3},Y.propTypes={callback:B.func.isRequired,enabled:B.bool,timeout:B.number};var H=function(){};exports.ReactInterval=Y,exports.useInterval=function(e,t,n){void 0===t&&(t=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=r.useRef(),d=r.useRef(!1),m=r.useRef(null),b=r.useRef(e),v=r.useCallback(function(){var e=m.current||0;if(p){var r=Date.now()-e,n=1+(r>0?Math.floor(r/t):0);m.current=e+t*n,h(Math.max(t-r,1)),b.current(n)}else h(t),b.current()},[t]),h=r.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=r.useCallback(function(){var e=d.current;d.current=!0,null===m.current&&(m.current=Date.now()+t),s&&!e&&(m.current-=t,v()),h(t)},[v,t,s,h]),S=r.useCallback(function(e){void 0===e&&(e=!0);var r=d.current;void 0!==y.current&&clearTimeout(y.current),d.current=!1,y.current=void 0,m.current=null,r&&e&&i()},[i]),O=r.useCallback(function(){return d.current},[]);return r.useEffect(function(){b.current=e},[e]),r.useEffect(function(){return u&&g(),S},[]),{start:g,stop:S,isActive:O}}; //# sourceMappingURL=index.js.map