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.6 kB
JavaScript
import e,{useRef as r,useCallback as t,useEffect as n}from"react";function o(e,r){return(o=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function i(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,c=a?Symbol.for("react.element"):60103,u=a?Symbol.for("react.portal"):60106,f=a?Symbol.for("react.fragment"):60107,s=a?Symbol.for("react.strict_mode"):60108,l=a?Symbol.for("react.profiler"):60114,p=a?Symbol.for("react.provider"):60109,y=a?Symbol.for("react.context"):60110,d=a?Symbol.for("react.async_mode"):60111,m=a?Symbol.for("react.concurrent_mode"):60111,b=a?Symbol.for("react.forward_ref"):60112,v=a?Symbol.for("react.suspense"):60113,h=a?Symbol.for("react.suspense_list"):60120,g=a?Symbol.for("react.memo"):60115,S=a?Symbol.for("react.lazy"):60116,O=a?Symbol.for("react.block"):60121,w=a?Symbol.for("react.fundamental"):60117,E=a?Symbol.for("react.responder"):60118,$=a?Symbol.for("react.scope"):60119;function j(e){if("object"==typeof e&&null!==e){var r=e.$$typeof;switch(r){case c:switch(e=e.type){case d:case m:case f:case l:case s:case v:return e;default:switch(e=e&&e.$$typeof){case y:case b:case S:case g:case p:return e;default:return r}}case u:return r}}}function x(e){return j(e)===m}var T={AsyncMode:d,ConcurrentMode:m,ContextConsumer:y,ContextProvider:p,Element:c,ForwardRef:b,Fragment:f,Lazy:S,Memo:g,Portal:u,Profiler:l,StrictMode:s,Suspense:v,isAsyncMode:function(e){return x(e)||j(e)===d},isConcurrentMode:x,isContextConsumer:function(e){return j(e)===y},isContextProvider:function(e){return j(e)===p},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===c},isForwardRef:function(e){return j(e)===b},isFragment:function(e){return j(e)===f},isLazy:function(e){return j(e)===S},isMemo:function(e){return j(e)===g},isPortal:function(e){return j(e)===u},isProfiler:function(e){return j(e)===l},isStrictMode:function(e){return j(e)===s},isSuspense:function(e){return j(e)===v},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===f||e===m||e===l||e===s||e===v||e===h||"object"==typeof e&&null!==e&&(e.$$typeof===S||e.$$typeof===g||e.$$typeof===p||e.$$typeof===y||e.$$typeof===b||e.$$typeof===w||e.$$typeof===E||e.$$typeof===$||e.$$typeof===O)},typeOf:j},P=i(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,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,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 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 r}}case n:return r}}}var O=s,w=u,E=c,$=t,j=l,x=o,T=m,P=d,_=n,k=a,C=i,I=p,N=!1;function R(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=_,r.Profiler=k,r.StrictMode=C,r.Suspense=I,r.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)===f},r.isConcurrentMode=R,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)===a},r.isStrictMode=function(e){return S(e)===i},r.isSuspense=function(e){return S(e)===p},r.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===o||e===s||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)},r.typeOf=S}()}),_=i(function(e){e.exports="production"===process.env.NODE_ENV?T:P}),k=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,I=Object.prototype.propertyIsEnumerable;function N(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=N(e),i=1;i<arguments.length;i++){for(var a in t=Object(arguments[i]))C.call(t,a)&&(o[a]=t[a]);if(k){n=k(t);for(var c=0;c<n.length;c++)I.call(t,n[c])&&(o[n[c]]=t[n[c]])}}return o},M="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",A=function(){};if("production"!==process.env.NODE_ENV){var D=M,V={},F=Function.call.bind(Object.prototype.hasOwnProperty);A=function(e){var r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}}function q(e,r,t,n,o){if("production"!==process.env.NODE_ENV)for(var i in e)if(F(e,i)){var a;try{if("function"!=typeof e[i]){var c=Error((n||"React class")+": "+t+" 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](r,i,n,t,null,D)}catch(e){a=e}if(!a||a instanceof Error||A((n||"React class")+": type specification of "+t+" `"+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 V)){V[a.message]=!0;var u=o?o():"";A("Failed "+t+" type: "+a.message+(null!=u?u:""))}}}q.resetWarningCache=function(){"production"!==process.env.NODE_ENV&&(V={})};var W=q,z=Function.call.bind(Object.prototype.hasOwnProperty),U=function(){};function L(){return null}function J(){}function B(){}"production"!==process.env.NODE_ENV&&(U=function(e){var r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}),B.resetWarningCache=J;var Y=i(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:a(L),arrayOf:function(e){return a(function(r,t,n,o,a){if("function"!=typeof e)return new i("Property `"+a+"` of component `"+n+"` has invalid PropType notation inside arrayOf.");var c=r[t];if(!Array.isArray(c))return new i("Invalid "+o+" `"+a+"` 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,a+"["+u+"]",M);if(s instanceof Error)return s}return null})},element:a(function(r,t,n,o,a){var c=r[t];return e(c)?null:new i("Invalid "+o+" `"+a+"` of type `"+f(c)+"` supplied to `"+n+"`, expected a single ReactElement.")}),elementType:a(function(e,r,t,n,o){var a=e[r];return _.isValidElementType(a)?null:new i("Invalid "+n+" `"+o+"` of type `"+f(a)+"` supplied to `"+t+"`, expected a single ReactElement type.")}),instanceOf:function(e){return a(function(r,t,n,o,a){var c;return r[t]instanceof e?null:new i("Invalid "+o+" `"+a+"` of type `"+((c=r[t]).constructor&&c.constructor.name?c.constructor.name:"<<anonymous>>")+"` supplied to `"+n+"`, expected instance of `"+(e.name||"<<anonymous>>")+"`.")})},node:a(function(e,r,t,n,o){return u(e[r])?null:new i("Invalid "+n+" `"+o+"` supplied to `"+t+"`, expected a ReactNode.")}),objectOf:function(e){return a(function(r,t,n,o,a){if("function"!=typeof e)return new i("Property `"+a+"` of component `"+n+"` has invalid PropType notation inside objectOf.");var c=r[t],u=f(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` of type `"+u+"` supplied to `"+n+"`, expected an object.");for(var s in c)if(z(c,s)){var l=e(c,s,n,o,a+"."+s,M);if(l instanceof Error)return l}return null})},oneOf:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&U(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."),L;function r(r,t,n,a,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 i("Invalid "+a+" `"+c+"` of value `"+String(u)+"` supplied to `"+n+"`, expected one of "+l+".")}return a(r)},oneOfType:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&U("Invalid argument supplied to oneOfType, expected an instance of array."),L;for(var r=0;r<e.length;r++){var t=e[r];if("function"!=typeof t)return U("Invalid argument supplied to oneOfType. Expected an array of check functions, but received "+l(t)+" at index "+r+"."),L}return a(function(r,t,n,o,a){for(var c=0;c<e.length;c++)if(null==(0,e[c])(r,t,n,o,a,M))return null;return new i("Invalid "+o+" `"+a+"` supplied to `"+n+"`.")})},shape:function(e){return a(function(r,t,n,o,a){var c=r[t],u=f(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` 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,a+"."+s,M);if(p)return p}}return null})},exact:function(e){return a(function(r,t,n,o,a){var c=r[t],u=f(c);if("object"!==u)return new i("Invalid "+o+" `"+a+"` 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 i("Invalid "+o+" `"+a+"` 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,a+"."+l,M);if(y)return y}return null})}};function o(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}function i(e){this.message=e,this.stack=""}function a(e){function r(r,t,n,o,a,c,u){if(o=o||"<<anonymous>>",c=c||n,u!==M){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 i(null===t[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(t,n,o,a,c)}process;var t=r.bind(null,!1);return t.isRequired=r.bind(null,!0),t}function c(e){return a(function(r,t,n,o,a,c){var u=r[t];return f(u)!==e?new i("Invalid "+o+" `"+a+"` 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,i=n.call(r);if(n!==r.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 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 i.prototype=Error.prototype,n.checkPropTypes=W,n.resetWarningCache=W.resetWarningCache,n.PropTypes=n,n}(_.isElement):function(){function e(e,r,t,n,o,i){if(i!==M){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 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:B,resetWarningCache:J};return t.PropTypes=t,t}()}),H=function(e){var r,t;function n(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=n).prototype=Object.create(t.prototype),r.prototype.constructor=r,o(r,t);var i=n.prototype;return i.componentDidMount=function(){this.props.enabled&&this.start()},i.shouldComponentUpdate=function(e){var r=this.props;return r.timeout!==e.timeout||r.callback!==e.callback||r.enabled!==e.enabled},i.componentDidUpdate=function(e){var r=this.props,t=r.enabled;t===e.enabled&&r.timeout===e.timeout||(t?this.start():this.stop())},i.componentWillUnmount=function(){this.stop()},n}(e.Component);H.defaultProps={enabled:!1,timeout:1e3},H.propTypes={callback:Y.func.isRequired,enabled:Y.bool,timeout:Y.number};var G=function(){};function K(e,o,i){void 0===o&&(o=1e3);var a=void 0===i?{}:i,c=a.onFinish,u=void 0===c?G:c,f=a.autoStart,s=void 0===f||f,l=a.immediate,p=void 0!==l&&l,y=a.selfCorrecting,d=void 0===y||y,m=r(),b=r(!1),v=r(null),h=r(e),g=t(function(){var e=v.current||0;if(d){var r=Date.now()-e,t=1+(r>0?Math.floor(r/o):0);v.current=e+o*t,S(Math.max(o-r,1)),h.current(t)}else S(o),h.current()},[o]),S=t(function(e){void 0!==m.current&&clearTimeout(m.current),b.current?m.current=setTimeout(g,e):console.debug("Trying to set interval timeout on inactive timer, this is no-op and probably indicates bug in your code.")},[g,b]),O=t(function(){var e=b.current;b.current=!0,null===v.current&&(v.current=Date.now()+o),p&&!e&&(v.current-=o,g()),S(o)},[g,o,p,S]),w=t(function(e){void 0===e&&(e=!0);var r=b.current;void 0!==m.current&&clearTimeout(m.current),b.current=!1,m.current=void 0,v.current=null,r&&e&&u()},[u]),E=t(function(){return b.current},[]);return n(function(){h.current=e},[e]),n(function(){return s&&O(),w},[]),{start:O,stop:w,isActive:E}}export{H as ReactInterval,K as useInterval};
//# sourceMappingURL=index.module.js.map