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.3 kB
import e,{useRef as t,useCallback as r,useEffect as n}from"react";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 i="function"==typeof Symbol&&Symbol.for,a=i?Symbol.for("react.element"):60103,c=i?Symbol.for("react.portal"):60106,u=i?Symbol.for("react.fragment"):60107,s=i?Symbol.for("react.strict_mode"):60108,f=i?Symbol.for("react.profiler"):60114,l=i?Symbol.for("react.provider"):60109,p=i?Symbol.for("react.context"):60110,y=i?Symbol.for("react.async_mode"):60111,d=i?Symbol.for("react.concurrent_mode"):60111,m=i?Symbol.for("react.forward_ref"):60112,b=i?Symbol.for("react.suspense"):60113,v=i?Symbol.for("react.suspense_list"):60120,h=i?Symbol.for("react.memo"):60115,g=i?Symbol.for("react.lazy"):60116,S=i?Symbol.for("react.block"):60121,O=i?Symbol.for("react.fundamental"):60117,w=i?Symbol.for("react.responder"):60118,E=i?Symbol.for("react.scope"):60119;function $(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case a:switch(e=e.type){case y:case d:case u:case f:case s: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:a,ForwardRef:m,Fragment:u,Lazy:g,Memo:h,Portal:c,Profiler:f,StrictMode:s,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===a},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)===f},isStrictMode:function(e){return $(e)===s},isSuspense:function(e){return $(e)===b},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===u||e===d||e===f||e===s||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,i=e?Symbol.for("react.strict_mode"):60108,a=e?Symbol.for("react.profiler"):60114,c=e?Symbol.for("react.provider"):60109,u=e?Symbol.for("react.context"):60110,s=e?Symbol.for("react.async_mode"):60111,f=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 s:case f:case o:case a:case i: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=f,w=u,E=c,$=r,j=l,x=o,T=m,P=d,k=n,C=a,_=i,I=p,N=!1;function R(e){return S(e)===f}t.AsyncMode=s,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 N||(N=!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.")),R(e)||S(e)===s},t.isConcurrentMode=R,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)===a},t.isStrictMode=function(e){return S(e)===i},t.isSuspense=function(e){return S(e)===p},t.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===o||e===f||e===a||e===i||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 N=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),i=1;i<arguments.length;i++){for(var a in r=Object(arguments[i]))C.call(r,a)&&(o[a]=r[a]);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},R="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",M=function(){};if("production"!==process.env.NODE_ENV){var A=R,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 i in e)if(V(e,i)){var a;try{if("function"!=typeof e[i]){var c=Error((n||"React class")+": "+r+" type `"+i+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[i]+"`.");throw c.name="Invariant Violation",c}a=e[i](t,i,n,r,null,A)}catch(e){a=e}if(!a||a instanceof Error||M((n||"React class")+": type specification of "+r+" `"+i+"` is invalid; the type checker function must return `null` or an `Error` but returned a "+typeof a+". You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument)."),a instanceof Error&&!(a.message in D)){D[a.message]=!0;var u=o?o():"";M("Failed "+r+" type: "+a.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:a(U),arrayOf:function(e){return a(function(t,r,n,o,a){if("function"!=typeof e)return new i("Property `"+a+"` of component `"+n+"` has invalid PropType notation inside arrayOf.");var c=t[r];if(!Array.isArray(c))return new i("Invalid "+o+" `"+a+"` of type `"+s(c)+"` supplied to `"+n+"`, expected an array.");for(var u=0;u<c.length;u++){var f=e(c,u,n,o,a+"["+u+"]",R);if(f instanceof Error)return f}return null})},element:a(function(t,r,n,o,a){var c=t[r];return e(c)?null:new i("Invalid "+o+" `"+a+"` of type `"+s(c)+"` supplied to `"+n+"`, expected a single ReactElement.")}),elementType:a(function(e,t,r,n,o){var a=e[t];return P.isValidElementType(a)?null:new i("Invalid "+n+" `"+o+"` of type `"+s(a)+"` supplied to `"+r+"`, expected a single ReactElement type.")}),instanceOf:function(e){return a(function(t,r,n,o,a){var c;return t[r]instanceof e?null:new i("Invalid "+o+" `"+a+"` of type `"+((c=t[r]).constructor&&c.constructor.name?c.constructor.name:"<<anonymous>>")+"` supplied to `"+n+"`, expected instance of `"+(e.name||"<<anonymous>>")+"`.")})},node:a(function(e,t,r,n,o){return u(e[t])?null:new i("Invalid "+n+" `"+o+"` supplied to `"+r+"`, expected a ReactNode.")}),objectOf:function(e){return a(function(t,r,n,o,a){if("function"!=typeof e)return new i("Property `"+a+"` of component `"+n+"` has invalid PropType notation inside objectOf.");var c=t[r],u=s(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` of type `"+u+"` supplied to `"+n+"`, expected an object.");for(var f in c)if(W(c,f)){var l=e(c,f,n,o,a+"."+f,R);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,a,c){for(var u=t[r],s=0;s<e.length;s++)if(o(u,e[s]))return null;var l=JSON.stringify(e,function(e,t){return"symbol"===f(t)?String(t):t});return new i("Invalid "+a+" `"+c+"` of value `"+String(u)+"` supplied to `"+n+"`, expected one of "+l+".")}return a(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 a(function(t,r,n,o,a){for(var c=0;c<e.length;c++)if(null==(0,e[c])(t,r,n,o,a,R))return null;return new i("Invalid "+o+" `"+a+"` supplied to `"+n+"`.")})},shape:function(e){return a(function(t,r,n,o,a){var c=t[r],u=s(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` of type `"+u+"` supplied to `"+n+"`, expected `object`.");for(var f in e){var l=e[f];if(l){var p=l(c,f,n,o,a+"."+f,R);if(p)return p}}return null})},exact:function(e){return a(function(t,r,n,o,a){var c=t[r],u=s(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` of type `"+u+"` supplied to `"+n+"`, expected `object`.");var f=N({},t[r],e);for(var l in f){var p=e[l];if(!p)return new i("Invalid "+o+" `"+a+"` 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,a+"."+l,R);if(y)return y}return null})}};function o(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function i(e){this.message=e,this.stack=""}function a(e){function t(t,r,n,o,a,c,u){if(o=o||"<<anonymous>>",c=c||n,u!==R){var s=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 s.name="Invariant Violation",s}return null==r[n]?t?new i(null===r[n]?"The "+a+" `"+c+"` is marked as required in `"+o+"`, but its value is `null`.":"The "+a+" `"+c+"` is marked as required in `"+o+"`, but its value is `undefined`."):null:e(r,n,o,a,c)}process;var r=t.bind(null,!1);return r.isRequired=t.bind(null,!0),r}function c(e){return a(function(t,r,n,o,a,c){var u=t[r];return s(u)!==e?new i("Invalid "+o+" `"+a+"` of type `"+f(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,i=n.call(t);if(n!==t.entries){for(;!(o=i.next()).done;)if(!u(o.value))return!1}else for(;!(o=i.next()).done;){var a=o.value;if(a&&!u(a[1]))return!1}return!0;default:return!1}}function s(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 f(e){if(null==e)return""+e;var t=s(e);if("object"===t){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return t}function l(e){var t=f(e);switch(t){case"array":case"object":return"an "+t;case"boolean":case"date":case"regexp":return"a "+t;default:return t}}return i.prototype=Error.prototype,n.checkPropTypes=q,n.resetWarningCache=q.resetWarningCache,n.PropTypes=n,n}(P.isElement):function(){function e(e,t,r,n,o,i){if(i!==R){var a=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 a.name="Invariant Violation",a}}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}()});class Y extends e.Component{constructor(e){super(e),this.callback=()=>{if(this.timer){const{callback:e}=this.props;"function"==typeof e&&e(),this.start()}},this.start=()=>{this.stop();const{timeout:e}=this.props;this.timer=setTimeout(this.callback,e)},this.stop=()=>{this.timer&&clearTimeout(this.timer),this.timer=null},this.render=()=>!1,this.timer=null}componentDidMount(){const{enabled:e}=this.props;e&&this.start()}shouldComponentUpdate({timeout:e,callback:t,enabled:r}){const{timeout:n,callback:o,enabled:i}=this.props;return n!==e||o!==t||i!==r}componentDidUpdate({enabled:e,timeout:t}){const{timeout:r,enabled:n}=this.props;n===e&&r===t||(n?this.start():this.stop())}componentWillUnmount(){this.stop()}}Y.defaultProps={enabled:!1,timeout:1e3},Y.propTypes={callback:B.func.isRequired,enabled:B.bool,timeout:B.number};const H=()=>{};function G(e,o=1e3,{onFinish:i=H,autoStart:a=!0,immediate:c=!1,selfCorrecting:u=!0}={}){const s=t(),f=t(!1),l=t(null),p=t(e),y=r(()=>{const e=l.current||0;if(u){const t=Date.now()-e,r=1+(t>0?Math.floor(t/o):0);l.current=e+o*r,d(Math.max(o-t,1)),p.current(r)}else d(o),p.current()},[o]),d=r(e=>{void 0!==s.current&&clearTimeout(s.current),f.current?s.current=setTimeout(y,e):console.debug("Trying to set interval timeout on inactive timer, this is no-op and probably indicates bug in your code.")},[y,f]),m=r(()=>{const e=f.current;f.current=!0,null===l.current&&(l.current=Date.now()+o),c&&!e&&(l.current-=o,y()),d(o)},[y,o,c,d]),b=r((e=!0)=>{const t=f.current;void 0!==s.current&&clearTimeout(s.current),f.current=!1,s.current=void 0,l.current=null,t&&e&&i()},[i]),v=r(()=>f.current,[]);return n(()=>{p.current=e},[e]),n(()=>(a&&m(),b),[]),{start:m,stop:b,isActive:v}}export{Y as ReactInterval,G as useInterval}; //# sourceMappingURL=index.modern.js.map