@capsitech/idle-timer
Version:
Activity detection for React.js
2 lines (1 loc) • 21.7 kB
JavaScript
var Lt=Object.create;var Xe=Object.defineProperty;var Rt=Object.getOwnPropertyDescriptor;var kt=Object.getOwnPropertyNames;var Pt=Object.getPrototypeOf,Ct=Object.prototype.hasOwnProperty;var St=(s,e)=>()=>(e||s((e={exports:{}}).exports,e),e.exports);var Mt=(s,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of kt(e))!Ct.call(s,a)&&a!==r&&Xe(s,a,{get:()=>e[a],enumerable:!(i=Rt(e,a))||i.enumerable});return s};var At=(s,e,r)=>(r=s!=null?Lt(Pt(s)):{},Mt(e||!s||!s.__esModule?Xe(r,"default",{value:s,enumerable:!0}):r,s));var Ke=St((Ee,ze)=>{(function(s,e){typeof Ee=="object"&&typeof ze<"u"?e(Ee):typeof define=="function"&&define.amd?define(["exports"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.fastUniqueNumbers={}))})(Ee,function(s){"use strict";var e=function(u){return function(p){var T=u(p);return p.add(T),T}},r=function(u){return function(p,T){return u.set(p,T),T}},i=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,a=536870912,l=a*2,b=function(u,p){return function(T){var O=p.get(T),g=O===void 0?T.size:O<l?O+1:0;if(!T.has(g))return u(T,g);if(T.size<a){for(;T.has(g);)g=Math.floor(Math.random()*l);return u(T,g)}if(T.size>i)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;T.has(g);)g=Math.floor(Math.random()*i);return u(T,g)}},M=new WeakMap,W=r(M),f=b(W,M),c=e(f);s.addUniqueNumber=c,s.generateUniqueNumber=f})});import{createContext as Ot,useContext as xt}from"react";import{jsx as _t}from"react/jsx-runtime";var ve=Ot(null);function Dt(s){let e=oe(s);return _t(ve.Provider,{value:e,children:s.children})}var Ht=ve.Consumer;function Nt(){return xt(ve)}function Te(){return Math.random().toString(36).substring(2)}var ae=At(Ke());var Qe=s=>s.method!==void 0&&s.method==="call";var Ze=s=>typeof s.id=="number"&&typeof s.result=="boolean";var et=s=>{let e=new Map([[0,()=>{}]]),r=new Map([[0,()=>{}]]),i=new Map,a=new Worker(s);return a.addEventListener("message",({data:f})=>{if(Qe(f)){let{params:{timerId:c,timerType:h}}=f;if(h==="interval"){let u=e.get(c);if(typeof u===void 0)throw new Error("The timer is in an undefined state.");if(typeof u=="number"){let p=i.get(u);if(p===void 0||p.timerId!==c||p.timerType!==h)throw new Error("The timer is in an undefined state.")}else typeof u=="function"&&u()}else if(h==="timeout"){let u=r.get(c);if(typeof u===void 0)throw new Error("The timer is in an undefined state.");if(typeof u=="number"){let p=i.get(u);if(p===void 0||p.timerId!==c||p.timerType!==h)throw new Error("The timer is in an undefined state.")}else typeof u=="function"&&(u(),r.delete(c))}}else if(Ze(f)){let{id:c}=f,h=i.get(c);if(h===void 0)throw new Error("The timer is in an undefined state.");let{timerId:u,timerType:p}=h;i.delete(c),p==="interval"?e.delete(u):r.delete(u)}else{let{error:{message:c}}=f;throw new Error(c)}}),{clearInterval:f=>{if(typeof e.get(f)=="function"){let c=(0,ae.generateUniqueNumber)(i);i.set(c,{timerId:f,timerType:"interval"}),e.set(f,c),a.postMessage({id:c,method:"clear",params:{timerId:f,timerType:"interval"}})}},clearTimeout:f=>{if(typeof r.get(f)=="function"){let c=(0,ae.generateUniqueNumber)(i);i.set(c,{timerId:f,timerType:"timeout"}),r.set(f,c),a.postMessage({id:c,method:"clear",params:{timerId:f,timerType:"timeout"}})}},setInterval:(f,c=0,...h)=>{let u=(0,ae.generateUniqueNumber)(e);return e.set(u,()=>{f(...h),typeof e.get(u)=="function"&&a.postMessage({id:null,method:"set",params:{delay:c,now:performance.timeOrigin+performance.now(),timerId:u,timerType:"interval"}})}),a.postMessage({id:null,method:"set",params:{delay:c,now:performance.timeOrigin+performance.now(),timerId:u,timerType:"interval"}}),u},setTimeout:(f,c=0,...h)=>{let u=(0,ae.generateUniqueNumber)(r);return r.set(u,()=>f(...h)),a.postMessage({id:null,method:"set",params:{delay:c,now:performance.timeOrigin+performance.now(),timerId:u,timerType:"timeout"}}),u}}};var tt=(s,e)=>{let r=null;return()=>{if(r!==null)return r;let i=new Blob([e],{type:"application/javascript; charset=utf-8"}),a=URL.createObjectURL(i);return r=s(a),setTimeout(()=>URL.revokeObjectURL(a)),r}};var rt=`(()=>{"use strict";const e=new Map,t=new Map,r=t=>{const r=e.get(t);return void 0!==r&&(clearTimeout(r),e.delete(t),!0)},s=e=>{const r=t.get(e);return void 0!==r&&(clearTimeout(r),t.delete(e),!0)},o=(e,t)=>{const r=performance.now(),s=e+t-r-performance.timeOrigin;return{expected:r+s,remainingDelay:s}},i=(e,t,r,s)=>{const o=r-performance.now();o>0?e.set(t,setTimeout(i,o,e,t,r,s)):(e.delete(t),postMessage({id:null,method:"call",params:{timerId:t,timerType:s}}))};addEventListener("message",(({data:n})=>{try{if("clear"===n.method){const{id:e,params:{timerId:t,timerType:o}}=n;if("interval"===o)postMessage({id:e,result:r(t)});else{if("timeout"!==o)throw new Error('The given type "'.concat(o,'" is not supported'));postMessage({id:e,result:s(t)})}}else{if("set"!==n.method)throw new Error('The given method "'.concat(n.method,'" is not supported'));{const{params:{delay:r,now:s,timerId:a,timerType:m}}=n;if("interval"===m)((t,r,s)=>{const{expected:n,remainingDelay:a}=o(t,s);e.set(r,setTimeout(i,a,e,r,n,"interval"))})(r,a,s);else{if("timeout"!==m)throw new Error('The given type "'.concat(m,'" is not supported'));((e,r,s)=>{const{expected:n,remainingDelay:a}=o(e,s);t.set(r,setTimeout(i,a,t,r,n,"timeout"))})(r,a,s)}}}}catch(e){postMessage({error:{message:e.message},id:n.id,result:null})}}))})();`;var Ie=tt(et,rt),nt=s=>Ie().clearInterval(s),st=s=>Ie().clearTimeout(s),it=(...s)=>Ie().setInterval(...s),ot=(...s)=>Ie().setTimeout(...s);var P=(typeof window>"u"?"undefined":typeof window)=="object";var v={setTimeout:P?setTimeout.bind(window):setTimeout,clearTimeout:P?clearTimeout.bind(window):clearTimeout,setInterval:P?setInterval.bind(window):setInterval,clearInterval:P?clearInterval.bind(window):clearInterval},ue={setTimeout:ot,clearTimeout:st,setInterval:it,clearInterval:nt};function Ft(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval,ue.setTimeout=setTimeout,ue.clearTimeout=clearTimeout,ue.setInterval=setInterval,ue.clearInterval=clearInterval}function at(s){v.setTimeout=s.setTimeout,v.clearTimeout=s.clearTimeout,v.setInterval=s.setInterval,v.clearInterval=s.clearInterval}var K={},Se=class{name;closed=!1;mc=new MessageChannel;constructor(e){this.name=e,K[e]=K[e]||[],K[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),i=`${this.name}:${String(Date.now())}${String(Math.random())}`;window.localStorage.setItem(i,r),v.setTimeout(()=>{window.localStorage.removeItem(i)},500),K[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=K[this.name].indexOf(this);K[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)}},Me=typeof window>"u"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Se;function ut(s=0){return new Promise(e=>v.setTimeout(e,s))}var be=class{options;channel;token=Te();isLeader=!1;isDead=!1;isApplying=!1;reApply=!1;intervals=[];listeners=[];deferred;constructor(e,r){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=i=>{let{token:a,action:l}=i.data;a!==this.token&&(l===0&&a>this.token&&(e=!0),l===1&&(e=!0))};this.channel.addEventListener("message",r);try{return this.sendAction(0),await ut(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(i=>{let a=()=>{if(e)return;e=!0;try{v.clearInterval(r)}catch{}let b=this.intervals.indexOf(r);b>=0&&this.intervals.splice(b,1),this.channel.removeEventListener("message",l),i()};r=v.setInterval(()=>{this.apply().then(()=>{this.isLeader&&a()})},this.options.fallbackInterval),this.intervals.push(r);let l=b=>{let{action:M}=b.data;M===2&&this.apply().then(()=>{this.isLeader&&a()})};this.channel.addEventListener("message",l)})}sendAction(e){this.channel.postMessage({action:e,token:this.token})}assumeLead(){this.isLeader=!0,this.options.onLeader();let e=r=>{let{action:i}=r.data;i===0&&this.sendAction(1)};return this.channel.addEventListener("message",e),this.listeners.push(e),this.sendAction(1)}waitForLeadership(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}close(){if(!this.isDead){this.isDead=!0,this.isLeader=!1,this.sendAction(2);try{this.listeners.forEach(e=>this.channel.removeEventListener("message",e)),this.intervals.forEach(e=>v.clearInterval(e))}catch{}}}};var ge=class{channel;options;elector;token=Te();registry=new Map;allIdle=!1;isLastActive=!1;constructor(e){let{channelName:r}=e;if(this.options=e,this.channel=new Me(r),this.registry.set(this.token,1),e.leaderElection){let i={fallbackInterval:2e3,responseTime:100,onLeader:this.options.onLeader};this.elector=new be(this.channel,i),this.elector.waitForLeadership()}this.channel.addEventListener("message",i=>{let{action:a,token:l,data:b={}}=i.data;switch(a){case 3:this.registry.set(l,2);break;case 4:this.registry.delete(l);break;case 5:this.idle(l,b.eventType);break;case 6:this.active(l);break;case 7:this.prompt(l);break;case 8:this.start(l);break;case 9:this.reset(l);break;case 10:this.activate(l);break;case 11:this.pause(l);break;case 12:this.resume(l);break;case 13:this.options.onMessage(b);break}}),this.send(3)}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(i=>i===0);e===this.token&&this.send(7),r&&this.options.onPrompt()}idle(e=this.token,r){this.registry.set(e,2);let i=[...this.registry.values()].every(a=>a===2);e===this.token&&this.send(5,r),!this.allIdle&&i&&(this.allIdle=!0,this.options.onIdle({type:r},e))}active(e=this.token){this.allIdle=!1,this.registry.set(e,1);let r=[...this.registry.values()].some(i=>i===1);e===this.token&&this.send(6),r&&this.options.onActive(),this.isLastActive=e===this.token}start(e=this.token){this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(8):this.options.start(!0),this.isLastActive=e===this.token}reset(e=this.token){this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(9):this.options.reset(!0),this.isLastActive=e===this.token}activate(e=this.token){this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(10):this.options.activate(!0),this.isLastActive=e===this.token}pause(e=this.token){e===this.token?this.send(11):this.options.pause(!0)}resume(e=this.token){e===this.token?this.send(12):this.options.resume(!0)}message(e){try{this.channel.postMessage({action:13,token:this.token,data:e})}catch{}}send(e,r){try{this.channel.postMessage({action:e,token:this.token,data:{eventType:r}})}catch{}}close(){this.options.leaderElection&&this.elector.close(),this.send(4),this.channel.close()}};import n from"react";function ct(s,e){let r;function i(...a){r&&clearTimeout(r),r=setTimeout(()=>{s(...a),r=null},e)}return i.cancel=function(){clearTimeout(r)},i}var lt=P?document:null,Ae=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange","focus"];var d=()=>Date.now();function ye(s,e){let r=0;return function(...i){let a=new Date().getTime();if(!(a-r<e))return r=a,s(...i)}}var Q=2147483647;function oe({timeout:s=1e3*60*20,promptTimeout:e=0,promptBeforeIdle:r=0,element:i=lt,events:a=Ae,timers:l=void 0,immediateEvents:b=[],immediateIdleEvents:M=[],onPresenceChange:W=()=>{},onPrompt:f=()=>{},onIdle:c=()=>{},onActive:h=()=>{},onAction:u=()=>{},onMessage:p=()=>{},onLeader:T=()=>{},debounce:O=0,throttle:g=0,eventsThrottle:we=200,startOnMount:J=!0,startManually:x=!1,stopOnIdle:ce=!1,crossTab:j=!1,name:De="idle-timer",syncTimers:Z=0,leaderElection:He=!1,disabled:A=!1}={}){let Ne=n.useRef(d()),le=n.useRef(d()),y=n.useRef(null),fe=n.useRef(null),w=n.useRef(null),V=n.useRef(0),ee=n.useRef(0),D=n.useRef(0),R=n.useRef(0),m=n.useRef(!1),E=n.useRef(!1),H=n.useRef(!1),N=n.useRef(!0),te=n.useRef(!1),_=n.useRef(null),o=n.useRef(null),C=n.useRef(s),B=n.useRef(0);n.useEffect(()=>{if(e&&console.warn("\u26A0\uFE0F IdleTimer -- The `promptTimeout` property has been deprecated in favor of `promptBeforeIdle`. It will be removed in the next major release."),r&&e)throw new Error("\u274C Both promptTimeout and promptBeforeIdle can not be set. The promptTimeout property will be deprecated in a future version.");if(s>=Q)throw new Error(`\u274C The value for the timeout property must fit in a 32 bit signed integer, ${Q}.`);if(e>=Q)throw new Error(`\u274C The value for the promptTimeout property must fit in a 32 bit signed integer, ${Q}.`);if(r>=Q)throw new Error(`\u274C The value for the promptBeforeIdle property must fit in a 32 bit signed integer, ${Q}.`);if(r>=s)throw new Error(`\u274C The value for the promptBeforeIdle property must be less than the timeout property, ${s}.`);if(r?(C.current=s-r,B.current=r):(C.current=s,B.current=e),!N.current){if(x||A)return;m.current&&(G.current(null,L),o.current&&o.current.active()),Y()}},[s,e,r,x,A]);let _e=n.useRef(ce);n.useEffect(()=>{_e.current=ce},[ce]);let Ue=n.useRef(b),Fe=n.useRef(M),re=n.useRef(i),Le=n.useRef([...new Set([...a,...b,...M]).values()]),S=n.useRef(A);n.useEffect(()=>{S.current=A,!N.current&&(A?he():x||Y())},[A]);let ne=n.useRef(W);n.useEffect(()=>{ne.current=W},[W]);let me=n.useRef(f);n.useEffect(()=>{me.current=f},[f]);let de=n.useRef(c);n.useEffect(()=>{de.current=c},[c]);let G=n.useRef(h);n.useEffect(()=>{G.current=h},[h]);let pe=n.useRef(u);n.useEffect(()=>{pe.current=u},[u]);let X=n.useRef(p);n.useEffect(()=>{X.current=p},[p]);let U=n.useMemo(()=>{let t=(I,z)=>pe.current(I,z);return O>0?ct(t,O):g>0?ye(t,g):t},[g,O]),We=n.useRef();n.useEffect(()=>{j&&Z&&(We.current=ye(()=>{o.current.active()},Z))},[j,Z]);let k=()=>{_.current!==null&&(v.clearTimeout(_.current),_.current=null)},q=(t,I=!0)=>{k(),_.current=v.setTimeout(se,t||C.current),I&&(w.current=d())},Ve=t=>{!E.current&&!m.current&&(me.current(t,L),ne.current({type:"active",prompted:!0},L)),R.current=0,D.current=d(),E.current=!0,q(B.current,!1)},Be=(t,I)=>{k(),m.current||(de.current(null,L),ne.current({type:"idle"},L)),m.current=!0,y.current=d(),fe.current=`${t}::${I}`,_e.current?$():E.current&&(D.current=0,E.current=!1)},Re=t=>{k(),(m.current||E.current)&&(G.current(t,L),ne.current({type:"active",prompted:!1},L)),E.current=!1,D.current=0,m.current=!1,V.current+=d()-y.current,ee.current+=d()-y.current,F(),q()},se=t=>{if(!m.current){U.cancel&&U.cancel();let z=d()-w.current;if(!(C.current+B.current<z)&&B.current>0&&!E.current){o.current?o.current.prompt():Ve(t);return}o.current?o.current.idle():Be(t.type),console.debug(`Page idle - ${t?t.type:""}`,d());return}o.current?o.current.active():Re(t),console.debug(`Page active - ${t?t.type:""}`,d())},ke=t=>{if(!J&&!w.current&&(w.current=d(),G.current(null,L)),U(t,L),E.current)return;if(k(),Fe.current.includes(t.type)||t.type==="visibilitychange"&&i&&i.visibilityState==="hidden"){m.current||se(t);return}if(!m.current&&Ue.current.includes(t.type)){se(t);return}let I=d()-w.current;if(m.current&&!ce||!m.current&&I>=C.current){se(t);return}H.current=!1,R.current=0,D.current=0,q(),j&&Z&&We.current()},ie=n.useRef(ke);n.useEffect(()=>{let t=te.current;t&&$(),we>0?ie.current=ye(ke,we):ie.current=ke,t&&F()},[we,g,O,pe,j,Z]);let ft=t=>{if(!J&&!w.current&&(w.current=d(),G.current()),U(t),!E.current&&(k(),!m.current&&t.type==="blur")){se(t);return}},mt=n.useRef(ft),F=()=>{P&&re.current&&(te.current||(Le.current.forEach(t=>{re.current.addEventListener(t,ie.current,{capture:!0,passive:!0})}),window.addEventListener("blur",mt.current,{capture:!0,passive:!0}),te.current=!0))},$=(t=!1)=>{P&&re.current&&(te.current||t)&&(Le.current.forEach(I=>{re.current.removeEventListener(I,ie.current,{capture:!0})}),window.removeEventListener("blur",ie.current,{capture:!0}),te.current=!1)},Y=n.useCallback(t=>S.current?!1:(k(),F(),m.current=!1,E.current=!1,H.current=!1,R.current=0,D.current=0,o.current&&!t&&o.current.start(),q(),!0),[_,m,S,C,o]),Pe=n.useCallback(t=>S.current?!1:(k(),F(),le.current=d(),ee.current+=d()-y.current,V.current=0,m.current=!1,E.current=!1,H.current=!1,R.current=0,D.current=0,o.current&&!t&&o.current.reset(),x||q(),!0),[_,m,C,x,S,o]),Ge=n.useCallback(t=>S.current?!1:(k(),F(),(m.current||E.current)&&Re(),m.current=!1,E.current=!1,H.current=!1,R.current=0,D.current=0,le.current=d(),o.current&&!t&&o.current.activate(),q(),!0),[_,m,E,S,C,o]),he=n.useCallback((t=!1)=>S.current||H.current?!1:(R.current=qe(),H.current=!0,$(),k(),o.current&&!t&&o.current.pause(),!0),[_,S,o]),Ce=n.useCallback((t=!1)=>S.current||!H.current?!1:(H.current=!1,E.current||F(),m.current||q(R.current),D.current&&(D.current=d()),o.current&&!t&&o.current.resume(),!0),[_,C,S,R,o]),dt=n.useCallback((t,I)=>(o.current?(I&&X.current(t,L),o.current.message(t)):I&&X.current(t,L),!0),[p]),pt=n.useCallback(()=>m.current,[m]),ht=n.useCallback(()=>E.current,[E]),vt=n.useCallback(()=>o.current?o.current.isLeader:null,[o]),Tt=n.useCallback(()=>o.current?o.current.isLastActive:null,[o]),Et=n.useCallback(()=>o.current?o.current.token:null,[o]),qe=n.useCallback(()=>{if(H.current)return R.current;let t=R.current?R.current:B.current+C.current,I=w.current?d()-w.current:0,z=Math.floor(t-I);return z<0?0:Math.abs(z)},[C,B,E,R,w]),$e=n.useCallback(()=>Math.round(d()-le.current),[le]),Ye=n.useCallback(()=>Math.round(d()-Ne.current),[Ne]),It=n.useCallback(()=>y.current?new Date(y.current):null,[y]),bt=n.useCallback(()=>fe.current?fe.current:null,[fe]),gt=n.useCallback(()=>w.current?new Date(w.current):null,[w]),Je=n.useCallback(()=>m.current?Math.round(d()-y.current+V.current):Math.round(V.current),[y,V]),je=n.useCallback(()=>m.current?Math.round(d()-y.current+ee.current):Math.round(ee.current),[y,ee]),yt=n.useCallback(()=>{let t=Math.round($e()-Je());return t>=0?t:0},[y,V]),wt=n.useCallback(()=>{let t=Math.round(Ye()-je());return t>=0?t:0},[y,V]);n.useEffect(()=>{if(O>0&&g>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");l&&at(l);let t=()=>{o.current&&o.current.close(),U.cancel&&U.cancel(),k(),$(!0)};return P&&window.removeEventListener("beforeunload",t),()=>{P&&window.removeEventListener("beforeunload",t),o.current&&o.current.close(),U.cancel&&U.cancel(),k(),$(!0)}},[]),n.useEffect(()=>{o.current&&o.current.close(),j?o.current=new ge({channelName:De,leaderElection:He,onPrompt:()=>{Ve()},onIdle:(t,I)=>{Be(`cross-tab-${t?t.type:""}`,I)},onActive:()=>{Re()},onMessage:t=>{X.current(t,L)},onLeader:T,start:Y,reset:Pe,activate:Ge,pause:he,resume:Ce}):o.current=null},[j,De,He,me,de,G,X,Y,Pe,he,Ce]),n.useEffect(()=>{N.current||(k(),$(!0)),!(x||A)&&(J?Y():F())},[x,J,A,N]),n.useEffect(()=>{if(!N.current){let t=[...new Set([...a,...b,...M]).values()];if($(),Le.current=t,re.current=i,Ue.current=b,Fe.current=M,x||A)return;J?Y():F()}},[i,JSON.stringify(a),JSON.stringify(b),N,x,J,A]),n.useEffect(()=>{N.current&&(N.current=!1)},[N]);let L={message:dt,start:Y,reset:Pe,activate:Ge,pause:he,resume:Ce,isIdle:pt,isPrompted:ht,isLeader:vt,isLastActiveTab:Tt,getTabId:Et,getRemainingTime:qe,getElapsedTime:$e,getTotalElapsedTime:Ye,getLastIdleTime:It,getLastIdleEvent:bt,getLastActiveTime:gt,getIdleTime:Je,getTotalIdleTime:je,getActiveTime:yt,getTotalActiveTime:wt,setOnPresenceChange:t=>{W=t,ne.current=t},setOnPrompt:t=>{f=t,me.current=t},setOnIdle:t=>{c=t,de.current=t},setOnActive:t=>{h=t,G.current=t},setOnAction:t=>{u=t,pe.current=t},setOnMessage:t=>{p=t,X.current=t}};return L}import{Component as Vt,forwardRef as Bt}from"react";import{jsx as qt}from"react/jsx-runtime";function Gt(s){return Bt(function(r,i){let a={...r},l=oe(a);return typeof i=="function"?i(l):i&&(i.current=l),qt(s,{...r,...l})})}var Oe=class extends Vt{},xe=class extends Oe{constructor(e){super(e),this.onPresenceChange&&e.setOnPresenceChange(this.onPresenceChange.bind(this)),this.onPrompt&&e.setOnPrompt(this.onPrompt.bind(this)),this.onIdle&&e.setOnIdle(this.onIdle.bind(this)),this.onActive&&e.setOnActive(this.onActive.bind(this)),this.onAction&&e.setOnAction(this.onAction.bind(this)),this.onMessage&&e.setOnMessage(this.onMessage.bind(this))}};export{Me as BroadcastChannel,Ae as DEFAULT_EVENTS,xe as IdleTimerComponent,Ht as IdleTimerConsumer,ve as IdleTimerContext,Dt as IdleTimerProvider,Ft as createMocks,oe as useIdleTimer,Nt as useIdleTimerContext,Gt as withIdleTimer,ue as workerTimers};