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