🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

react-idle-timer

Package Overview
Dependencies
Maintainers
1
Versions
142
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-idle-timer - npm Package Compare versions

Comparing version

to
5.3.0

2

dist/index.cjs.js

@@ -1,1 +0,1 @@

var vt=Object.create,se=Object.defineProperty;var Tt=Object.getOwnPropertyDescriptor;var It=Object.getOwnPropertyNames;var Et=Object.getPrototypeOf,bt=Object.prototype.hasOwnProperty;var _e=t=>se(t,"__esModule",{value:!0});var gt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),wt=(t,e)=>{for(var r in e)se(t,r,{get:e[r],enumerable:!0})},yt=(t,e,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of It(e))!bt.call(t,n)&&n!=="default"&&se(t,n,{get:()=>e[n],enumerable:!(r=Tt(e,n))||r.enumerable});return t},$=t=>yt(_e(se(t!=null?vt(Et(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var We=gt((le,Ue)=>{(function(t,e){typeof le=="object"&&typeof Ue!="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,_),u=e(d);t.addUniqueNumber=u,t.generateUniqueNumber=d,Object.defineProperty(t,"__esModule",{value:!0})})});_e(exports);wt(exports,{IdleTimerConsumer:()=>it,IdleTimerContext:()=>Z,IdleTimerProvider:()=>ot,createMocks:()=>$e,useIdleTimer:()=>q,useIdleTimerContext:()=>st,withIdleTimer:()=>Fe,workerTimers:()=>Xe});var ae=$(require("react"));function Fe(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=$(We());var Be=t=>t.method!==void 0&&t.method==="call";var He=t=>t.error===null&&typeof t.id=="number";var Ge=t=>{let e=new Map([[0,()=>{}]]),r=new Map([[0,()=>{}]]),n=new Map,a=new Worker(t);return a.addEventListener("message",({data:d})=>{if(Be(d)){let{params:{timerId:u,timerType:p}}=d;if(p==="interval"){let f=e.get(u);if(typeof f=="number"){let o=n.get(f);if(o===void 0||o.timerId!==u||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(u);if(typeof f=="number"){let o=n.get(f);if(o===void 0||o.timerId!==u||o.timerType!==p)throw new Error("The timer is in an undefined state.")}else if(typeof f!="undefined")f(),r.delete(u);else throw new Error("The timer is in an undefined state.")}}else if(He(d)){let{id:u}=d,p=n.get(u);if(p===void 0)throw new Error("The timer is in an undefined state.");let{timerId:f,timerType:o}=p;n.delete(u),o==="interval"?e.delete(f):r.delete(f)}else{let{error:{message:u}}=d;throw new Error(u)}}),{clearInterval:d=>{let u=(0,K.generateUniqueNumber)(n);n.set(u,{timerId:d,timerType:"interval"}),e.set(d,u),a.postMessage({id:u,method:"clear",params:{timerId:d,timerType:"interval"}})},clearTimeout:d=>{let u=(0,K.generateUniqueNumber)(n);n.set(u,{timerId:d,timerType:"timeout"}),r.set(d,u),a.postMessage({id:u,method:"clear",params:{timerId:d,timerType:"timeout"}})},setInterval:(d,u)=>{let p=(0,K.generateUniqueNumber)(e);return e.set(p,()=>{d(),typeof e.get(p)=="function"&&a.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"interval"}})}),a.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"interval"}}),p},setTimeout:(d,u)=>{let p=(0,K.generateUniqueNumber)(r);return r.set(p,d),a.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"timeout"}}),p}}};var Q=null,qe=(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 Ve=`(()=>{"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 ce=qe(Ge,Ve),je=t=>ce().clearInterval(t),Ye=t=>ce().clearTimeout(t),Je=(t,e)=>ce().setInterval(t,e),ze=(t,e)=>ce().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},Xe={setTimeout:ze,clearTimeout:Ye,setInterval:Je,clearInterval:je};function $e(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function Ke(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var V={},Qe=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)}},Ze=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Qe;function et(t=0){return new Promise(e=>v.setTimeout(e,t))}var I=()=>g?performance.now():Date.now();function ue(){return I().toString(36).substring(2)}var c;(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"})(c||(c={}));var Ee=class{constructor(e,r){this.token=ue();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===c.APPLY&&a>this.token&&(e=!0),m===c.TELL&&(e=!0))};this.channel.addEventListener("message",r);try{return this.sendAction(c.APPLY),await et(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;_===c.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===c.APPLY&&this.sendAction(c.TELL)};return this.channel.addEventListener("message",e),this.listeners.push(e),this.sendAction(c.TELL)}waitForLeadership(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}close(){this.isDead||(this.isDead=!0,this.isLeader=!1,this.sendAction(c.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=ue();this.registry=new Map;this.allIdle=!1;let{channelName:r}=e;if(this.options=e,this.channel=new Ze(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 c.REGISTER:this.registry.set(m,1);break;case c.DEREGISTER:this.registry.delete(m);break;case c.IDLE:this.idle(m);break;case c.ACTIVE:this.active(m);break;case c.PROMPT:this.prompt(m);break;case c.MESSAGE:this.options.onMessage(b);break;case c.START:this.options.start(!0);break;case c.RESET:this.options.reset(!0);break;case c.PAUSE:this.options.pause(!0);break;case c.RESUME:this.options.resume(!0);break}}),this.send(c.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(c.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(c.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(c.ACTIVE),this.allIdle&&r&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:c.RESET,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:c.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(c.DEREGISTER),this.channel.close()}};var tt=g?document:null,rt=["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 nt(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=tt,events:n=rt,timers:a=void 0,immediateEvents:m=[],onPrompt:b=()=>{},onIdle:_=()=>{},onActive:ee=()=>{},onAction:d=()=>{},onMessage:u=()=>{},debounce:p=0,throttle:f=0,eventsThrottle:o=200,startOnMount:h=!0,startManually:k=!1,stopOnIdle:y=!1,crossTab:B=!1,name:ge="idle-timer",syncTimers:Y=0,leaderElection:we=!1}={}){let J=(0,i.useRef)(I()),ye=(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),at=N(y),Le=(0,i.useRef)(m),fe=(0,i.useRef)(r),he=(0,i.useRef)([...new Set([...n,...m]).values()]),Me=N(_),re=N(ee),ke=N(b),ne=N(u),L=(0,i.useRef)();(0,i.useEffect)(()=>{L.current&&L.current.cancel&&L.current.cancel(),p>0?L.current=nt(d,p):f>0?L.current=de(d,f):L.current=d},[d,f,p]);let xe=(0,i.useRef)();(0,i.useEffect)(()=>{B&&Y&&(xe.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())},Pe=s=>{w.current=0,S.current=I(),E.current=!0,H(te.current,!1),ke.current(s)},Se=()=>{T.current=!0,A.current=I(),at.current?(R(),W()):E.current&&(S.current=0,E.current=!1),Me.current()},Re=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():Pe(s);return}l.current?l.current.idle():Se();return}l.current?l.current.active():Re(s)},Ae=s=>{if(L.current(s),E.current)return;B&&Y&&xe.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&&Le.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(Ae,o):oe.current=Ae,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(c.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(c.RESET)),H()},[D,T,C,l]),Te=(0,i.useCallback)((s=!1)=>P.current?!1:(w.current=Oe(),P.current=!0,W(),R(),l.current&&(s||l.current.send(c.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(c.RESUME)),!0):!1,[D,C,w,l]),lt=(0,i.useCallback)((s,M)=>{l.current?(M&&ne.current(s),l.current.message(s)):M&&ne.current(s)},[u]),ct=(0,i.useCallback)(()=>T.current,[T]),ut=(0,i.useCallback)(()=>E.current,[E]),mt=(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]),Oe=(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,Ne=Math.ceil(s-M);return Ne<0?0:Math.abs(Ne)},[C,te,E,w,O]),dt=(0,i.useCallback)(()=>Math.round(I()-pe.current),[pe]),De=(0,i.useCallback)(()=>Math.round(I()-J.current),[J]),pt=(0,i.useCallback)(()=>A.current?new Date(ye.current-J.current+A.current):null,[A]),ft=(0,i.useCallback)(()=>O.current?new Date(ye.current-J.current+O.current):null,[O]),Ce=(0,i.useCallback)(()=>T.current?Math.round(I()-A.current+z.current):Math.round(z.current),[A,z]),ht=(0,i.useCallback)(()=>{let s=Math.round(De()-Ce());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&&Ke(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:ge,leaderElection:we,onPrompt:()=>{Pe()},onIdle:()=>{Se()},onActive:()=>{Re()},onMessage:ne.current,start:G,reset:ie,pause:Te,resume:Ie}):l.current=null},[B,ge,we,ke,Me,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,Le.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:lt,start:G,reset:ie,pause:Te,resume:Ie,isIdle:ct,isPrompted:ut,isLeader:mt,getRemainingTime:Oe,getElapsedTime:dt,getTotalElapsedTime:De,getLastIdleTime:pt,getLastActiveTime:ft,getTotalIdleTime:Ce,getTotalActiveTime:ht}}var j=$(require("react"));var Z=(0,j.createContext)(null);function ot(t){let e=q(t);return j.default.createElement(Z.Provider,{value:e},t.children)}var it=Z.Consumer;function st(){return(0,j.useContext)(Z)}
var vt=Object.create,ie=Object.defineProperty;var Tt=Object.getOwnPropertyDescriptor;var It=Object.getOwnPropertyNames;var Et=Object.getPrototypeOf,bt=Object.prototype.hasOwnProperty;var Fe=t=>ie(t,"__esModule",{value:!0});var gt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),wt=(t,e)=>{for(var r in e)ie(t,r,{get:e[r],enumerable:!0})},yt=(t,e,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of It(e))!bt.call(t,n)&&n!=="default"&&ie(t,n,{get:()=>e[n],enumerable:!(r=Tt(e,n))||r.enumerable});return t},$=t=>yt(Fe(ie(t!=null?vt(Et(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Be=gt((ae,We)=>{(function(t,e){typeof ae=="object"&&typeof We!="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}},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,l=536870912,m=l*2,E=function(f,o){return function(h){var y=o.get(h),M=y===void 0?h.size:y<m?y+1:0;if(!h.has(M))return f(h,M);if(h.size<l){for(;h.has(M);)M=Math.floor(Math.random()*m);return f(h,M)}if(h.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;h.has(M);)M=Math.floor(Math.random()*n);return f(h,M)}},_=new WeakMap,ee=r(_),d=E(ee,_),u=e(d);t.addUniqueNumber=u,t.generateUniqueNumber=d,Object.defineProperty(t,"__esModule",{value:!0})})});Fe(exports);wt(exports,{DEFAULT_EVENTS:()=>ue,IdleTimerConsumer:()=>it,IdleTimerContext:()=>Z,IdleTimerProvider:()=>ot,createMocks:()=>Ke,useIdleTimer:()=>q,useIdleTimerContext:()=>st,withIdleTimer:()=>Ue,workerTimers:()=>$e});var se=$(require("react"));function Ue(t){return(0,se.forwardRef)(function(r,n){let l={...r};!l.onPrompt&&t.prototype.onPrompt&&(l.onPrompt=()=>{t.prototype.onPrompt()}),!l.onIdle&&t.prototype.onIdle&&(l.onIdle=()=>{t.prototype.onIdle()}),!l.onActive&&t.prototype.onActive&&(l.onActive=E=>{t.prototype.onActive(E)}),!l.onAction&&t.prototype.onAction&&(l.onAction=E=>{t.prototype.onAction(E)});let m=q(l);return typeof n=="function"?n(m):n&&(n.current=m),se.default.createElement(t,{...r,...m})})}var i=$(require("react"));var K=$(Be());var He=t=>t.method!==void 0&&t.method==="call";var Ge=t=>t.error===null&&typeof t.id=="number";var qe=t=>{let e=new Map([[0,()=>{}]]),r=new Map([[0,()=>{}]]),n=new Map,l=new Worker(t);return l.addEventListener("message",({data:d})=>{if(He(d)){let{params:{timerId:u,timerType:p}}=d;if(p==="interval"){let f=e.get(u);if(typeof f=="number"){let o=n.get(f);if(o===void 0||o.timerId!==u||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(u);if(typeof f=="number"){let o=n.get(f);if(o===void 0||o.timerId!==u||o.timerType!==p)throw new Error("The timer is in an undefined state.")}else if(typeof f!="undefined")f(),r.delete(u);else throw new Error("The timer is in an undefined state.")}}else if(Ge(d)){let{id:u}=d,p=n.get(u);if(p===void 0)throw new Error("The timer is in an undefined state.");let{timerId:f,timerType:o}=p;n.delete(u),o==="interval"?e.delete(f):r.delete(f)}else{let{error:{message:u}}=d;throw new Error(u)}}),{clearInterval:d=>{let u=(0,K.generateUniqueNumber)(n);n.set(u,{timerId:d,timerType:"interval"}),e.set(d,u),l.postMessage({id:u,method:"clear",params:{timerId:d,timerType:"interval"}})},clearTimeout:d=>{let u=(0,K.generateUniqueNumber)(n);n.set(u,{timerId:d,timerType:"timeout"}),r.set(d,u),l.postMessage({id:u,method:"clear",params:{timerId:d,timerType:"timeout"}})},setInterval:(d,u)=>{let p=(0,K.generateUniqueNumber)(e);return e.set(p,()=>{d(),typeof e.get(p)=="function"&&l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"interval"}})}),l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"interval"}}),p},setTimeout:(d,u)=>{let p=(0,K.generateUniqueNumber)(r);return r.set(p,d),l.postMessage({id:null,method:"set",params:{delay:u,now:performance.now(),timerId:p,timerType:"timeout"}}),p}}};var Q=null,Ve=(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 je=`(()=>{"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=Ve(qe,je),Ye=t=>le().clearInterval(t),Je=t=>le().clearTimeout(t),ze=(t,e)=>le().setInterval(t,e),Xe=(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},$e={setTimeout:Xe,clearTimeout:Je,setInterval:ze,clearInterval:Ye};function Ke(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function Qe(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var V={},Ze=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(l=>{l!==this&&l.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)}},et=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Ze;function tt(t=0){return new Promise(e=>v.setTimeout(e,t))}var I=()=>b?performance.now():Date.now();function ce(){return I().toString(36).substring(2)}var c;(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"})(c||(c={}));var be=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:l,action:m}=n.data;l!==this.token&&(m===c.APPLY&&l>this.token&&(e=!0),m===c.TELL&&(e=!0))};this.channel.addEventListener("message",r);try{return this.sendAction(c.APPLY),await tt(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 l=()=>{if(e)return;e=!0,v.clearInterval(r);let E=this.intervals.indexOf(r);this.intervals.splice(E,1),this.channel.removeEventListener("message",m),n()};r=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&l()})},this.options.fallbackInterval),this.intervals.push(r);let m=E=>{let{action:_}=E.data;_===c.CLOSE&&this.apply().then(()=>{this.isLeader&&l()})};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===c.APPLY&&this.sendAction(c.TELL)};return this.channel.addEventListener("message",e),this.listeners.push(e),this.sendAction(c.TELL)}waitForLeadership(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}close(){this.isDead||(this.isDead=!0,this.isLeader=!1,this.sendAction(c.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 ge=class{constructor(e){this.token=ce();this.registry=new Map;this.allIdle=!1;let{channelName:r}=e;if(this.options=e,this.channel=new et(r),this.registry.set(this.token,1),e.leaderElection){let n={fallbackInterval:2e3,responseTime:100};this.elector=new be(this.channel,n),this.elector.waitForLeadership()}this.channel.addEventListener("message",n=>{let{action:l,token:m,data:E}=n.data;switch(l){case c.REGISTER:this.registry.set(m,1);break;case c.DEREGISTER:this.registry.delete(m);break;case c.IDLE:this.idle(m);break;case c.ACTIVE:this.active(m);break;case c.PROMPT:this.prompt(m);break;case c.MESSAGE:this.options.onMessage(E);break;case c.START:this.options.start(!0);break;case c.RESET:this.options.reset(!0);break;case c.PAUSE:this.options.pause(!0);break;case c.RESUME:this.options.resume(!0);break}}),this.send(c.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(c.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(c.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(c.ACTIVE),this.allIdle&&r&&(this.allIdle=!1,this.options.onActive())}sync(){try{this.channel.postMessage({action:c.RESET,token:this.token})}catch{}}message(e){try{this.channel.postMessage({action:c.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(c.DEREGISTER),this.channel.close()}};var rt=b?document:null,ue=["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 nt(t,e){let r;function n(...l){r&&clearTimeout(r),r=setTimeout(()=>{t(...l),r=null},e)}return n.cancel=function(){clearTimeout(r)},n}function de(t,e){let r=0;return function(...n){let l=new Date().getTime();if(!(l-r<e))return r=l,t(...n)}}function q({timeout:t=1e3*60*20,promptTimeout:e=0,element:r=rt,events:n=ue,timers:l=void 0,immediateEvents:m=[],onPrompt:E=()=>{},onIdle:_=()=>{},onActive:ee=()=>{},onAction:d=()=>{},onMessage:u=()=>{},debounce:p=0,throttle:f=0,eventsThrottle:o=200,startOnMount:h=!0,startManually:y=!1,stopOnIdle:M=!1,crossTab:B=!1,name:we="idle-timer",syncTimers:Y=0,leaderElection:ye=!1}={}){let J=(0,i.useRef)(I()),Le=(0,i.useRef)(Date.now()),pe=(0,i.useRef)(I()),R=(0,i.useRef)(null),A=(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),g=(0,i.useRef)(!1),L=(0,i.useRef)(0),O=(0,i.useRef)(0),D=(0,i.useRef)(null),a=(0,i.useRef)(null),C=N(t),te=N(e),at=N(M),Me=(0,i.useRef)(m),fe=(0,i.useRef)(r),he=(0,i.useRef)([...new Set([...n,...m]).values()]),ke=N(_),re=N(ee),xe=N(E),ne=N(u),w=(0,i.useRef)();(0,i.useEffect)(()=>{w.current&&w.current.cancel&&w.current.cancel(),p>0?w.current=nt(d,p):f>0?w.current=de(d,f):w.current=d},[d,f,p]);let Pe=(0,i.useRef)();(0,i.useEffect)(()=>{B&&Y&&(Pe.current=de(()=>{a.current.sync()},Y))},[B,Y]);let S=()=>{D.current!==null&&(v.clearTimeout(D.current),D.current=null)},H=(s,k=!0)=>{S(),D.current=v.setTimeout(ve,s||C.current),k&&(A.current=I())},Se=s=>{L.current=0,O.current=I(),g.current=!0,H(te.current,!1),xe.current(s)},Re=()=>{T.current=!0,R.current=I(),at.current?(S(),W()):g.current&&(O.current=0,g.current=!1),ke.current()},Ae=s=>{g.current=!1,O.current=0,T.current=!1,z.current+=I()-R.current,U(),H(),re.current(s)},ve=s=>{if(!T.current){if(w.current.cancel&&w.current.cancel(),te.current>0&&!g.current){a.current?a.current.prompt():Se(s);return}a.current?a.current.idle():Re();return}a.current?a.current.active():Ae(s)},Oe=s=>{if(w.current(s),g.current)return;B&&Y&&Pe.current(),S();let k=I()-A.current;if((T.current&&!M||!T.current&&k>C.current)&&ve(s),P.current=!1,L.current=0,O.current=0,!T.current&&Me.current.includes(s.type)){ve(s);return}H(),a.current&&a.current.active()},oe=(0,i.useRef)();(0,i.useEffect)(()=>{let s=X.current;s&&W(),o>0?oe.current=de(Oe,o):oe.current=Oe,s&&U()},[o,B,Y]);let U=()=>{!b||X.current||(he.current.forEach(s=>{fe.current.addEventListener(s,oe.current,{capture:!0,passive:!0})}),X.current=!0)},W=(s=!1)=>{!b||(X.current||s)&&(he.current.forEach(k=>{fe.current.removeEventListener(k,oe.current,{capture:!0})}),X.current=!1)},G=(0,i.useCallback)(s=>{S(),U(),g.current&&re.current(),T.current=!1,g.current=!1,P.current=!1,L.current=0,O.current=0,a.current&&!s&&(a.current.active(),a.current.send(c.START)),H()},[D,T,C,a]),Te=(0,i.useCallback)(s=>{S(),U(),T.current=!1,g.current=!1,P.current=!1,L.current=0,O.current=0,pe.current=I(),a.current&&(a.current.allIdle=!1,s||a.current.send(c.RESET)),y||H()},[D,T,C,y,a]),Ie=(0,i.useCallback)((s=!1)=>P.current?!1:(L.current=De(),P.current=!0,W(),S(),a.current&&(s||a.current.send(c.PAUSE)),!0),[D,a]),Ee=(0,i.useCallback)((s=!1)=>P.current?(P.current=!1,g.current||U(),T.current||H(L.current),O.current&&(O.current=I()),a.current&&(s||a.current.send(c.RESUME)),!0):!1,[D,C,L,a]),lt=(0,i.useCallback)((s,k)=>{a.current?(k&&ne.current(s),a.current.message(s)):k&&ne.current(s)},[u]),ct=(0,i.useCallback)(()=>T.current,[T]),ut=(0,i.useCallback)(()=>g.current,[g]),mt=(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]),De=(0,i.useCallback)(()=>{if(P.current)return L.current;let s=L.current?L.current:te.current+C.current,k=A.current?I()-A.current:0,_e=Math.ceil(s-k);return _e<0?0:Math.abs(_e)},[C,te,g,L,A]),dt=(0,i.useCallback)(()=>Math.round(I()-pe.current),[pe]),Ce=(0,i.useCallback)(()=>Math.round(I()-J.current),[J]),pt=(0,i.useCallback)(()=>R.current?new Date(Le.current-J.current+R.current):null,[R]),ft=(0,i.useCallback)(()=>A.current?new Date(Le.current-J.current+A.current):null,[A]),Ne=(0,i.useCallback)(()=>T.current?Math.round(I()-R.current+z.current):Math.round(z.current),[R,z]),ht=(0,i.useCallback)(()=>{let s=Math.round(Ce()-Ne());return s>=0?s:0},[J,R,z]);return(0,i.useEffect)(()=>{if(p>0&&f>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");l&&Qe(l);let s=()=>{a.current&&a.current.close(),w.current.cancel&&w.current.cancel(),S(),W(!0)};return b&&window.addEventListener("beforeunload",s),()=>{b&&window.removeEventListener("beforeunload",s),a.current&&a.current.close(),w.current.cancel&&w.current.cancel(),S(),W(!0)}},[]),(0,i.useEffect)(()=>{a.current&&a.current.close(),B?a.current=new ge({channelName:we,leaderElection:ye,onPrompt:()=>{Se()},onIdle:()=>{Re()},onActive:()=>{Ae()},onMessage:ne.current,start:G,reset:Te,pause:Ie,resume:Ee}):a.current=null},[B,we,ye,xe,ke,re,ne,G,Te,Ie,Ee]),(0,i.useEffect)(()=>{S(),W(!0),F.current||(P.current=!0,T.current=!0,L.current=0),!y&&(h?G():U())},[y,h,F]),(0,i.useEffect)(()=>{if(!F.current){let s=[...new Set([...n,...m]).values()];if(W(),he.current=s,fe.current=r,Me.current=m,y)return;h?G():U()}},[r,JSON.stringify(n),JSON.stringify(m),F,y,h]),(0,i.useEffect)(()=>{if(F.current)F.current=!1;else{if(C.current=t,y)return;T.current&&(re.current(),a.current&&a.current.active()),G()}},[t,a,y,F,T]),{message:lt,start:G,reset:Te,pause:Ie,resume:Ee,isIdle:ct,isPrompted:ut,isLeader:mt,getRemainingTime:De,getElapsedTime:dt,getTotalElapsedTime:Ce,getLastIdleTime:pt,getLastActiveTime:ft,getTotalIdleTime:Ne,getTotalActiveTime:ht}}var j=$(require("react"));var Z=(0,j.createContext)(null);function ot(t){let e=q(t);return j.default.createElement(Z.Provider,{value:e},t.children)}var it=Z.Consumer;function st(){return(0,j.useContext)(Z)}

@@ -5,2 +5,3 @@ export { withIdleTimer } from './withIdleTimer';

export { createMocks, workerTimers } from './utils/timers';
export { DEFAULT_EVENTS } from './utils/defaults';
export { IIdleTimer } from './types/IIdleTimer';

@@ -7,0 +8,0 @@ export { IIdleTimerProps } from './types/IIdleTimerProps';

@@ -1,1 +0,1 @@

var at=Object.create,ve=Object.defineProperty;var lt=Object.getOwnPropertyDescriptor;var ct=Object.getOwnPropertyNames;var ut=Object.getPrototypeOf,mt=Object.prototype.hasOwnProperty;var dt=t=>ve(t,"__esModule",{value:!0});var pt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var ft=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ct(e))!mt.call(t,r)&&r!=="default"&&ve(t,r,{get:()=>e[r],enumerable:!(n=lt(e,r))||n.enumerable});return t},ht=t=>ft(dt(ve(t!=null?at(ut(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var Ne=pt((ie,Ce)=>{(function(t,e){typeof ie=="object"&&typeof Ce!="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,u=s*2,g=function(p,o){return function(f){var x=o.get(f),L=x===void 0?f.size:x<u?x+1:0;if(!f.has(L))return p(f,L);if(f.size<s){for(;f.has(L);)L=Math.floor(Math.random()*u);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),c=e(m);t.addUniqueNumber=c,t.generateUniqueNumber=m,Object.defineProperty(t,"__esModule",{value:!0})})});import vt,{forwardRef as Tt}from"react";function It(t){return Tt(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 u=$(s);return typeof r=="function"?r(u):r&&(r.current=u),vt.createElement(t,{...n,...u})})}import{useEffect as F,useRef as h,useCallback as b}from"react";var K=ht(Ne());var _e=t=>t.method!==void 0&&t.method==="call";var Fe=t=>t.error===null&&typeof t.id=="number";var Ue=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,s=new Worker(t);return s.addEventListener("message",({data:m})=>{if(_e(m)){let{params:{timerId:c,timerType:d}}=m;if(d==="interval"){let p=e.get(c);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==c||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(c);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==c||o.timerType!==d)throw new Error("The timer is in an undefined state.")}else if(typeof p!="undefined")p(),n.delete(c);else throw new Error("The timer is in an undefined state.")}}else if(Fe(m)){let{id:c}=m,d=r.get(c);if(d===void 0)throw new Error("The timer is in an undefined state.");let{timerId:p,timerType:o}=d;r.delete(c),o==="interval"?e.delete(p):n.delete(p)}else{let{error:{message:c}}=m;throw new Error(c)}}),{clearInterval:m=>{let c=(0,K.generateUniqueNumber)(r);r.set(c,{timerId:m,timerType:"interval"}),e.set(m,c),s.postMessage({id:c,method:"clear",params:{timerId:m,timerType:"interval"}})},clearTimeout:m=>{let c=(0,K.generateUniqueNumber)(r);r.set(c,{timerId:m,timerType:"timeout"}),n.set(m,c),s.postMessage({id:c,method:"clear",params:{timerId:m,timerType:"timeout"}})},setInterval:(m,c)=>{let d=(0,K.generateUniqueNumber)(e);return e.set(d,()=>{m(),typeof e.get(d)=="function"&&s.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"interval"}})}),s.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"interval"}}),d},setTimeout:(m,c)=>{let d=(0,K.generateUniqueNumber)(n);return n.set(d,m),s.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"timeout"}}),d}}};var Q=null,We=(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=We(Ue,Be),He=t=>se().clearInterval(t),Ge=t=>se().clearTimeout(t),qe=(t,e)=>se().setInterval(t,e),Ve=(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},Et={setTimeout:Ve,clearTimeout:Ge,setInterval:qe,clearInterval:He};function bt(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function je(t){v.setTimeout=t.setTimeout,v.clearTimeout=t.clearTimeout,v.setInterval=t.setInterval,v.clearInterval=t.clearInterval}var j={},Ye=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)}},Je=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Ye;function ze(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:u}=r.data;s!==this.token&&(u===l.APPLY&&s>this.token&&(e=!0),u===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 s=()=>{if(e)return;e=!0,v.clearInterval(n);let g=this.intervals.indexOf(n);this.intervals.splice(g,1),this.channel.removeEventListener("message",u),r()};n=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&s()})},this.options.fallbackInterval),this.intervals.push(n);let u=g=>{let{action:U}=g.data;U===l.CLOSE&&this.apply().then(()=>{this.isLeader&&s()})};this.channel.addEventListener("message",u)})}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 Je(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:u,data:g}=r.data;switch(s){case l.REGISTER:this.registry.set(u,1);break;case l.DEREGISTER:this.registry.delete(u);break;case l.IDLE:this.idle(u);break;case l.ACTIVE:this.active(u);break;case l.PROMPT:this.prompt(u);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 Xe=w?document:null,$e=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];import{useRef as gt,useEffect as wt}from"react";function _(t){let e=gt(t);return wt(()=>{e.current=t},[t]),e}function Ke(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=Xe,events:r=$e,timers:s=void 0,immediateEvents:u=[],onPrompt:g=()=>{},onIdle:U=()=>{},onActive:Z=()=>{},onAction:m=()=>{},onMessage:c=()=>{},debounce:d=0,throttle:p=0,eventsThrottle:o=200,startOnMount:f=!0,startManually:x=!1,stopOnIdle:L=!1,crossTab:G=!1,name:Ee="idle-timer",syncTimers:Y=0,leaderElection:be=!1}={}){let J=h(I()),ge=h(Date.now()),ue=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),Qe=_(L),we=h(u),me=h(n),de=h([...new Set([...r,...u]).values()]),ye=_(U),te=_(Z),Le=_(g),re=_(c),M=h();F(()=>{M.current&&M.current.cancel&&M.current.cancel(),d>0?M.current=Ke(m,d):p>0?M.current=le(m,p):M.current=m},[m,p,d]);let Me=h();F(()=>{G&&Y&&(Me.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())},ke=i=>{y.current=0,R.current=I(),E.current=!0,q(ee.current,!1),Le.current(i)},xe=()=>{T.current=!0,O.current=I(),Qe.current?(A(),H()):E.current&&(R.current=0,E.current=!1),ye.current()},Pe=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():ke(i);return}a.current?a.current.idle():xe();return}a.current?a.current.active():Pe(i)},Se=i=>{if(M.current(i),E.current)return;G&&Y&&Me.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&&we.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(Se,o):ne.current=Se,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,ue.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=Re(),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]),Ze=b((i,k)=>{a.current?(k&&re.current(i),a.current.message(i)):k&&re.current(i)},[c]),et=b(()=>T.current,[T]),tt=b(()=>E.current,[E]),rt=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]),Re=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,De=Math.ceil(i-k);return De<0?0:Math.abs(De)},[N,ee,E,y,D]),nt=b(()=>Math.round(I()-ue.current),[ue]),Ae=b(()=>Math.round(I()-J.current),[J]),ot=b(()=>O.current?new Date(ge.current-J.current+O.current):null,[O]),it=b(()=>D.current?new Date(ge.current-J.current+D.current):null,[D]),Oe=b(()=>T.current?Math.round(I()-O.current+z.current):Math.round(z.current),[O,z]),st=b(()=>{let i=Math.round(Ae()-Oe());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&&je(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:Ee,leaderElection:be,onPrompt:()=>{ke()},onIdle:()=>{xe()},onActive:()=>{Pe()},onMessage:re.current,start:V,reset:oe,pause:fe,resume:he}):a.current=null},[G,Ee,be,Le,ye,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,...u]).values()];if(H(),de.current=i,me.current=n,we.current=u,x)return;f?V():B()}},[n,JSON.stringify(r),JSON.stringify(u),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:Ze,start:V,reset:oe,pause:fe,resume:he,isIdle:et,isPrompted:tt,isLeader:rt,getRemainingTime:Re,getElapsedTime:nt,getTotalElapsedTime:Ae,getLastIdleTime:ot,getLastActiveTime:it,getTotalIdleTime:Oe,getTotalActiveTime:st}}import yt,{createContext as Lt,useContext as Mt}from"react";var ce=Lt(null);function kt(t){let e=$(t);return yt.createElement(ce.Provider,{value:e},t.children)}var xt=ce.Consumer;function Pt(){return Mt(ce)}export{xt as IdleTimerConsumer,ce as IdleTimerContext,kt as IdleTimerProvider,bt as createMocks,$ as useIdleTimer,Pt as useIdleTimerContext,It as withIdleTimer,Et as workerTimers};
var at=Object.create,ve=Object.defineProperty;var lt=Object.getOwnPropertyDescriptor;var ct=Object.getOwnPropertyNames;var ut=Object.getPrototypeOf,mt=Object.prototype.hasOwnProperty;var dt=t=>ve(t,"__esModule",{value:!0});var pt=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var ft=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ct(e))!mt.call(t,r)&&r!=="default"&&ve(t,r,{get:()=>e[r],enumerable:!(n=lt(e,r))||n.enumerable});return t},ht=t=>ft(dt(ve(t!=null?at(ut(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var _e=pt((oe,Ne)=>{(function(t,e){typeof oe=="object"&&typeof Ne!="undefined"?e(oe):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.fastUniqueNumbers={}))})(oe,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,a=536870912,u=a*2,b=function(p,o){return function(f){var L=o.get(f),k=L===void 0?f.size:L<u?L+1:0;if(!f.has(k))return p(f,k);if(f.size<a){for(;f.has(k);)k=Math.floor(Math.random()*u);return p(f,k)}if(f.size>r)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;f.has(k);)k=Math.floor(Math.random()*r);return p(f,k)}},U=new WeakMap,Z=n(U),m=b(Z,U),c=e(m);t.addUniqueNumber=c,t.generateUniqueNumber=m,Object.defineProperty(t,"__esModule",{value:!0})})});import vt,{forwardRef as Tt}from"react";function It(t){return Tt(function(n,r){let a={...n};!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 u=$(a);return typeof r=="function"?r(u):r&&(r.current=u),vt.createElement(t,{...n,...u})})}import{useEffect as F,useRef as h,useCallback as E}from"react";var K=ht(_e());var Fe=t=>t.method!==void 0&&t.method==="call";var Ue=t=>t.error===null&&typeof t.id=="number";var We=t=>{let e=new Map([[0,()=>{}]]),n=new Map([[0,()=>{}]]),r=new Map,a=new Worker(t);return a.addEventListener("message",({data:m})=>{if(Fe(m)){let{params:{timerId:c,timerType:d}}=m;if(d==="interval"){let p=e.get(c);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==c||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(c);if(typeof p=="number"){let o=r.get(p);if(o===void 0||o.timerId!==c||o.timerType!==d)throw new Error("The timer is in an undefined state.")}else if(typeof p!="undefined")p(),n.delete(c);else throw new Error("The timer is in an undefined state.")}}else if(Ue(m)){let{id:c}=m,d=r.get(c);if(d===void 0)throw new Error("The timer is in an undefined state.");let{timerId:p,timerType:o}=d;r.delete(c),o==="interval"?e.delete(p):n.delete(p)}else{let{error:{message:c}}=m;throw new Error(c)}}),{clearInterval:m=>{let c=(0,K.generateUniqueNumber)(r);r.set(c,{timerId:m,timerType:"interval"}),e.set(m,c),a.postMessage({id:c,method:"clear",params:{timerId:m,timerType:"interval"}})},clearTimeout:m=>{let c=(0,K.generateUniqueNumber)(r);r.set(c,{timerId:m,timerType:"timeout"}),n.set(m,c),a.postMessage({id:c,method:"clear",params:{timerId:m,timerType:"timeout"}})},setInterval:(m,c)=>{let d=(0,K.generateUniqueNumber)(e);return e.set(d,()=>{m(),typeof e.get(d)=="function"&&a.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"interval"}})}),a.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"interval"}}),d},setTimeout:(m,c)=>{let d=(0,K.generateUniqueNumber)(n);return n.set(d,m),a.postMessage({id:null,method:"set",params:{delay:c,now:performance.now(),timerId:d,timerType:"timeout"}}),d}}};var Q=null,Be=(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 He=`(()=>{"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 ie=Be(We,He),Ge=t=>ie().clearInterval(t),qe=t=>ie().clearTimeout(t),Ve=(t,e)=>ie().setInterval(t,e),je=(t,e)=>ie().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},Et={setTimeout:je,clearTimeout:qe,setInterval:Ve,clearInterval:Ge};function bt(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval}function Ye(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(a=>{a!==this&&a.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)}},ze=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Je;function Xe(t=0){return new Promise(e=>v.setTimeout(e,t))}var I=()=>g?performance.now():Date.now();function se(){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=se();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:a,action:u}=r.data;a!==this.token&&(u===l.APPLY&&a>this.token&&(e=!0),u===l.TELL&&(e=!0))};this.channel.addEventListener("message",n);try{return this.sendAction(l.APPLY),await Xe(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 a=()=>{if(e)return;e=!0,v.clearInterval(n);let b=this.intervals.indexOf(n);this.intervals.splice(b,1),this.channel.removeEventListener("message",u),r()};n=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&a()})},this.options.fallbackInterval),this.intervals.push(n);let u=b=>{let{action:U}=b.data;U===l.CLOSE&&this.apply().then(()=>{this.isLeader&&a()})};this.channel.addEventListener("message",u)})}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=se();this.registry=new Map;this.allIdle=!1;let{channelName:n}=e;if(this.options=e,this.channel=new ze(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:a,token:u,data:b}=r.data;switch(a){case l.REGISTER:this.registry.set(u,1);break;case l.DEREGISTER:this.registry.delete(u);break;case l.IDLE:this.idle(u);break;case l.ACTIVE:this.active(u);break;case l.PROMPT:this.prompt(u);break;case l.MESSAGE:this.options.onMessage(b);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 $e=g?document:null,Ee=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];import{useRef as gt,useEffect as wt}from"react";function _(t){let e=gt(t);return wt(()=>{e.current=t},[t]),e}function Ke(t,e){let n;function r(...a){n&&clearTimeout(n),n=setTimeout(()=>{t(...a),n=null},e)}return r.cancel=function(){clearTimeout(n)},r}function ae(t,e){let n=0;return function(...r){let a=new Date().getTime();if(!(a-n<e))return n=a,t(...r)}}function $({timeout:t=1e3*60*20,promptTimeout:e=0,element:n=$e,events:r=Ee,timers:a=void 0,immediateEvents:u=[],onPrompt:b=()=>{},onIdle:U=()=>{},onActive:Z=()=>{},onAction:m=()=>{},onMessage:c=()=>{},debounce:d=0,throttle:p=0,eventsThrottle:o=200,startOnMount:f=!0,startManually:L=!1,stopOnIdle:k=!1,crossTab:G=!1,name:be="idle-timer",syncTimers:Y=0,leaderElection:ge=!1}={}){let J=h(I()),we=h(Date.now()),ce=h(I()),A=h(null),O=h(null),z=h(0),T=h(!0),S=h(!1),W=h(!0),X=h(!1),w=h(!1),M=h(0),D=h(0),C=h(null),s=h(null),N=_(t),ee=_(e),Qe=_(k),ye=h(u),ue=h(n),me=h([...new Set([...r,...u]).values()]),Le=_(U),te=_(Z),Me=_(b),re=_(c),y=h();F(()=>{y.current&&y.current.cancel&&y.current.cancel(),d>0?y.current=Ke(m,d):p>0?y.current=ae(m,p):y.current=m},[m,p,d]);let ke=h();F(()=>{G&&Y&&(ke.current=ae(()=>{s.current.sync()},Y))},[G,Y]);let R=()=>{C.current!==null&&(v.clearTimeout(C.current),C.current=null)},q=(i,x=!0)=>{R(),C.current=v.setTimeout(de,i||N.current),x&&(O.current=I())},xe=i=>{M.current=0,D.current=I(),w.current=!0,q(ee.current,!1),Me.current(i)},Pe=()=>{T.current=!0,A.current=I(),Qe.current?(R(),H()):w.current&&(D.current=0,w.current=!1),Le.current()},Se=i=>{w.current=!1,D.current=0,T.current=!1,z.current+=I()-A.current,B(),q(),te.current(i)},de=i=>{if(!T.current){if(y.current.cancel&&y.current.cancel(),ee.current>0&&!w.current){s.current?s.current.prompt():xe(i);return}s.current?s.current.idle():Pe();return}s.current?s.current.active():Se(i)},Re=i=>{if(y.current(i),w.current)return;G&&Y&&ke.current(),R();let x=I()-O.current;if((T.current&&!k||!T.current&&x>N.current)&&de(i),S.current=!1,M.current=0,D.current=0,!T.current&&ye.current.includes(i.type)){de(i);return}q(),s.current&&s.current.active()},ne=h();F(()=>{let i=X.current;i&&H(),o>0?ne.current=ae(Re,o):ne.current=Re,i&&B()},[o,G,Y]);let B=()=>{!g||X.current||(me.current.forEach(i=>{ue.current.addEventListener(i,ne.current,{capture:!0,passive:!0})}),X.current=!0)},H=(i=!1)=>{!g||(X.current||i)&&(me.current.forEach(x=>{ue.current.removeEventListener(x,ne.current,{capture:!0})}),X.current=!1)},V=E(i=>{R(),B(),w.current&&te.current(),T.current=!1,w.current=!1,S.current=!1,M.current=0,D.current=0,s.current&&!i&&(s.current.active(),s.current.send(l.START)),q()},[C,T,N,s]),pe=E(i=>{R(),B(),T.current=!1,w.current=!1,S.current=!1,M.current=0,D.current=0,ce.current=I(),s.current&&(s.current.allIdle=!1,i||s.current.send(l.RESET)),L||q()},[C,T,N,L,s]),fe=E((i=!1)=>S.current?!1:(M.current=Ae(),S.current=!0,H(),R(),s.current&&(i||s.current.send(l.PAUSE)),!0),[C,s]),he=E((i=!1)=>S.current?(S.current=!1,w.current||B(),T.current||q(M.current),D.current&&(D.current=I()),s.current&&(i||s.current.send(l.RESUME)),!0):!1,[C,N,M,s]),Ze=E((i,x)=>{s.current?(x&&re.current(i),s.current.message(i)):x&&re.current(i)},[c]),et=E(()=>T.current,[T]),tt=E(()=>w.current,[w]),rt=E(()=>{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]),Ae=E(()=>{if(S.current)return M.current;let i=M.current?M.current:ee.current+N.current,x=O.current?I()-O.current:0,Ce=Math.ceil(i-x);return Ce<0?0:Math.abs(Ce)},[N,ee,w,M,O]),nt=E(()=>Math.round(I()-ce.current),[ce]),Oe=E(()=>Math.round(I()-J.current),[J]),ot=E(()=>A.current?new Date(we.current-J.current+A.current):null,[A]),it=E(()=>O.current?new Date(we.current-J.current+O.current):null,[O]),De=E(()=>T.current?Math.round(I()-A.current+z.current):Math.round(z.current),[A,z]),st=E(()=>{let i=Math.round(Oe()-De());return i>=0?i:0},[J,A,z]);return F(()=>{if(d>0&&p>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");a&&Ye(a);let i=()=>{s.current&&s.current.close(),y.current.cancel&&y.current.cancel(),R(),H(!0)};return g&&window.addEventListener("beforeunload",i),()=>{g&&window.removeEventListener("beforeunload",i),s.current&&s.current.close(),y.current.cancel&&y.current.cancel(),R(),H(!0)}},[]),F(()=>{s.current&&s.current.close(),G?s.current=new Ie({channelName:be,leaderElection:ge,onPrompt:()=>{xe()},onIdle:()=>{Pe()},onActive:()=>{Se()},onMessage:re.current,start:V,reset:pe,pause:fe,resume:he}):s.current=null},[G,be,ge,Me,Le,te,re,V,pe,fe,he]),F(()=>{R(),H(!0),W.current||(S.current=!0,T.current=!0,M.current=0),!L&&(f?V():B())},[L,f,W]),F(()=>{if(!W.current){let i=[...new Set([...r,...u]).values()];if(H(),me.current=i,ue.current=n,ye.current=u,L)return;f?V():B()}},[n,JSON.stringify(r),JSON.stringify(u),W,L,f]),F(()=>{if(W.current)W.current=!1;else{if(N.current=t,L)return;T.current&&(te.current(),s.current&&s.current.active()),V()}},[t,s,L,W,T]),{message:Ze,start:V,reset:pe,pause:fe,resume:he,isIdle:et,isPrompted:tt,isLeader:rt,getRemainingTime:Ae,getElapsedTime:nt,getTotalElapsedTime:Oe,getLastIdleTime:ot,getLastActiveTime:it,getTotalIdleTime:De,getTotalActiveTime:st}}import yt,{createContext as Lt,useContext as Mt}from"react";var le=Lt(null);function kt(t){let e=$(t);return yt.createElement(le.Provider,{value:e},t.children)}var xt=le.Consumer;function Pt(){return Mt(le)}export{Ee as DEFAULT_EVENTS,xt as IdleTimerConsumer,le as IdleTimerContext,kt as IdleTimerProvider,bt as createMocks,$ as useIdleTimer,Pt as useIdleTimerContext,It as withIdleTimer,Et as workerTimers};
{
"name": "react-idle-timer",
"version": "5.2.0",
"version": "5.3.0",
"description": "Activity detection for React.js",

@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js",