react-idle-timer
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
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)} | ||
var ht=Object.create,se=Object.defineProperty;var vt=Object.getOwnPropertyDescriptor;var Tt=Object.getOwnPropertyNames;var It=Object.getPrototypeOf,Et=Object.prototype.hasOwnProperty;var Ce=t=>se(t,"__esModule",{value:!0});var bt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),gt=(t,e)=>{for(var n in e)se(t,n,{get:e[n],enumerable:!0})},wt=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Tt(e))!Et.call(t,r)&&r!=="default"&&se(t,r,{get:()=>e[r],enumerable:!(n=vt(e,r))||n.enumerable});return t},$=t=>wt(Ce(se(t!=null?ht(It(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Ue=bt((ae,Fe)=>{(function(t,e){typeof ae=="object"&&typeof Fe!="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,ee=n(_),d=M(ee,_),c=e(d);t.addUniqueNumber=c,t.generateUniqueNumber=d,Object.defineProperty(t,"__esModule",{value:!0})})});Ce(exports);gt(exports,{IdleTimerConsumer:()=>ot,IdleTimerContext:()=>Z,IdleTimerProvider:()=>nt,createMocks:()=>Xe,useIdleTimer:()=>q,useIdleTimerContext:()=>it,withIdleTimer:()=>_e,workerTimers:()=>ze});var Ne=$(require("react"));function _e(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 Ne.default.createElement(t,{...n,...u})}}var i=$(require("react"));var K=$(Ue());var Be=t=>t.method!==void 0&&t.method==="call";var We=t=>t.error===null&&typeof t.id=="number";var He=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,u=new Worker(t);return u.addEventListener("message",({data:d})=>{if(Be(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(We(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,K.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,K.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,K.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,K.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 Q=null,Ge=(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 qe=`(()=>{"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=Ge(He,qe),Ve=t=>le().clearInterval(t),je=t=>le().clearTimeout(t),Ye=(t,e)=>le().setInterval(t,e),Je=(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},ze={setTimeout:Je,clearTimeout:je,setInterval:Ye,clearInterval:Ve};function Xe(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function $e(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var V={},Ke=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)}},Qe=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Ke;function Ze(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 Ze(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 Qe(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 et=b?document:null,tt=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];var ce=$(require("react"));function N(t){let e=(0,ce.useRef)(t);return(0,ce.useEffect)(()=>{e.current=t},[t]),e}function rt(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=et,events:r=tt,timers:u=void 0,immediateEvents:m=[],onPrompt:M=()=>{},onIdle:_=()=>{},onActive:ee=()=>{},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),te=N(e),st=N(w),we=(0,i.useRef)(m),pe=(0,i.useRef)(n),fe=(0,i.useRef)([...new Set([...r,...m]).values()]),ye=N(_),re=N(ee),Le=N(M),ne=N(c),y=(0,i.useRef)();(0,i.useEffect)(()=>{y.current&&y.current.cancel&&y.current.cancel(),p>0?y.current=rt(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(te.current,!1),Le.current(s)},xe=()=>{T.current=!0,A.current=I(),st.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(),re.current(s)},he=s=>{if(!T.current){if(te.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()},oe=(0,i.useRef)();(0,i.useEffect)(()=>{let s=X.current;s&&B(),o>0?oe.current=me(Pe,o):oe.current=Pe,s&&U()},[o,W,Y]);let U=()=>{!b||X.current||(fe.current.forEach(s=>{pe.current.addEventListener(s,oe.current,{capture:!0,passive:!0})}),X.current=!0)},B=(s=!1)=>{!b||(X.current||s)&&(fe.current.forEach(L=>{pe.current.removeEventListener(L,oe.current,{capture:!0})}),X.current=!1)},G=(0,i.useCallback)(s=>{R(),U(),E.current&&re.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]),ie=(0,i.useCallback)(s=>{R(),U(),(T.current||E.current)&&(re.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]),at=(0,i.useCallback)((s,L)=>{a.current?(L&&ne.current(s),a.current.message(s)):L&&ne.current(s)},[c]),lt=(0,i.useCallback)(()=>T.current,[T]),ut=(0,i.useCallback)(()=>E.current,[E]),ct=(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?te.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,te,E,g,O]),mt=(0,i.useCallback)(()=>Math.round(I()-de.current),[de]),Ae=(0,i.useCallback)(()=>Math.round(I()-J.current),[J]),dt=(0,i.useCallback)(()=>A.current?new Date(ge.current-J.current+A.current):null,[A]),pt=(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]),ft=(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&&$e(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:ne.current,start:G,reset:ie,pause:ve,resume:Te}):a.current=null},[W,be,Le,ye,re,ne,G,ie,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?ie():G()}},[t,k,F,T]),{message:at,start:G,reset:ie,pause:ve,resume:Te,isIdle:lt,isPrompted:ut,isLeader:ct,getRemainingTime:Re,getElapsedTime:mt,getTotalElapsedTime:Ae,getLastIdleTime:dt,getLastActiveTime:pt,getTotalIdleTime:Oe,getTotalActiveTime:ft}}var j=$(require("react"));var Z=(0,j.createContext)(null);function nt(t){let e=q(t);return j.default.createElement(Z.Provider,{value:e},t.children)}var ot=Z.Consumer;function it(){return(0,j.useContext)(Z)} |
@@ -1,1 +0,1 @@ | ||
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}; | ||
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})})});import ht from"react";function vt(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 ht.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},Tt={setTimeout:qe,clearTimeout:He,setInterval:Ge,clearInterval:We};function It(){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 Et,useEffect as bt}from"react";function _(t){let e=Et(t);return bt(()=>{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 gt,{createContext as wt,useContext as yt}from"react";var ue=wt(null);function Lt(t){let e=$(t);return gt.createElement(ue.Provider,{value:e},t.children)}var Mt=ue.Consumer;function kt(){return yt(ue)}export{Mt as IdleTimerConsumer,ue as IdleTimerContext,Lt as IdleTimerProvider,It as createMocks,$ as useIdleTimer,kt as useIdleTimerContext,vt as withIdleTimer,Tt as workerTimers}; |
@@ -48,3 +48,3 @@ /// <reference types="react" /> | ||
* When promptTimeout is set, this function is called after the user becomes | ||
* idle. This is useful for displaying a confirm prompt. If the prompt timeout | ||
* idle. This is useful for displaying a confirm prompt. If the prompt timeout | ||
* is reached, onIdle is then called. | ||
@@ -51,0 +51,0 @@ * |
{ | ||
"name": "react-idle-timer", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "Activity detection for React.js", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
@@ -37,3 +37,3 @@ <p align="center"> | ||
Welcome to the all new IdleTimer! The documentation has moved to [https://idletimer.dev](https://idletimer.dev). There you can find everything you need to get started using and contributing to IdleTimer. | ||
Welcome to the all new IdleTimer! The documentation has moved to [https://idletimer.dev](https://idletimer.dev). There you can find everything you need to get started using and contributing to IdleTimer. | ||
@@ -40,0 +40,0 @@ </br> |
56796
0.08%