react-idle-timer
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
var lt=Object.create,ie=Object.defineProperty;var mt=Object.getOwnPropertyDescriptor;var dt=Object.getOwnPropertyNames;var pt=Object.getPrototypeOf,ft=Object.prototype.hasOwnProperty;var Ae=t=>ie(t,"__esModule",{value:!0});var Tt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),vt=(t,e)=>{for(var n in e)ie(t,n,{get:e[n],enumerable:!0})},ht=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of dt(e))!ft.call(t,r)&&r!=="default"&&ie(t,r,{get:()=>e[r],enumerable:!(n=mt(e,r))||n.enumerable});return t},se=t=>ht(Ae(ie(t!=null?lt(pt(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Ce=Tt((ae,De)=>{(function(t,e){typeof ae=="object"&&typeof De!="undefined"?e(ae):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.fastUniqueNumbers={}))})(ae,function(t){"use strict";var e=function(l){return function(f){var p=l(f);return f.add(p),p}},n=function(l){return function(f,p){return l.set(f,p),p}},r=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,m=536870912,T=m*2,F=function(l,f){return function(p){var M=f.get(p),y=M===void 0?p.size:M<T?M+1:0;if(!p.has(y))return l(p,y);if(p.size<m){for(;p.has(y);)y=Math.floor(Math.random()*T);return l(p,y)}if(p.size>r)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;p.has(y);)y=Math.floor(Math.random()*r);return l(p,y)}},j=new WeakMap,Z=n(j),u=F(Z,j),o=e(u);t.addUniqueNumber=o,t.generateUniqueNumber=u,Object.defineProperty(t,"__esModule",{value:!0})})});Ae(exports);vt(exports,{IdleTimerConsumer:()=>et,IdleTimerContext:()=>Q,IdleTimerProvider:()=>Ze,createMocks:()=>je,useIdleTimer:()=>G,useIdleTimerContext:()=>tt,withIdleTimer:()=>Oe,workerTimers:()=>Ve});function Oe(t){return function(n){let r={...n};!r.onPrompt&&t.prototype.onPrompt&&(r.onPrompt=()=>{t.prototype.onPrompt()}),!r.onIdle&&t.prototype.onIdle&&(r.onIdle=()=>{t.prototype.onIdle()}),!r.onActive&&t.prototype.onActive&&(r.onActive=T=>{t.prototype.onActive(T)}),!r.onAction&&t.prototype.onAction&&(r.onAction=T=>{t.prototype.onAction(T)});let m=G(r);return React.createElement(t,{...n,...m})}}var i=se(require("react"));var Y=se(Ce());var Ne=t=>t.method!==void 0&&t.method==="call";var _e=t=>t.error===null&&typeof t.id=="number";var Ue=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,m=new Worker(t);return m.addEventListener("message",({data:u})=>{if(Ne(u)){let{params:{timerId:o,timerType:c}}=u;if(c==="interval"){let l=e.get(o);if(typeof l=="number"){let f=r.get(l);if(f===void 0||f.timerId!==o||f.timerType!==c)throw new Error("The timer is in an undefined state.")}else if(typeof l!="undefined")l();else throw new Error("The timer is in an undefined state.")}else if(c==="timeout"){let l=n.get(o);if(typeof l=="number"){let f=r.get(l);if(f===void 0||f.timerId!==o||f.timerType!==c)throw new Error("The timer is in an undefined state.")}else if(typeof l!="undefined")l(),n.delete(o);else throw new Error("The timer is in an undefined state.")}}else if(_e(u)){let{id:o}=u,c=r.get(o);if(c===void 0)throw new Error("The timer is in an undefined state.");let{timerId:l,timerType:f}=c;r.delete(o),f==="interval"?e.delete(l):n.delete(l)}else{let{error:{message:o}}=u;throw new Error(o)}}),{clearInterval:u=>{let o=(0,Y.generateUniqueNumber)(r);r.set(o,{timerId:u,timerType:"interval"}),e.set(u,o),m.postMessage({id:o,method:"clear",params:{timerId:u,timerType:"interval"}})},clearTimeout:u=>{let o=(0,Y.generateUniqueNumber)(r);r.set(o,{timerId:u,timerType:"timeout"}),n.set(u,o),m.postMessage({id:o,method:"clear",params:{timerId:u,timerType:"timeout"}})},setInterval:(u,o)=>{let c=(0,Y.generateUniqueNumber)(e);return e.set(c,()=>{u(),typeof e.get(c)=="function"&&m.postMessage({id:null,method:"set",params:{delay:o,now:performance.now(),timerId:c,timerType:"interval"}})}),m.postMessage({id:null,method:"set",params:{delay:o,now:performance.now(),timerId:c,timerType:"interval"}}),c},setTimeout:(u,o)=>{let c=(0,Y.generateUniqueNumber)(n);return n.set(c,u),m.postMessage({id:null,method:"set",params:{delay:o,now:performance.now(),timerId:c,timerType:"timeout"}}),c}}};var K=null,Fe=(t,e)=>()=>{if(K!==null)return K;let n=new Blob([e],{type:"application/javascript; charset=utf-8"}),r=URL.createObjectURL(n);return K=t(r),K.setTimeout(()=>URL.revokeObjectURL(r),0),K};var Be=`(()=>{"use strict";const e=new Map,t=new Map,r=(e,t)=>{let r,o;const i=performance.now();r=i,o=e-Math.max(0,i-t);return{expected:r+o,remainingDelay:o}},o=(e,t,r,i)=>{const s=performance.now();s>r?postMessage({id:null,method:"call",params:{timerId:t,timerType:i}}):e.set(t,setTimeout(o,r-s,e,t,r,i))};addEventListener("message",(i=>{let{data:s}=i;try{if("clear"===s.method){const{id:r,params:{timerId:o,timerType:i}}=s;if("interval"===i)(t=>{const r=e.get(t);if(void 0===r)throw new Error('There is no interval scheduled with the given id "'.concat(t,'".'));clearTimeout(r),e.delete(t)})(o),postMessage({error:null,id:r});else{if("timeout"!==i)throw new Error('The given type "'.concat(i,'" is not supported'));(e=>{const r=t.get(e);if(void 0===r)throw new Error('There is no timeout scheduled with the given id "'.concat(e,'".'));clearTimeout(r),t.delete(e)})(o),postMessage({error:null,id:r})}}else{if("set"!==s.method)throw new Error('The given method "'.concat(s.method,'" is not supported'));{const{params:{delay:i,now:n,timerId:a,timerType:d}}=s;if("interval"===d)((t,i,s)=>{const{expected:n,remainingDelay:a}=r(t,s);e.set(i,setTimeout(o,a,e,i,n,"interval"))})(i,a,n);else{if("timeout"!==d)throw new Error('The given type "'.concat(d,'" is not supported'));((e,i,s)=>{const{expected:n,remainingDelay:a}=r(e,s);t.set(i,setTimeout(o,a,t,i,n,"timeout"))})(i,a,n)}}}}catch(e){postMessage({error:{message:e.message},id:s.id,result:null})}}))})();`;var ue=Fe(Ue,Be),We=t=>ue().clearInterval(t),He=t=>ue().clearTimeout(t),Ge=(t,e)=>ue().setInterval(t,e),qe=(t,e)=>ue().setTimeout(t,e);var E=(typeof window=="undefined"?"undefined":typeof window)=="object";var b={setTimeout:E?setTimeout.bind(window):setTimeout,clearTimeout:E?clearTimeout.bind(window):clearTimeout,setInterval:E?setInterval.bind(window):setInterval,clearInterval:E?clearInterval.bind(window):clearInterval},Ve={setTimeout:qe,clearTimeout:He,setInterval:Ge,clearInterval:We};function je(){b.setTimeout=setTimeout,b.clearTimeout=clearTimeout,b.setInterval=setInterval,b.clearInterval=clearInterval}function Je(t){b.setTimeout=t.setTimeout,b.clearTimeout=t.clearTimeout,b.setInterval=t.setInterval,b.clearInterval=t.clearInterval}var q={},ze=class{constructor(e){this.closed=!1;this.mc=new MessageChannel;this.name=e,q[e]=q[e]||[],q[e].push(this),this.mc.port1.start(),this.mc.port2.start(),this.onStorage=this.onStorage.bind(this),window.addEventListener("storage",this.onStorage)}onStorage(e){if(e.storageArea!==window.localStorage||e.key.substring(0,this.name.length)!==this.name||e.newValue===null)return;let n=JSON.parse(e.newValue);this.mc.port2.postMessage(n)}postMessage(e){if(this.closed)throw new Error("InvalidStateError");let n=JSON.stringify(e),r=`${this.name}:${String(Date.now())}${String(Math.random())}`;window.localStorage.setItem(r,n),b.setTimeout(()=>{window.localStorage.removeItem(r)},500),q[this.name].forEach(m=>{m!==this&&m.mc.port2.postMessage(JSON.parse(n))})}close(){if(this.closed)return;this.closed=!0,this.mc.port1.close(),this.mc.port2.close(),window.removeEventListener("storage",this.onStorage);let e=q[this.name].indexOf(this);q[this.name].splice(e,1)}get onmessage(){return this.mc.port1.onmessage}set onmessage(e){this.mc.port1.onmessage=e}get onmessageerror(){return this.mc.port1.onmessageerror}set onmessageerror(e){this.mc.port1.onmessageerror=e}addEventListener(e,n){return this.mc.port1.addEventListener(e,n)}removeEventListener(e,n){return this.mc.port1.removeEventListener(e,n)}dispatchEvent(e){return this.mc.port1.dispatchEvent(e)}},Xe=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:ze;var h=()=>E?performance.now():Date.now();function $e(){return h().toString(36).substring(2)}var d;(function(o){o[o.REGISTER=0]="REGISTER",o[o.DEREGISTER=1]="DEREGISTER",o[o.IDLE=2]="IDLE",o[o.ACTIVE=3]="ACTIVE",o[o.PROMPT=4]="PROMPT",o[o.START=5]="START",o[o.RESET=6]="RESET",o[o.PAUSE=7]="PAUSE",o[o.RESUME=8]="RESUME",o[o.MESSAGE=9]="MESSAGE"})(d||(d={}));var S;(function(r){r[r.PROMPTED=0]="PROMPTED",r[r.ACTIVE=1]="ACTIVE",r[r.IDLE=2]="IDLE"})(S||(S={}));var he=class{constructor(e){this.token=$e();this.registry=new Map;this.allIdle=!1;let{channelName:n}=e;this.options=e,this.channel=new Xe(n),this.registry.set(this.token,1),this.channel.addEventListener("message",r=>{let{action:m,token:T,data:F}=r.data;switch(m){case 0:this.registry.set(T,1);break;case 1:this.registry.delete(T);break;case 2:this.idle(T);break;case 3:this.active(T);break;case 4:this.prompt(T);break;case 9:this.options.onMessage(F);break;case 5:this.options.start(!0);break;case 6:this.options.reset(!0);break;case 7:this.options.pause(!0);break;case 8:this.options.resume(!0);break}}),this.send(0)}prompt(e=this.token){this.registry.set(e,0);let n=[...this.registry.values()].every(r=>r===0);e===this.token&&this.send(4),n&&this.options.onPrompt()}idle(e=this.token){this.registry.set(e,2);let n=[...this.registry.values()].every(r=>r===2);e===this.token&&this.send(2),!this.allIdle&&n&&(this.allIdle=!0,this.options.onIdle())}active(e=this.token){this.registry.set(e,1);let n=[...this.registry.values()].some(r=>r===1);e===this.token&&this.send(3),this.allIdle&&n&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:6,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:9,token:this.token,data:e})}catch{}}send(e){try{this.channel.postMessage({action:e,token:this.token})}catch{}}close(){this.send(1),this.channel.close()}};var Ye=E?document:null,Ke=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];var ce=se(require("react"));function C(t){let e=(0,ce.useRef)(t);return(0,ce.useEffect)(()=>{e.current=t},[t]),e}function Qe(t,e){let n;function r(...m){n&&clearTimeout(n),n=setTimeout(()=>{t(...m),n=null},e)}return r.cancel=function(){clearTimeout(n)},r}function le(t,e){let n=0;return function(...r){let m=new Date().getTime();if(!(m-n<e))return n=m,t(...r)}}function G({timeout:t=1e3*60*20,promptTimeout:e=0,element:n=Ye,events:r=Ke,timers:m=void 0,immediateEvents:T=[],onPrompt:F=()=>{},onIdle:j=()=>{},onActive:Z=()=>{},onAction:u=()=>{},onMessage:o=()=>{},debounce:c=0,throttle:l=0,eventsThrottle:f=200,startOnMount:p=!0,startManually:M=!1,stopOnIdle:y=!1,crossTab:B=!1,syncTimers:J=0}={}){let z=(0,i.useRef)(h()),Ie=(0,i.useRef)(Date.now()),me=(0,i.useRef)(h()),L=(0,i.useRef)(null),A=(0,i.useRef)(null),X=(0,i.useRef)(0),v=(0,i.useRef)(!0),k=(0,i.useRef)(!1),N=(0,i.useRef)(!0),$=(0,i.useRef)(!1),I=(0,i.useRef)(!1),g=(0,i.useRef)(0),R=(0,i.useRef)(0),O=(0,i.useRef)(null),a=(0,i.useRef)(null),D=C(t),ee=C(e),rt=C(y),Ee=(0,i.useRef)(T),de=(0,i.useRef)(n),pe=(0,i.useRef)([...new Set([...r,...T]).values()]),be=C(j),te=C(Z),ge=C(F),re=C(o),w=(0,i.useRef)();(0,i.useEffect)(()=>{w.current&&w.current.cancel&&w.current.cancel(),c>0?w.current=Qe(u,c):l>0?w.current=le(u,l):w.current=u},[u,l,c]);let ye=(0,i.useRef)();(0,i.useEffect)(()=>{B&&J&&(ye.current=le(()=>{a.current.sync()},J))},[B,J]);let P=()=>{O.current!==null&&(b.clearTimeout(O.current),O.current=null)},W=(s,x=!0)=>{P(),O.current=b.setTimeout(fe,s||D.current),x&&(A.current=h())},we=s=>{g.current=0,R.current=h(),I.current=!0,W(ee.current,!1),ge.current(s)},xe=()=>{v.current=!0,L.current=h(),rt.current?(P(),U()):I.current&&(R.current=0,I.current=!1),be.current()},Me=s=>{I.current=!1,R.current=0,v.current=!1,X.current+=h()-L.current,_(),W(),te.current(s)},fe=s=>{if(!v.current){if(ee.current>0&&!I.current){a.current?a.current.prompt():we(s);return}a.current?a.current.idle():xe();return}a.current?a.current.active():Me(s)},Se=s=>{if(w.current(s),I.current)return;B&&J&&ye.current(),P();let x=h()-A.current;if((v.current&&!y||!v.current&&x>D.current)&&fe(s),k.current=!1,g.current=0,R.current=0,!v.current&&Ee.current.includes(s.type)){fe(s);return}W(),a.current&&a.current.active()},ne=(0,i.useRef)();(0,i.useEffect)(()=>{let s=$.current;s&&U(),f>0?ne.current=le(Se,f):ne.current=Se,s&&_()},[f,B,J]);let _=()=>{!E||$.current||(pe.current.forEach(s=>{de.current.addEventListener(s,ne.current,{capture:!0,passive:!0})}),$.current=!0)},U=(s=!1)=>{!E||($.current||s)&&(pe.current.forEach(x=>{de.current.removeEventListener(x,ne.current,{capture:!0})}),$.current=!1)},H=(0,i.useCallback)(s=>{P(),_(),I.current&&te.current(),v.current=!1,I.current=!1,k.current=!1,g.current=0,R.current=0,a.current&&!s&&(a.current.active(),a.current.send(d.START)),W()},[O,v,D,a]),oe=(0,i.useCallback)(s=>{P(),_(),(v.current||I.current)&&(te.current(),a.current&&a.current.active()),v.current=!1,I.current=!1,k.current=!1,g.current=0,R.current=0,me.current=h(),a.current&&(a.current.allIdle=!1,s||a.current.send(d.RESET)),W()},[O,v,D,a]),Te=(0,i.useCallback)((s=!1)=>k.current?!1:(g.current=ke(),k.current=!0,U(),P(),a.current&&(s||a.current.send(d.PAUSE)),!0),[O,a]),ve=(0,i.useCallback)((s=!1)=>k.current?(k.current=!1,I.current||_(),v.current||W(g.current),R.current&&(R.current=h()),a.current&&(s||a.current.send(d.RESUME)),!0):!1,[O,D,g,a]),nt=(0,i.useCallback)((s,x)=>{a.current?(x&&re.current(s),a.current.message(s)):x&&re.current(s)},[o]),ot=(0,i.useCallback)(()=>v.current,[v]),it=(0,i.useCallback)(()=>I.current,[I]),ke=(0,i.useCallback)(()=>{if(k.current)return g.current;let s=g.current?g.current:I.current?ee.current:D.current,x=A.current?I.current?h()-R.current:h()-A.current:0,Le=Math.ceil(s-x);return Le<0?0:Math.abs(Le)},[D,ee,I,g,A]),st=(0,i.useCallback)(()=>Math.round(h()-me.current),[me]),Re=(0,i.useCallback)(()=>Math.round(h()-z.current),[z]),at=(0,i.useCallback)(()=>L.current?new Date(Ie.current-z.current+L.current):null,[L]),ut=(0,i.useCallback)(()=>A.current?new Date(Ie.current-z.current+A.current):null,[A]),Pe=(0,i.useCallback)(()=>v.current?Math.round(h()-L.current+X.current):Math.round(X.current),[L,X]),ct=(0,i.useCallback)(()=>{let s=Math.round(Re()-Pe());return s>=0?s:0},[z,L,X]);return(0,i.useEffect)(()=>{if(c>0&&l>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");m&&Je(m);let s=()=>{a.current&&a.current.close(),w.current.cancel&&w.current.cancel(),P(),U(!0)};return E&&window.addEventListener("beforeunload",s),()=>{E&&window.removeEventListener("beforeunload",s),a.current&&a.current.close(),w.current.cancel&&w.current.cancel(),P(),U(!0)}},[]),(0,i.useEffect)(()=>{a.current&&a.current.close(),B?a.current=new he({channelName:"idle-timer",onPrompt:()=>{we()},onIdle:()=>{xe()},onActive:()=>{Me()},onMessage:re.current,start:H,reset:oe,pause:Te,resume:ve}):a.current=null},[B,ge,be,te,re,H,oe,Te,ve]),(0,i.useEffect)(()=>{P(),U(!0),N.current||(k.current=!0,v.current=!0,g.current=0),!M&&(p?H():_())},[M,p,N]),(0,i.useEffect)(()=>{if(!N.current){let s=[...new Set([...r,...T]).values()];if(U(),pe.current=s,de.current=n,Ee.current=T,M)return;p?H():_()}},[n,JSON.stringify(r),JSON.stringify(T),N,M,p]),(0,i.useEffect)(()=>{if(N.current)N.current=!1;else{if(D.current=t,M)return;v.current?oe():H()}},[t,M,N,v]),{message:nt,start:H,reset:oe,pause:Te,resume:ve,isIdle:ot,isPrompted:it,getRemainingTime:ke,getElapsedTime:st,getTotalElapsedTime:Re,getLastIdleTime:at,getLastActiveTime:ut,getTotalIdleTime:Pe,getTotalActiveTime:ct}}var V=se(require("react"));var Q=(0,V.createContext)(null);function Ze(t){let e=G(t);return V.default.createElement(Q.Provider,{value:e},t.children)}var et=Q.Consumer;function tt(){return(0,V.useContext)(Q)} | ||
var ft=Object.create,ie=Object.defineProperty;var ht=Object.getOwnPropertyDescriptor;var vt=Object.getOwnPropertyNames;var Tt=Object.getPrototypeOf,It=Object.prototype.hasOwnProperty;var Ce=t=>ie(t,"__esModule",{value:!0});var Et=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),bt=(t,e)=>{for(var n in e)ie(t,n,{get:e[n],enumerable:!0})},gt=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of vt(e))!It.call(t,r)&&r!=="default"&&ie(t,r,{get:()=>e[r],enumerable:!(n=ht(e,r))||n.enumerable});return t},se=t=>gt(Ce(ie(t!=null?ft(Tt(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Fe=Et((ae,_e)=>{(function(t,e){typeof ae=="object"&&typeof _e!="undefined"?e(ae):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.fastUniqueNumbers={}))})(ae,function(t){"use strict";var e=function(f){return function(o){var h=f(o);return o.add(h),h}},n=function(f){return function(o,h){return f.set(o,h),h}},r=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,u=536870912,m=u*2,M=function(f,o){return function(h){var k=o.get(h),w=k===void 0?h.size:k<m?k+1:0;if(!h.has(w))return f(h,w);if(h.size<u){for(;h.has(w);)w=Math.floor(Math.random()*m);return f(h,w)}if(h.size>r)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;h.has(w);)w=Math.floor(Math.random()*r);return f(h,w)}},_=new WeakMap,Z=n(_),d=M(Z,_),c=e(d);t.addUniqueNumber=c,t.generateUniqueNumber=d,Object.defineProperty(t,"__esModule",{value:!0})})});Ce(exports);bt(exports,{IdleTimerConsumer:()=>nt,IdleTimerContext:()=>Q,IdleTimerProvider:()=>rt,createMocks:()=>ze,useIdleTimer:()=>q,useIdleTimerContext:()=>ot,withIdleTimer:()=>Ne,workerTimers:()=>Je});function Ne(t){return function(n){let r={...n};!r.onPrompt&&t.prototype.onPrompt&&(r.onPrompt=()=>{t.prototype.onPrompt()}),!r.onIdle&&t.prototype.onIdle&&(r.onIdle=()=>{t.prototype.onIdle()}),!r.onActive&&t.prototype.onActive&&(r.onActive=m=>{t.prototype.onActive(m)}),!r.onAction&&t.prototype.onAction&&(r.onAction=m=>{t.prototype.onAction(m)});let u=q(r);return React.createElement(t,{...n,...u})}}var i=se(require("react"));var $=se(Fe());var Ue=t=>t.method!==void 0&&t.method==="call";var Be=t=>t.error===null&&typeof t.id=="number";var We=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,u=new Worker(t);return u.addEventListener("message",({data:d})=>{if(Ue(d)){let{params:{timerId:c,timerType:p}}=d;if(p==="interval"){let f=e.get(c);if(typeof f=="number"){let o=r.get(f);if(o===void 0||o.timerId!==c||o.timerType!==p)throw new Error("The timer is in an undefined state.")}else if(typeof f!="undefined")f();else throw new Error("The timer is in an undefined state.")}else if(p==="timeout"){let f=n.get(c);if(typeof f=="number"){let o=r.get(f);if(o===void 0||o.timerId!==c||o.timerType!==p)throw new Error("The timer is in an undefined state.")}else if(typeof f!="undefined")f(),n.delete(c);else throw new Error("The timer is in an undefined state.")}}else if(Be(d)){let{id:c}=d,p=r.get(c);if(p===void 0)throw new Error("The timer is in an undefined state.");let{timerId:f,timerType:o}=p;r.delete(c),o==="interval"?e.delete(f):n.delete(f)}else{let{error:{message:c}}=d;throw new Error(c)}}),{clearInterval:d=>{let c=(0,$.generateUniqueNumber)(r);r.set(c,{timerId:d,timerType:"interval"}),e.set(d,c),u.postMessage({id:c,method:"clear",params:{timerId:d,timerType:"interval"}})},clearTimeout:d=>{let c=(0,$.generateUniqueNumber)(r);r.set(c,{timerId:d,timerType:"timeout"}),n.set(d,c),u.postMessage({id:c,method:"clear",params:{timerId:d,timerType:"timeout"}})},setInterval:(d,c)=>{let p=(0,$.generateUniqueNumber)(e);return e.set(p,()=>{d(),typeof e.get(p)=="function"&&u.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:p,timerType:"interval"}})}),u.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:p,timerType:"interval"}}),p},setTimeout:(d,c)=>{let p=(0,$.generateUniqueNumber)(n);return n.set(p,d),u.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:p,timerType:"timeout"}}),p}}};var K=null,He=(t,e)=>()=>{if(K!==null)return K;let n=new Blob([e],{type:"application/javascript; charset=utf-8"}),r=URL.createObjectURL(n);return K=t(r),K.setTimeout(()=>URL.revokeObjectURL(r),0),K};var Ge=`(()=>{"use strict";const e=new Map,t=new Map,r=(e,t)=>{let r,o;const i=performance.now();r=i,o=e-Math.max(0,i-t);return{expected:r+o,remainingDelay:o}},o=(e,t,r,i)=>{const s=performance.now();s>r?postMessage({id:null,method:"call",params:{timerId:t,timerType:i}}):e.set(t,setTimeout(o,r-s,e,t,r,i))};addEventListener("message",(i=>{let{data:s}=i;try{if("clear"===s.method){const{id:r,params:{timerId:o,timerType:i}}=s;if("interval"===i)(t=>{const r=e.get(t);if(void 0===r)throw new Error('There is no interval scheduled with the given id "'.concat(t,'".'));clearTimeout(r),e.delete(t)})(o),postMessage({error:null,id:r});else{if("timeout"!==i)throw new Error('The given type "'.concat(i,'" is not supported'));(e=>{const r=t.get(e);if(void 0===r)throw new Error('There is no timeout scheduled with the given id "'.concat(e,'".'));clearTimeout(r),t.delete(e)})(o),postMessage({error:null,id:r})}}else{if("set"!==s.method)throw new Error('The given method "'.concat(s.method,'" is not supported'));{const{params:{delay:i,now:n,timerId:a,timerType:d}}=s;if("interval"===d)((t,i,s)=>{const{expected:n,remainingDelay:a}=r(t,s);e.set(i,setTimeout(o,a,e,i,n,"interval"))})(i,a,n);else{if("timeout"!==d)throw new Error('The given type "'.concat(d,'" is not supported'));((e,i,s)=>{const{expected:n,remainingDelay:a}=r(e,s);t.set(i,setTimeout(o,a,t,i,n,"timeout"))})(i,a,n)}}}}catch(e){postMessage({error:{message:e.message},id:s.id,result:null})}}))})();`;var le=He(We,Ge),qe=t=>le().clearInterval(t),Ve=t=>le().clearTimeout(t),je=(t,e)=>le().setInterval(t,e),Ye=(t,e)=>le().setTimeout(t,e);var b=(typeof window=="undefined"?"undefined":typeof window)=="object";var v={setTimeout:b?setTimeout.bind(window):setTimeout,clearTimeout:b?clearTimeout.bind(window):clearTimeout,setInterval:b?setInterval.bind(window):setInterval,clearInterval:b?clearInterval.bind(window):clearInterval},Je={setTimeout:Ye,clearTimeout:Ve,setInterval:je,clearInterval:qe};function ze(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function Xe(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var V={},$e=class{constructor(e){this.closed=!1;this.mc=new MessageChannel;this.name=e,V[e]=V[e]||[],V[e].push(this),this.mc.port1.start(),this.mc.port2.start(),this.onStorage=this.onStorage.bind(this),window.addEventListener("storage",this.onStorage)}onStorage(e){if(e.storageArea!==window.localStorage||e.key.substring(0,this.name.length)!==this.name||e.newValue===null)return;let n=JSON.parse(e.newValue);this.mc.port2.postMessage(n)}postMessage(e){if(this.closed)throw new Error("InvalidStateError");let n=JSON.stringify(e),r=`${this.name}:${String(Date.now())}${String(Math.random())}`;window.localStorage.setItem(r,n),v.setTimeout(()=>{window.localStorage.removeItem(r)},500),V[this.name].forEach(u=>{u!==this&&u.mc.port2.postMessage(JSON.parse(n))})}close(){if(this.closed)return;this.closed=!0,this.mc.port1.close(),this.mc.port2.close(),window.removeEventListener("storage",this.onStorage);let e=V[this.name].indexOf(this);V[this.name].splice(e,1)}get onmessage(){return this.mc.port1.onmessage}set onmessage(e){this.mc.port1.onmessage=e}get onmessageerror(){return this.mc.port1.onmessageerror}set onmessageerror(e){this.mc.port1.onmessageerror=e}addEventListener(e,n){return this.mc.port1.addEventListener(e,n)}removeEventListener(e,n){return this.mc.port1.removeEventListener(e,n)}dispatchEvent(e){return this.mc.port1.dispatchEvent(e)}},Ke=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:$e;function Qe(t=0){return new Promise(e=>v.setTimeout(e,t))}var I=()=>b?performance.now():Date.now();function ue(){return I().toString(36).substring(2)}var l;(function(o){o[o.APPLY=0]="APPLY",o[o.TELL=1]="TELL",o[o.CLOSE=2]="CLOSE",o[o.REGISTER=3]="REGISTER",o[o.DEREGISTER=4]="DEREGISTER",o[o.IDLE=5]="IDLE",o[o.ACTIVE=6]="ACTIVE",o[o.PROMPT=7]="PROMPT",o[o.START=8]="START",o[o.RESET=9]="RESET",o[o.PAUSE=10]="PAUSE",o[o.RESUME=11]="RESUME",o[o.MESSAGE=12]="MESSAGE"})(l||(l={}));var Ie=class{constructor(e,n){this.token=ue();this.isLeader=!1;this.isDead=!1;this.isApplying=!1;this.reApply=!1;this.intervals=[];this.listeners=[];this.channel=e,this.options=n,this.apply=this.apply.bind(this),this.awaitLeadership=this.awaitLeadership.bind(this),this.sendAction=this.sendAction.bind(this)}async apply(){if(this.isLeader||this.isDead)return!1;if(this.isApplying)return this.reApply=!0,!1;this.isApplying=!0;let e=!1,n=r=>{let{token:u,action:m}=r.data;u!==this.token&&(m===l.APPLY&&u>this.token&&(e=!0),m===l.TELL&&(e=!0))};this.channel.addEventListener("message",n);try{return this.sendAction(l.APPLY),await Qe(this.options.responseTime),this.channel.removeEventListener("message",n),this.isApplying=!1,e?this.reApply?this.apply():!1:(this.assumeLead(),!0)}catch{return!1}}awaitLeadership(){if(this.isLeader)return Promise.resolve();let e=!1,n=null;return new Promise(r=>{let u=()=>{if(e)return;e=!0,v.clearInterval(n);let M=this.intervals.indexOf(n);this.intervals.splice(M,1),this.channel.removeEventListener("message",m),r()};n=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&u()})},this.options.fallbackInterval),this.intervals.push(n);let m=M=>{let{action:_}=M.data;_===l.CLOSE&&this.apply().then(()=>{this.isLeader&&u()})};this.channel.addEventListener("message",m)})}sendAction(e){this.channel.postMessage({action:e,token:this.token})}assumeLead(){this.isLeader=!0;let e=n=>{let{action:r}=n.data;r===l.APPLY&&this.sendAction(l.TELL)};return this.channel.addEventListener("message",e),this.listeners.push(e),this.sendAction(l.TELL)}waitForLeadership(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}close(){this.isDead||(this.isDead=!0,this.isLeader=!1,this.sendAction(l.CLOSE),this.listeners.forEach(e=>this.channel.removeEventListener("message",e)),this.intervals.forEach(e=>v.clearInterval(e)))}};var x;(function(r){r[r.PROMPTED=0]="PROMPTED",r[r.ACTIVE=1]="ACTIVE",r[r.IDLE=2]="IDLE"})(x||(x={}));var Ee=class{constructor(e){this.token=ue();this.registry=new Map;this.allIdle=!1;let{channelName:n}=e;if(this.options=e,this.channel=new Ke(n),this.registry.set(this.token,1),e.leaderElection){let r={fallbackInterval:2e3,responseTime:100};this.elector=new Ie(this.channel,r),this.elector.waitForLeadership()}this.channel.addEventListener("message",r=>{let{action:u,token:m,data:M}=r.data;switch(u){case l.REGISTER:this.registry.set(m,1);break;case l.DEREGISTER:this.registry.delete(m);break;case l.IDLE:this.idle(m);break;case l.ACTIVE:this.active(m);break;case l.PROMPT:this.prompt(m);break;case l.MESSAGE:this.options.onMessage(M);break;case l.START:this.options.start(!0);break;case l.RESET:this.options.reset(!0);break;case l.PAUSE:this.options.pause(!0);break;case l.RESUME:this.options.resume(!0);break}}),this.send(l.REGISTER)}get isLeader(){if(!this.elector)throw new Error('\u274C Leader election is not enabled. To Enable it set the "leaderElection" property to true.');return this.elector.isLeader}prompt(e=this.token){this.registry.set(e,0);let n=[...this.registry.values()].every(r=>r===0);e===this.token&&this.send(l.PROMPT),n&&this.options.onPrompt()}idle(e=this.token){this.registry.set(e,2);let n=[...this.registry.values()].every(r=>r===2);e===this.token&&this.send(l.IDLE),!this.allIdle&&n&&(this.allIdle=!0,this.options.onIdle())}active(e=this.token){this.registry.set(e,1);let n=[...this.registry.values()].some(r=>r===1);e===this.token&&this.send(l.ACTIVE),this.allIdle&&n&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:l.RESET,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:l.MESSAGE,token:this.token,data:e})}catch{}}send(e){try{this.channel.postMessage({action:e,token:this.token})}catch{}}close(){this.options.leaderElection&&this.elector.close(),this.send(l.DEREGISTER),this.channel.close()}};var Ze=b?document:null,et=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];var ce=se(require("react"));function N(t){let e=(0,ce.useRef)(t);return(0,ce.useEffect)(()=>{e.current=t},[t]),e}function tt(t,e){let n;function r(...u){n&&clearTimeout(n),n=setTimeout(()=>{t(...u),n=null},e)}return r.cancel=function(){clearTimeout(n)},r}function me(t,e){let n=0;return function(...r){let u=new Date().getTime();if(!(u-n<e))return n=u,t(...r)}}function q({timeout:t=1e3*60*20,promptTimeout:e=0,element:n=Ze,events:r=et,timers:u=void 0,immediateEvents:m=[],onPrompt:M=()=>{},onIdle:_=()=>{},onActive:Z=()=>{},onAction:d=()=>{},onMessage:c=()=>{},debounce:p=0,throttle:f=0,eventsThrottle:o=200,startOnMount:h=!0,startManually:k=!1,stopOnIdle:w=!1,crossTab:W=!1,syncTimers:Y=0,leaderElection:be=!1}={}){let J=(0,i.useRef)(I()),ge=(0,i.useRef)(Date.now()),de=(0,i.useRef)(I()),A=(0,i.useRef)(null),O=(0,i.useRef)(null),z=(0,i.useRef)(0),T=(0,i.useRef)(!0),S=(0,i.useRef)(!1),F=(0,i.useRef)(!0),X=(0,i.useRef)(!1),E=(0,i.useRef)(!1),g=(0,i.useRef)(0),P=(0,i.useRef)(0),D=(0,i.useRef)(null),a=(0,i.useRef)(null),C=N(t),ee=N(e),it=N(w),we=(0,i.useRef)(m),pe=(0,i.useRef)(n),fe=(0,i.useRef)([...new Set([...r,...m]).values()]),ye=N(_),te=N(Z),Le=N(M),re=N(c),y=(0,i.useRef)();(0,i.useEffect)(()=>{y.current&&y.current.cancel&&y.current.cancel(),p>0?y.current=tt(d,p):f>0?y.current=me(d,f):y.current=d},[d,f,p]);let Me=(0,i.useRef)();(0,i.useEffect)(()=>{W&&Y&&(Me.current=me(()=>{a.current.sync()},Y))},[W,Y]);let R=()=>{D.current!==null&&(v.clearTimeout(D.current),D.current=null)},H=(s,L=!0)=>{R(),D.current=v.setTimeout(he,s||C.current),L&&(O.current=I())},ke=s=>{g.current=0,P.current=I(),E.current=!0,H(ee.current,!1),Le.current(s)},xe=()=>{T.current=!0,A.current=I(),it.current?(R(),B()):E.current&&(P.current=0,E.current=!1),ye.current()},Se=s=>{E.current=!1,P.current=0,T.current=!1,z.current+=I()-A.current,U(),H(),te.current(s)},he=s=>{if(!T.current){if(ee.current>0&&!E.current){a.current?a.current.prompt():ke(s);return}a.current?a.current.idle():xe();return}a.current?a.current.active():Se(s)},Pe=s=>{if(y.current(s),E.current)return;W&&Y&&Me.current(),R();let L=I()-O.current;if((T.current&&!w||!T.current&&L>C.current)&&he(s),S.current=!1,g.current=0,P.current=0,!T.current&&we.current.includes(s.type)){he(s);return}H(),a.current&&a.current.active()},ne=(0,i.useRef)();(0,i.useEffect)(()=>{let s=X.current;s&&B(),o>0?ne.current=me(Pe,o):ne.current=Pe,s&&U()},[o,W,Y]);let U=()=>{!b||X.current||(fe.current.forEach(s=>{pe.current.addEventListener(s,ne.current,{capture:!0,passive:!0})}),X.current=!0)},B=(s=!1)=>{!b||(X.current||s)&&(fe.current.forEach(L=>{pe.current.removeEventListener(L,ne.current,{capture:!0})}),X.current=!1)},G=(0,i.useCallback)(s=>{R(),U(),E.current&&te.current(),T.current=!1,E.current=!1,S.current=!1,g.current=0,P.current=0,a.current&&!s&&(a.current.active(),a.current.send(l.START)),H()},[D,T,C,a]),oe=(0,i.useCallback)(s=>{R(),U(),(T.current||E.current)&&(te.current(),a.current&&a.current.active()),T.current=!1,E.current=!1,S.current=!1,g.current=0,P.current=0,de.current=I(),a.current&&(a.current.allIdle=!1,s||a.current.send(l.RESET)),H()},[D,T,C,a]),ve=(0,i.useCallback)((s=!1)=>S.current?!1:(g.current=Re(),S.current=!0,B(),R(),a.current&&(s||a.current.send(l.PAUSE)),!0),[D,a]),Te=(0,i.useCallback)((s=!1)=>S.current?(S.current=!1,E.current||U(),T.current||H(g.current),P.current&&(P.current=I()),a.current&&(s||a.current.send(l.RESUME)),!0):!1,[D,C,g,a]),st=(0,i.useCallback)((s,L)=>{a.current?(L&&re.current(s),a.current.message(s)):L&&re.current(s)},[c]),at=(0,i.useCallback)(()=>T.current,[T]),lt=(0,i.useCallback)(()=>E.current,[E]),ut=(0,i.useCallback)(()=>{if(!a.current)throw new Error('\u274C Cross Tab feature is not enabled. To enable it set the "crossTab" property to true.');return a.current.isLeader},[a]),Re=(0,i.useCallback)(()=>{if(S.current)return g.current;let s=g.current?g.current:E.current?ee.current:C.current,L=O.current?E.current?I()-P.current:I()-O.current:0,De=Math.ceil(s-L);return De<0?0:Math.abs(De)},[C,ee,E,g,O]),ct=(0,i.useCallback)(()=>Math.round(I()-de.current),[de]),Ae=(0,i.useCallback)(()=>Math.round(I()-J.current),[J]),mt=(0,i.useCallback)(()=>A.current?new Date(ge.current-J.current+A.current):null,[A]),dt=(0,i.useCallback)(()=>O.current?new Date(ge.current-J.current+O.current):null,[O]),Oe=(0,i.useCallback)(()=>T.current?Math.round(I()-A.current+z.current):Math.round(z.current),[A,z]),pt=(0,i.useCallback)(()=>{let s=Math.round(Ae()-Oe());return s>=0?s:0},[J,A,z]);return(0,i.useEffect)(()=>{if(p>0&&f>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");u&&Xe(u);let s=()=>{a.current&&a.current.close(),y.current.cancel&&y.current.cancel(),R(),B(!0)};return b&&window.addEventListener("beforeunload",s),()=>{b&&window.removeEventListener("beforeunload",s),a.current&&a.current.close(),y.current.cancel&&y.current.cancel(),R(),B(!0)}},[]),(0,i.useEffect)(()=>{a.current&&a.current.close(),W?a.current=new Ee({channelName:"idle-timer",leaderElection:be,onPrompt:()=>{ke()},onIdle:()=>{xe()},onActive:()=>{Se()},onMessage:re.current,start:G,reset:oe,pause:ve,resume:Te}):a.current=null},[W,be,Le,ye,te,re,G,oe,ve,Te]),(0,i.useEffect)(()=>{R(),B(!0),F.current||(S.current=!0,T.current=!0,g.current=0),!k&&(h?G():U())},[k,h,F]),(0,i.useEffect)(()=>{if(!F.current){let s=[...new Set([...r,...m]).values()];if(B(),fe.current=s,pe.current=n,we.current=m,k)return;h?G():U()}},[n,JSON.stringify(r),JSON.stringify(m),F,k,h]),(0,i.useEffect)(()=>{if(F.current)F.current=!1;else{if(C.current=t,k)return;T.current?oe():G()}},[t,k,F,T]),{message:st,start:G,reset:oe,pause:ve,resume:Te,isIdle:at,isPrompted:lt,isLeader:ut,getRemainingTime:Re,getElapsedTime:ct,getTotalElapsedTime:Ae,getLastIdleTime:mt,getLastActiveTime:dt,getTotalIdleTime:Oe,getTotalActiveTime:pt}}var j=se(require("react"));var Q=(0,j.createContext)(null);function rt(t){let e=q(t);return j.default.createElement(Q.Provider,{value:e},t.children)}var nt=Q.Consumer;function ot(){return(0,j.useContext)(Q)} |
@@ -1,1 +0,1 @@ | ||
var rt=Object.create,Te=Object.defineProperty;var nt=Object.getOwnPropertyDescriptor;var ot=Object.getOwnPropertyNames;var it=Object.getPrototypeOf,st=Object.prototype.hasOwnProperty;var at=t=>Te(t,"__esModule",{value:!0});var ut=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var ct=(t,e,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ot(e))!st.call(t,r)&&r!=="default"&&Te(t,r,{get:()=>e[r],enumerable:!(o=nt(e,r))||o.enumerable});return t},lt=t=>ct(at(Te(t!=null?rt(it(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Ae=ut((ie,Le)=>{(function(t,e){typeof ie=="object"&&typeof Le!="undefined"?e(ie):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.fastUniqueNumbers={}))})(ie,function(t){"use strict";var e=function(c){return function(f){var d=c(f);return f.add(d),d}},o=function(c){return function(f,d){return c.set(f,d),d}},r=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,l=536870912,T=l*2,W=function(c,f){return function(d){var S=f.get(d),w=S===void 0?d.size:S<T?S+1:0;if(!d.has(w))return c(d,w);if(d.size<l){for(;d.has(w);)w=Math.floor(Math.random()*T);return c(d,w)}if(d.size>r)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;d.has(w);)w=Math.floor(Math.random()*r);return c(d,w)}},j=new WeakMap,Z=o(j),a=W(Z,j),n=e(a);t.addUniqueNumber=n,t.generateUniqueNumber=a,Object.defineProperty(t,"__esModule",{value:!0})})});function mt(t){return function(o){let r={...o};!r.onPrompt&&t.prototype.onPrompt&&(r.onPrompt=()=>{t.prototype.onPrompt()}),!r.onIdle&&t.prototype.onIdle&&(r.onIdle=()=>{t.prototype.onIdle()}),!r.onActive&&t.prototype.onActive&&(r.onActive=T=>{t.prototype.onActive(T)}),!r.onAction&&t.prototype.onAction&&(r.onAction=T=>{t.prototype.onAction(T)});let l=Y(r);return React.createElement(t,{...o,...l})}}import{useEffect as _,useRef as p,useCallback as b}from"react";var K=lt(Ae());var Oe=t=>t.method!==void 0&&t.method==="call";var De=t=>t.error===null&&typeof t.id=="number";var Ce=t=>{let e=new Map([[0,()=>{}]]),o=new Map([[0,()=>{}]]),r=new Map,l=new Worker(t);return l.addEventListener("message",({data:a})=>{if(Oe(a)){let{params:{timerId:n,timerType:u}}=a;if(u==="interval"){let c=e.get(n);if(typeof c=="number"){let f=r.get(c);if(f===void 0||f.timerId!==n||f.timerType!==u)throw new Error("The timer is in an undefined state.")}else if(typeof c!="undefined")c();else throw new Error("The timer is in an undefined state.")}else if(u==="timeout"){let c=o.get(n);if(typeof c=="number"){let f=r.get(c);if(f===void 0||f.timerId!==n||f.timerType!==u)throw new Error("The timer is in an undefined state.")}else if(typeof c!="undefined")c(),o.delete(n);else throw new Error("The timer is in an undefined state.")}}else if(De(a)){let{id:n}=a,u=r.get(n);if(u===void 0)throw new Error("The timer is in an undefined state.");let{timerId:c,timerType:f}=u;r.delete(n),f==="interval"?e.delete(c):o.delete(c)}else{let{error:{message:n}}=a;throw new Error(n)}}),{clearInterval:a=>{let n=(0,K.generateUniqueNumber)(r);r.set(n,{timerId:a,timerType:"interval"}),e.set(a,n),l.postMessage({id:n,method:"clear",params:{timerId:a,timerType:"interval"}})},clearTimeout:a=>{let n=(0,K.generateUniqueNumber)(r);r.set(n,{timerId:a,timerType:"timeout"}),o.set(a,n),l.postMessage({id:n,method:"clear",params:{timerId:a,timerType:"timeout"}})},setInterval:(a,n)=>{let u=(0,K.generateUniqueNumber)(e);return e.set(u,()=>{a(),typeof e.get(u)=="function"&&l.postMessage({id:null,method:"set",params:{delay:n,now:performance.now(),timerId:u,timerType:"interval"}})}),l.postMessage({id:null,method:"set",params:{delay:n,now:performance.now(),timerId:u,timerType:"interval"}}),u},setTimeout:(a,n)=>{let u=(0,K.generateUniqueNumber)(o);return o.set(u,a),l.postMessage({id:null,method:"set",params:{delay:n,now:performance.now(),timerId:u,timerType:"timeout"}}),u}}};var Q=null,Ne=(t,e)=>()=>{if(Q!==null)return Q;let o=new Blob([e],{type:"application/javascript; charset=utf-8"}),r=URL.createObjectURL(o);return Q=t(r),Q.setTimeout(()=>URL.revokeObjectURL(r),0),Q};var _e=`(()=>{"use strict";const e=new Map,t=new Map,r=(e,t)=>{let r,o;const i=performance.now();r=i,o=e-Math.max(0,i-t);return{expected:r+o,remainingDelay:o}},o=(e,t,r,i)=>{const s=performance.now();s>r?postMessage({id:null,method:"call",params:{timerId:t,timerType:i}}):e.set(t,setTimeout(o,r-s,e,t,r,i))};addEventListener("message",(i=>{let{data:s}=i;try{if("clear"===s.method){const{id:r,params:{timerId:o,timerType:i}}=s;if("interval"===i)(t=>{const r=e.get(t);if(void 0===r)throw new Error('There is no interval scheduled with the given id "'.concat(t,'".'));clearTimeout(r),e.delete(t)})(o),postMessage({error:null,id:r});else{if("timeout"!==i)throw new Error('The given type "'.concat(i,'" is not supported'));(e=>{const r=t.get(e);if(void 0===r)throw new Error('There is no timeout scheduled with the given id "'.concat(e,'".'));clearTimeout(r),t.delete(e)})(o),postMessage({error:null,id:r})}}else{if("set"!==s.method)throw new Error('The given method "'.concat(s.method,'" is not supported'));{const{params:{delay:i,now:n,timerId:a,timerType:d}}=s;if("interval"===d)((t,i,s)=>{const{expected:n,remainingDelay:a}=r(t,s);e.set(i,setTimeout(o,a,e,i,n,"interval"))})(i,a,n);else{if("timeout"!==d)throw new Error('The given type "'.concat(d,'" is not supported'));((e,i,s)=>{const{expected:n,remainingDelay:a}=r(e,s);t.set(i,setTimeout(o,a,t,i,n,"timeout"))})(i,a,n)}}}}catch(e){postMessage({error:{message:e.message},id:s.id,result:null})}}))})();`;var se=Ne(Ce,_e),Ue=t=>se().clearInterval(t),Fe=t=>se().clearTimeout(t),Be=(t,e)=>se().setInterval(t,e),We=(t,e)=>se().setTimeout(t,e);var E=(typeof window=="undefined"?"undefined":typeof window)=="object";var g={setTimeout:E?setTimeout.bind(window):setTimeout,clearTimeout:E?clearTimeout.bind(window):clearTimeout,setInterval:E?setInterval.bind(window):setInterval,clearInterval:E?clearInterval.bind(window):clearInterval},dt={setTimeout:We,clearTimeout:Fe,setInterval:Be,clearInterval:Ue};function pt(){g.setTimeout=setTimeout,g.clearTimeout=clearTimeout,g.setInterval=setInterval,g.clearInterval=clearInterval}function He(t){g.setTimeout=t.setTimeout,g.clearTimeout=t.clearTimeout,g.setInterval=t.setInterval,g.clearInterval=t.clearInterval}var V={},Ge=class{constructor(e){this.closed=!1;this.mc=new MessageChannel;this.name=e,V[e]=V[e]||[],V[e].push(this),this.mc.port1.start(),this.mc.port2.start(),this.onStorage=this.onStorage.bind(this),window.addEventListener("storage",this.onStorage)}onStorage(e){if(e.storageArea!==window.localStorage||e.key.substring(0,this.name.length)!==this.name||e.newValue===null)return;let o=JSON.parse(e.newValue);this.mc.port2.postMessage(o)}postMessage(e){if(this.closed)throw new Error("InvalidStateError");let o=JSON.stringify(e),r=`${this.name}:${String(Date.now())}${String(Math.random())}`;window.localStorage.setItem(r,o),g.setTimeout(()=>{window.localStorage.removeItem(r)},500),V[this.name].forEach(l=>{l!==this&&l.mc.port2.postMessage(JSON.parse(o))})}close(){if(this.closed)return;this.closed=!0,this.mc.port1.close(),this.mc.port2.close(),window.removeEventListener("storage",this.onStorage);let e=V[this.name].indexOf(this);V[this.name].splice(e,1)}get onmessage(){return this.mc.port1.onmessage}set onmessage(e){this.mc.port1.onmessage=e}get onmessageerror(){return this.mc.port1.onmessageerror}set onmessageerror(e){this.mc.port1.onmessageerror=e}addEventListener(e,o){return this.mc.port1.addEventListener(e,o)}removeEventListener(e,o){return this.mc.port1.removeEventListener(e,o)}dispatchEvent(e){return this.mc.port1.dispatchEvent(e)}},qe=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Ge;var h=()=>E?performance.now():Date.now();function Ve(){return h().toString(36).substring(2)}var m;(function(n){n[n.REGISTER=0]="REGISTER",n[n.DEREGISTER=1]="DEREGISTER",n[n.IDLE=2]="IDLE",n[n.ACTIVE=3]="ACTIVE",n[n.PROMPT=4]="PROMPT",n[n.START=5]="START",n[n.RESET=6]="RESET",n[n.PAUSE=7]="PAUSE",n[n.RESUME=8]="RESUME",n[n.MESSAGE=9]="MESSAGE"})(m||(m={}));var k;(function(r){r[r.PROMPTED=0]="PROMPTED",r[r.ACTIVE=1]="ACTIVE",r[r.IDLE=2]="IDLE"})(k||(k={}));var ve=class{constructor(e){this.token=Ve();this.registry=new Map;this.allIdle=!1;let{channelName:o}=e;this.options=e,this.channel=new qe(o),this.registry.set(this.token,1),this.channel.addEventListener("message",r=>{let{action:l,token:T,data:W}=r.data;switch(l){case 0:this.registry.set(T,1);break;case 1:this.registry.delete(T);break;case 2:this.idle(T);break;case 3:this.active(T);break;case 4:this.prompt(T);break;case 9:this.options.onMessage(W);break;case 5:this.options.start(!0);break;case 6:this.options.reset(!0);break;case 7:this.options.pause(!0);break;case 8:this.options.resume(!0);break}}),this.send(0)}prompt(e=this.token){this.registry.set(e,0);let o=[...this.registry.values()].every(r=>r===0);e===this.token&&this.send(4),o&&this.options.onPrompt()}idle(e=this.token){this.registry.set(e,2);let o=[...this.registry.values()].every(r=>r===2);e===this.token&&this.send(2),!this.allIdle&&o&&(this.allIdle=!0,this.options.onIdle())}active(e=this.token){this.registry.set(e,1);let o=[...this.registry.values()].some(r=>r===1);e===this.token&&this.send(3),this.allIdle&&o&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:6,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:9,token:this.token,data:e})}catch{}}send(e){try{this.channel.postMessage({action:e,token:this.token})}catch{}}close(){this.send(1),this.channel.close()}};var je=E?document:null,Je=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];import{useRef as ft,useEffect as Tt}from"react";function N(t){let e=ft(t);return Tt(()=>{e.current=t},[t]),e}function ze(t,e){let o;function r(...l){o&&clearTimeout(o),o=setTimeout(()=>{t(...l),o=null},e)}return r.cancel=function(){clearTimeout(o)},r}function ae(t,e){let o=0;return function(...r){let l=new Date().getTime();if(!(l-o<e))return o=l,t(...r)}}function Y({timeout:t=1e3*60*20,promptTimeout:e=0,element:o=je,events:r=Je,timers:l=void 0,immediateEvents:T=[],onPrompt:W=()=>{},onIdle:j=()=>{},onActive:Z=()=>{},onAction:a=()=>{},onMessage:n=()=>{},debounce:u=0,throttle:c=0,eventsThrottle:f=200,startOnMount:d=!0,startManually:S=!1,stopOnIdle:w=!1,crossTab:H=!1,syncTimers:J=0}={}){let z=p(h()),he=p(Date.now()),ce=p(h()),A=p(null),O=p(null),X=p(0),v=p(!0),R=p(!1),U=p(!0),$=p(!1),I=p(!1),y=p(0),P=p(0),D=p(null),s=p(null),C=N(t),ee=N(e),Xe=N(w),Ie=p(T),le=p(o),me=p([...new Set([...r,...T]).values()]),Ee=N(j),te=N(Z),be=N(W),re=N(n),x=p();_(()=>{x.current&&x.current.cancel&&x.current.cancel(),u>0?x.current=ze(a,u):c>0?x.current=ae(a,c):x.current=a},[a,c,u]);let ge=p();_(()=>{H&&J&&(ge.current=ae(()=>{s.current.sync()},J))},[H,J]);let L=()=>{D.current!==null&&(g.clearTimeout(D.current),D.current=null)},G=(i,M=!0)=>{L(),D.current=g.setTimeout(de,i||C.current),M&&(O.current=h())},ye=i=>{y.current=0,P.current=h(),I.current=!0,G(ee.current,!1),be.current(i)},we=()=>{v.current=!0,A.current=h(),Xe.current?(L(),B()):I.current&&(P.current=0,I.current=!1),Ee.current()},xe=i=>{I.current=!1,P.current=0,v.current=!1,X.current+=h()-A.current,F(),G(),te.current(i)},de=i=>{if(!v.current){if(ee.current>0&&!I.current){s.current?s.current.prompt():ye(i);return}s.current?s.current.idle():we();return}s.current?s.current.active():xe(i)},Me=i=>{if(x.current(i),I.current)return;H&&J&&ge.current(),L();let M=h()-O.current;if((v.current&&!w||!v.current&&M>C.current)&&de(i),R.current=!1,y.current=0,P.current=0,!v.current&&Ie.current.includes(i.type)){de(i);return}G(),s.current&&s.current.active()},ne=p();_(()=>{let i=$.current;i&&B(),f>0?ne.current=ae(Me,f):ne.current=Me,i&&F()},[f,H,J]);let F=()=>{!E||$.current||(me.current.forEach(i=>{le.current.addEventListener(i,ne.current,{capture:!0,passive:!0})}),$.current=!0)},B=(i=!1)=>{!E||($.current||i)&&(me.current.forEach(M=>{le.current.removeEventListener(M,ne.current,{capture:!0})}),$.current=!1)},q=b(i=>{L(),F(),I.current&&te.current(),v.current=!1,I.current=!1,R.current=!1,y.current=0,P.current=0,s.current&&!i&&(s.current.active(),s.current.send(m.START)),G()},[D,v,C,s]),oe=b(i=>{L(),F(),(v.current||I.current)&&(te.current(),s.current&&s.current.active()),v.current=!1,I.current=!1,R.current=!1,y.current=0,P.current=0,ce.current=h(),s.current&&(s.current.allIdle=!1,i||s.current.send(m.RESET)),G()},[D,v,C,s]),pe=b((i=!1)=>R.current?!1:(y.current=Se(),R.current=!0,B(),L(),s.current&&(i||s.current.send(m.PAUSE)),!0),[D,s]),fe=b((i=!1)=>R.current?(R.current=!1,I.current||F(),v.current||G(y.current),P.current&&(P.current=h()),s.current&&(i||s.current.send(m.RESUME)),!0):!1,[D,C,y,s]),$e=b((i,M)=>{s.current?(M&&re.current(i),s.current.message(i)):M&&re.current(i)},[n]),Ye=b(()=>v.current,[v]),Ke=b(()=>I.current,[I]),Se=b(()=>{if(R.current)return y.current;let i=y.current?y.current:I.current?ee.current:C.current,M=O.current?I.current?h()-P.current:h()-O.current:0,Pe=Math.ceil(i-M);return Pe<0?0:Math.abs(Pe)},[C,ee,I,y,O]),Qe=b(()=>Math.round(h()-ce.current),[ce]),ke=b(()=>Math.round(h()-z.current),[z]),Ze=b(()=>A.current?new Date(he.current-z.current+A.current):null,[A]),et=b(()=>O.current?new Date(he.current-z.current+O.current):null,[O]),Re=b(()=>v.current?Math.round(h()-A.current+X.current):Math.round(X.current),[A,X]),tt=b(()=>{let i=Math.round(ke()-Re());return i>=0?i:0},[z,A,X]);return _(()=>{if(u>0&&c>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");l&&He(l);let i=()=>{s.current&&s.current.close(),x.current.cancel&&x.current.cancel(),L(),B(!0)};return E&&window.addEventListener("beforeunload",i),()=>{E&&window.removeEventListener("beforeunload",i),s.current&&s.current.close(),x.current.cancel&&x.current.cancel(),L(),B(!0)}},[]),_(()=>{s.current&&s.current.close(),H?s.current=new ve({channelName:"idle-timer",onPrompt:()=>{ye()},onIdle:()=>{we()},onActive:()=>{xe()},onMessage:re.current,start:q,reset:oe,pause:pe,resume:fe}):s.current=null},[H,be,Ee,te,re,q,oe,pe,fe]),_(()=>{L(),B(!0),U.current||(R.current=!0,v.current=!0,y.current=0),!S&&(d?q():F())},[S,d,U]),_(()=>{if(!U.current){let i=[...new Set([...r,...T]).values()];if(B(),me.current=i,le.current=o,Ie.current=T,S)return;d?q():F()}},[o,JSON.stringify(r),JSON.stringify(T),U,S,d]),_(()=>{if(U.current)U.current=!1;else{if(C.current=t,S)return;v.current?oe():q()}},[t,S,U,v]),{message:$e,start:q,reset:oe,pause:pe,resume:fe,isIdle:Ye,isPrompted:Ke,getRemainingTime:Se,getElapsedTime:Qe,getTotalElapsedTime:ke,getLastIdleTime:Ze,getLastActiveTime:et,getTotalIdleTime:Re,getTotalActiveTime:tt}}import vt,{createContext as ht,useContext as It}from"react";var ue=ht(null);function Et(t){let e=Y(t);return vt.createElement(ue.Provider,{value:e},t.children)}var bt=ue.Consumer;function gt(){return It(ue)}export{bt as IdleTimerConsumer,ue as IdleTimerContext,Et as IdleTimerProvider,pt as createMocks,Y as useIdleTimer,gt as useIdleTimerContext,mt as withIdleTimer,dt as workerTimers}; | ||
var st=Object.create,ve=Object.defineProperty;var at=Object.getOwnPropertyDescriptor;var lt=Object.getOwnPropertyNames;var ut=Object.getPrototypeOf,ct=Object.prototype.hasOwnProperty;var mt=t=>ve(t,"__esModule",{value:!0});var dt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var pt=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of lt(e))!ct.call(t,r)&&r!=="default"&&ve(t,r,{get:()=>e[r],enumerable:!(n=at(e,r))||n.enumerable});return t},ft=t=>pt(mt(ve(t!=null?st(ut(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Ce=dt((ie,De)=>{(function(t,e){typeof ie=="object"&&typeof De!="undefined"?e(ie):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.fastUniqueNumbers={}))})(ie,function(t){"use strict";var e=function(p){return function(o){var f=p(o);return o.add(f),f}},n=function(p){return function(o,f){return p.set(o,f),f}},r=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,l=536870912,c=l*2,k=function(p,o){return function(f){var x=o.get(f),y=x===void 0?f.size:x<c?x+1:0;if(!f.has(y))return p(f,y);if(f.size<l){for(;f.has(y);)y=Math.floor(Math.random()*c);return p(f,y)}if(f.size>r)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;f.has(y);)y=Math.floor(Math.random()*r);return p(f,y)}},U=new WeakMap,Z=n(U),m=k(Z,U),u=e(m);t.addUniqueNumber=u,t.generateUniqueNumber=m,Object.defineProperty(t,"__esModule",{value:!0})})});function ht(t){return function(n){let r={...n};!r.onPrompt&&t.prototype.onPrompt&&(r.onPrompt=()=>{t.prototype.onPrompt()}),!r.onIdle&&t.prototype.onIdle&&(r.onIdle=()=>{t.prototype.onIdle()}),!r.onActive&&t.prototype.onActive&&(r.onActive=c=>{t.prototype.onActive(c)}),!r.onAction&&t.prototype.onAction&&(r.onAction=c=>{t.prototype.onAction(c)});let l=$(r);return React.createElement(t,{...n,...l})}}import{useEffect as F,useRef as h,useCallback as b}from"react";var K=ft(Ce());var Ne=t=>t.method!==void 0&&t.method==="call";var _e=t=>t.error===null&&typeof t.id=="number";var Fe=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,l=new Worker(t);return l.addEventListener("message",({data:m})=>{if(Ne(m)){let{params:{timerId:u,timerType:d}}=m;if(d==="interval"){let p=e.get(u);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==u||o.timerType!==d)throw new Error("The timer is in an undefined state.")}else if(typeof p!="undefined")p();else throw new Error("The timer is in an undefined state.")}else if(d==="timeout"){let p=n.get(u);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==u||o.timerType!==d)throw new Error("The timer is in an undefined state.")}else if(typeof p!="undefined")p(),n.delete(u);else throw new Error("The timer is in an undefined state.")}}else if(_e(m)){let{id:u}=m,d=r.get(u);if(d===void 0)throw new Error("The timer is in an undefined state.");let{timerId:p,timerType:o}=d;r.delete(u),o==="interval"?e.delete(p):n.delete(p)}else{let{error:{message:u}}=m;throw new Error(u)}}),{clearInterval:m=>{let u=(0,K.generateUniqueNumber)(r);r.set(u,{timerId:m,timerType:"interval"}),e.set(m,u),l.postMessage({id:u,method:"clear",params:{timerId:m,timerType:"interval"}})},clearTimeout:m=>{let u=(0,K.generateUniqueNumber)(r);r.set(u,{timerId:m,timerType:"timeout"}),n.set(m,u),l.postMessage({id:u,method:"clear",params:{timerId:m,timerType:"timeout"}})},setInterval:(m,u)=>{let d=(0,K.generateUniqueNumber)(e);return e.set(d,()=>{m(),typeof e.get(d)=="function"&&l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:d,timerType:"interval"}})}),l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:d,timerType:"interval"}}),d},setTimeout:(m,u)=>{let d=(0,K.generateUniqueNumber)(n);return n.set(d,m),l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:d,timerType:"timeout"}}),d}}};var Q=null,Ue=(t,e)=>()=>{if(Q!==null)return Q;let n=new Blob([e],{type:"application/javascript; charset=utf-8"}),r=URL.createObjectURL(n);return Q=t(r),Q.setTimeout(()=>URL.revokeObjectURL(r),0),Q};var Be=`(()=>{"use strict";const e=new Map,t=new Map,r=(e,t)=>{let r,o;const i=performance.now();r=i,o=e-Math.max(0,i-t);return{expected:r+o,remainingDelay:o}},o=(e,t,r,i)=>{const s=performance.now();s>r?postMessage({id:null,method:"call",params:{timerId:t,timerType:i}}):e.set(t,setTimeout(o,r-s,e,t,r,i))};addEventListener("message",(i=>{let{data:s}=i;try{if("clear"===s.method){const{id:r,params:{timerId:o,timerType:i}}=s;if("interval"===i)(t=>{const r=e.get(t);if(void 0===r)throw new Error('There is no interval scheduled with the given id "'.concat(t,'".'));clearTimeout(r),e.delete(t)})(o),postMessage({error:null,id:r});else{if("timeout"!==i)throw new Error('The given type "'.concat(i,'" is not supported'));(e=>{const r=t.get(e);if(void 0===r)throw new Error('There is no timeout scheduled with the given id "'.concat(e,'".'));clearTimeout(r),t.delete(e)})(o),postMessage({error:null,id:r})}}else{if("set"!==s.method)throw new Error('The given method "'.concat(s.method,'" is not supported'));{const{params:{delay:i,now:n,timerId:a,timerType:d}}=s;if("interval"===d)((t,i,s)=>{const{expected:n,remainingDelay:a}=r(t,s);e.set(i,setTimeout(o,a,e,i,n,"interval"))})(i,a,n);else{if("timeout"!==d)throw new Error('The given type "'.concat(d,'" is not supported'));((e,i,s)=>{const{expected:n,remainingDelay:a}=r(e,s);t.set(i,setTimeout(o,a,t,i,n,"timeout"))})(i,a,n)}}}}catch(e){postMessage({error:{message:e.message},id:s.id,result:null})}}))})();`;var se=Ue(Fe,Be),We=t=>se().clearInterval(t),He=t=>se().clearTimeout(t),Ge=(t,e)=>se().setInterval(t,e),qe=(t,e)=>se().setTimeout(t,e);var g=(typeof window=="undefined"?"undefined":typeof window)=="object";var v={setTimeout:g?setTimeout.bind(window):setTimeout,clearTimeout:g?clearTimeout.bind(window):clearTimeout,setInterval:g?setInterval.bind(window):setInterval,clearInterval:g?clearInterval.bind(window):clearInterval},vt={setTimeout:qe,clearTimeout:He,setInterval:Ge,clearInterval:We};function Tt(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function Ve(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var j={},je=class{constructor(e){this.closed=!1;this.mc=new MessageChannel;this.name=e,j[e]=j[e]||[],j[e].push(this),this.mc.port1.start(),this.mc.port2.start(),this.onStorage=this.onStorage.bind(this),window.addEventListener("storage",this.onStorage)}onStorage(e){if(e.storageArea!==window.localStorage||e.key.substring(0,this.name.length)!==this.name||e.newValue===null)return;let n=JSON.parse(e.newValue);this.mc.port2.postMessage(n)}postMessage(e){if(this.closed)throw new Error("InvalidStateError");let n=JSON.stringify(e),r=`${this.name}:${String(Date.now())}${String(Math.random())}`;window.localStorage.setItem(r,n),v.setTimeout(()=>{window.localStorage.removeItem(r)},500),j[this.name].forEach(l=>{l!==this&&l.mc.port2.postMessage(JSON.parse(n))})}close(){if(this.closed)return;this.closed=!0,this.mc.port1.close(),this.mc.port2.close(),window.removeEventListener("storage",this.onStorage);let e=j[this.name].indexOf(this);j[this.name].splice(e,1)}get onmessage(){return this.mc.port1.onmessage}set onmessage(e){this.mc.port1.onmessage=e}get onmessageerror(){return this.mc.port1.onmessageerror}set onmessageerror(e){this.mc.port1.onmessageerror=e}addEventListener(e,n){return this.mc.port1.addEventListener(e,n)}removeEventListener(e,n){return this.mc.port1.removeEventListener(e,n)}dispatchEvent(e){return this.mc.port1.dispatchEvent(e)}},Ye=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:je;function Je(t=0){return new Promise(e=>v.setTimeout(e,t))}var I=()=>g?performance.now():Date.now();function ae(){return I().toString(36).substring(2)}var a;(function(o){o[o.APPLY=0]="APPLY",o[o.TELL=1]="TELL",o[o.CLOSE=2]="CLOSE",o[o.REGISTER=3]="REGISTER",o[o.DEREGISTER=4]="DEREGISTER",o[o.IDLE=5]="IDLE",o[o.ACTIVE=6]="ACTIVE",o[o.PROMPT=7]="PROMPT",o[o.START=8]="START",o[o.RESET=9]="RESET",o[o.PAUSE=10]="PAUSE",o[o.RESUME=11]="RESUME",o[o.MESSAGE=12]="MESSAGE"})(a||(a={}));var Te=class{constructor(e,n){this.token=ae();this.isLeader=!1;this.isDead=!1;this.isApplying=!1;this.reApply=!1;this.intervals=[];this.listeners=[];this.channel=e,this.options=n,this.apply=this.apply.bind(this),this.awaitLeadership=this.awaitLeadership.bind(this),this.sendAction=this.sendAction.bind(this)}async apply(){if(this.isLeader||this.isDead)return!1;if(this.isApplying)return this.reApply=!0,!1;this.isApplying=!0;let e=!1,n=r=>{let{token:l,action:c}=r.data;l!==this.token&&(c===a.APPLY&&l>this.token&&(e=!0),c===a.TELL&&(e=!0))};this.channel.addEventListener("message",n);try{return this.sendAction(a.APPLY),await Je(this.options.responseTime),this.channel.removeEventListener("message",n),this.isApplying=!1,e?this.reApply?this.apply():!1:(this.assumeLead(),!0)}catch{return!1}}awaitLeadership(){if(this.isLeader)return Promise.resolve();let e=!1,n=null;return new Promise(r=>{let l=()=>{if(e)return;e=!0,v.clearInterval(n);let k=this.intervals.indexOf(n);this.intervals.splice(k,1),this.channel.removeEventListener("message",c),r()};n=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&l()})},this.options.fallbackInterval),this.intervals.push(n);let c=k=>{let{action:U}=k.data;U===a.CLOSE&&this.apply().then(()=>{this.isLeader&&l()})};this.channel.addEventListener("message",c)})}sendAction(e){this.channel.postMessage({action:e,token:this.token})}assumeLead(){this.isLeader=!0;let e=n=>{let{action:r}=n.data;r===a.APPLY&&this.sendAction(a.TELL)};return this.channel.addEventListener("message",e),this.listeners.push(e),this.sendAction(a.TELL)}waitForLeadership(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}close(){this.isDead||(this.isDead=!0,this.isLeader=!1,this.sendAction(a.CLOSE),this.listeners.forEach(e=>this.channel.removeEventListener("message",e)),this.intervals.forEach(e=>v.clearInterval(e)))}};var S;(function(r){r[r.PROMPTED=0]="PROMPTED",r[r.ACTIVE=1]="ACTIVE",r[r.IDLE=2]="IDLE"})(S||(S={}));var Ie=class{constructor(e){this.token=ae();this.registry=new Map;this.allIdle=!1;let{channelName:n}=e;if(this.options=e,this.channel=new Ye(n),this.registry.set(this.token,1),e.leaderElection){let r={fallbackInterval:2e3,responseTime:100};this.elector=new Te(this.channel,r),this.elector.waitForLeadership()}this.channel.addEventListener("message",r=>{let{action:l,token:c,data:k}=r.data;switch(l){case a.REGISTER:this.registry.set(c,1);break;case a.DEREGISTER:this.registry.delete(c);break;case a.IDLE:this.idle(c);break;case a.ACTIVE:this.active(c);break;case a.PROMPT:this.prompt(c);break;case a.MESSAGE:this.options.onMessage(k);break;case a.START:this.options.start(!0);break;case a.RESET:this.options.reset(!0);break;case a.PAUSE:this.options.pause(!0);break;case a.RESUME:this.options.resume(!0);break}}),this.send(a.REGISTER)}get isLeader(){if(!this.elector)throw new Error('\u274C Leader election is not enabled. To Enable it set the "leaderElection" property to true.');return this.elector.isLeader}prompt(e=this.token){this.registry.set(e,0);let n=[...this.registry.values()].every(r=>r===0);e===this.token&&this.send(a.PROMPT),n&&this.options.onPrompt()}idle(e=this.token){this.registry.set(e,2);let n=[...this.registry.values()].every(r=>r===2);e===this.token&&this.send(a.IDLE),!this.allIdle&&n&&(this.allIdle=!0,this.options.onIdle())}active(e=this.token){this.registry.set(e,1);let n=[...this.registry.values()].some(r=>r===1);e===this.token&&this.send(a.ACTIVE),this.allIdle&&n&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:a.RESET,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:a.MESSAGE,token:this.token,data:e})}catch{}}send(e){try{this.channel.postMessage({action:e,token:this.token})}catch{}}close(){this.options.leaderElection&&this.elector.close(),this.send(a.DEREGISTER),this.channel.close()}};var ze=g?document:null,Xe=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];import{useRef as It,useEffect as Et}from"react";function _(t){let e=It(t);return Et(()=>{e.current=t},[t]),e}function $e(t,e){let n;function r(...l){n&&clearTimeout(n),n=setTimeout(()=>{t(...l),n=null},e)}return r.cancel=function(){clearTimeout(n)},r}function le(t,e){let n=0;return function(...r){let l=new Date().getTime();if(!(l-n<e))return n=l,t(...r)}}function $({timeout:t=1e3*60*20,promptTimeout:e=0,element:n=ze,events:r=Xe,timers:l=void 0,immediateEvents:c=[],onPrompt:k=()=>{},onIdle:U=()=>{},onActive:Z=()=>{},onAction:m=()=>{},onMessage:u=()=>{},debounce:d=0,throttle:p=0,eventsThrottle:o=200,startOnMount:f=!0,startManually:x=!1,stopOnIdle:y=!1,crossTab:G=!1,syncTimers:Y=0,leaderElection:Ee=!1}={}){let J=h(I()),be=h(Date.now()),ce=h(I()),O=h(null),D=h(null),z=h(0),T=h(!0),P=h(!1),B=h(!0),X=h(!1),E=h(!1),w=h(0),R=h(0),C=h(null),s=h(null),N=_(t),ee=_(e),Ke=_(y),ge=h(c),me=h(n),de=h([...new Set([...r,...c]).values()]),we=_(U),te=_(Z),ye=_(k),re=_(u),L=h();F(()=>{L.current&&L.current.cancel&&L.current.cancel(),d>0?L.current=$e(m,d):p>0?L.current=le(m,p):L.current=m},[m,p,d]);let Le=h();F(()=>{G&&Y&&(Le.current=le(()=>{s.current.sync()},Y))},[G,Y]);let A=()=>{C.current!==null&&(v.clearTimeout(C.current),C.current=null)},q=(i,M=!0)=>{A(),C.current=v.setTimeout(pe,i||N.current),M&&(D.current=I())},Me=i=>{w.current=0,R.current=I(),E.current=!0,q(ee.current,!1),ye.current(i)},ke=()=>{T.current=!0,O.current=I(),Ke.current?(A(),H()):E.current&&(R.current=0,E.current=!1),we.current()},xe=i=>{E.current=!1,R.current=0,T.current=!1,z.current+=I()-O.current,W(),q(),te.current(i)},pe=i=>{if(!T.current){if(ee.current>0&&!E.current){s.current?s.current.prompt():Me(i);return}s.current?s.current.idle():ke();return}s.current?s.current.active():xe(i)},Se=i=>{if(L.current(i),E.current)return;G&&Y&&Le.current(),A();let M=I()-D.current;if((T.current&&!y||!T.current&&M>N.current)&&pe(i),P.current=!1,w.current=0,R.current=0,!T.current&&ge.current.includes(i.type)){pe(i);return}q(),s.current&&s.current.active()},ne=h();F(()=>{let i=X.current;i&&H(),o>0?ne.current=le(Se,o):ne.current=Se,i&&W()},[o,G,Y]);let W=()=>{!g||X.current||(de.current.forEach(i=>{me.current.addEventListener(i,ne.current,{capture:!0,passive:!0})}),X.current=!0)},H=(i=!1)=>{!g||(X.current||i)&&(de.current.forEach(M=>{me.current.removeEventListener(M,ne.current,{capture:!0})}),X.current=!1)},V=b(i=>{A(),W(),E.current&&te.current(),T.current=!1,E.current=!1,P.current=!1,w.current=0,R.current=0,s.current&&!i&&(s.current.active(),s.current.send(a.START)),q()},[C,T,N,s]),oe=b(i=>{A(),W(),(T.current||E.current)&&(te.current(),s.current&&s.current.active()),T.current=!1,E.current=!1,P.current=!1,w.current=0,R.current=0,ce.current=I(),s.current&&(s.current.allIdle=!1,i||s.current.send(a.RESET)),q()},[C,T,N,s]),fe=b((i=!1)=>P.current?!1:(w.current=Pe(),P.current=!0,H(),A(),s.current&&(i||s.current.send(a.PAUSE)),!0),[C,s]),he=b((i=!1)=>P.current?(P.current=!1,E.current||W(),T.current||q(w.current),R.current&&(R.current=I()),s.current&&(i||s.current.send(a.RESUME)),!0):!1,[C,N,w,s]),Qe=b((i,M)=>{s.current?(M&&re.current(i),s.current.message(i)):M&&re.current(i)},[u]),Ze=b(()=>T.current,[T]),et=b(()=>E.current,[E]),tt=b(()=>{if(!s.current)throw new Error('\u274C Cross Tab feature is not enabled. To enable it set the "crossTab" property to true.');return s.current.isLeader},[s]),Pe=b(()=>{if(P.current)return w.current;let i=w.current?w.current:E.current?ee.current:N.current,M=D.current?E.current?I()-R.current:I()-D.current:0,Oe=Math.ceil(i-M);return Oe<0?0:Math.abs(Oe)},[N,ee,E,w,D]),rt=b(()=>Math.round(I()-ce.current),[ce]),Re=b(()=>Math.round(I()-J.current),[J]),nt=b(()=>O.current?new Date(be.current-J.current+O.current):null,[O]),ot=b(()=>D.current?new Date(be.current-J.current+D.current):null,[D]),Ae=b(()=>T.current?Math.round(I()-O.current+z.current):Math.round(z.current),[O,z]),it=b(()=>{let i=Math.round(Re()-Ae());return i>=0?i:0},[J,O,z]);return F(()=>{if(d>0&&p>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");l&&Ve(l);let i=()=>{s.current&&s.current.close(),L.current.cancel&&L.current.cancel(),A(),H(!0)};return g&&window.addEventListener("beforeunload",i),()=>{g&&window.removeEventListener("beforeunload",i),s.current&&s.current.close(),L.current.cancel&&L.current.cancel(),A(),H(!0)}},[]),F(()=>{s.current&&s.current.close(),G?s.current=new Ie({channelName:"idle-timer",leaderElection:Ee,onPrompt:()=>{Me()},onIdle:()=>{ke()},onActive:()=>{xe()},onMessage:re.current,start:V,reset:oe,pause:fe,resume:he}):s.current=null},[G,Ee,ye,we,te,re,V,oe,fe,he]),F(()=>{A(),H(!0),B.current||(P.current=!0,T.current=!0,w.current=0),!x&&(f?V():W())},[x,f,B]),F(()=>{if(!B.current){let i=[...new Set([...r,...c]).values()];if(H(),de.current=i,me.current=n,ge.current=c,x)return;f?V():W()}},[n,JSON.stringify(r),JSON.stringify(c),B,x,f]),F(()=>{if(B.current)B.current=!1;else{if(N.current=t,x)return;T.current?oe():V()}},[t,x,B,T]),{message:Qe,start:V,reset:oe,pause:fe,resume:he,isIdle:Ze,isPrompted:et,isLeader:tt,getRemainingTime:Pe,getElapsedTime:rt,getTotalElapsedTime:Re,getLastIdleTime:nt,getLastActiveTime:ot,getTotalIdleTime:Ae,getTotalActiveTime:it}}import bt,{createContext as gt,useContext as wt}from"react";var ue=gt(null);function yt(t){let e=$(t);return bt.createElement(ue.Provider,{value:e},t.children)}var Lt=ue.Consumer;function Mt(){return wt(ue)}export{Lt as IdleTimerConsumer,ue as IdleTimerContext,yt as IdleTimerProvider,Tt as createMocks,$ as useIdleTimer,Mt as useIdleTimerContext,ht as withIdleTimer,vt as workerTimers}; |
/// <reference types="react" /> | ||
import { MessageActionType } from '../types/MessageActionType'; | ||
interface ITabManagerOptions { | ||
channelName: string; | ||
leaderElection: boolean; | ||
onPrompt: (event?: Event) => void; | ||
@@ -13,14 +15,2 @@ onIdle: (event?: Event) => void; | ||
} | ||
export declare enum MessageAction { | ||
REGISTER = 0, | ||
DEREGISTER = 1, | ||
IDLE = 2, | ||
ACTIVE = 3, | ||
PROMPT = 4, | ||
START = 5, | ||
RESET = 6, | ||
PAUSE = 7, | ||
RESUME = 8, | ||
MESSAGE = 9 | ||
} | ||
declare enum RegistryState { | ||
@@ -34,2 +24,3 @@ PROMPTED = 0, | ||
private options; | ||
private elector; | ||
private token; | ||
@@ -39,2 +30,3 @@ registry: Map<string, RegistryState>; | ||
constructor(options: ITabManagerOptions); | ||
get isLeader(): boolean; | ||
prompt(token?: string): void; | ||
@@ -45,5 +37,5 @@ idle(token?: string): void; | ||
message(data: any): void; | ||
send(action: MessageAction): void; | ||
send(action: MessageActionType): void; | ||
close(): void; | ||
} | ||
export {}; |
@@ -36,2 +36,8 @@ export interface IIdleTimer { | ||
/** | ||
* Returns wether or not the current tab is the leader. | ||
* | ||
* @returns Leader state. | ||
*/ | ||
isLeader(): boolean; | ||
/** | ||
* Returns wether or not the prompt is active. | ||
@@ -38,0 +44,0 @@ * |
@@ -137,2 +137,7 @@ /// <reference types="react" /> | ||
syncTimers?: number; | ||
/** | ||
* Enables the leader election feature. Leader Election will assign one tab to | ||
* be the leader. Determine if a tab is leader using the `isLeader` method. | ||
*/ | ||
leaderElection?: boolean; | ||
} |
@@ -9,2 +9,2 @@ import { IIdleTimer } from './types/IIdleTimer'; | ||
*/ | ||
export declare function useIdleTimer({ timeout, promptTimeout, element, events, timers, immediateEvents, onPrompt, onIdle, onActive, onAction, onMessage, debounce, throttle, eventsThrottle, startOnMount, startManually, stopOnIdle, crossTab, syncTimers }?: IIdleTimerProps): IIdleTimer; | ||
export declare function useIdleTimer({ timeout, promptTimeout, element, events, timers, immediateEvents, onPrompt, onIdle, onActive, onAction, onMessage, debounce, throttle, eventsThrottle, startOnMount, startManually, stopOnIdle, crossTab, syncTimers, leaderElection }?: IIdleTimerProps): IIdleTimer; |
{ | ||
"name": "react-idle-timer", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Activity detection for React.js", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
56748
14.01%30
11.11%610
15.31%