UNPKG

@capsitech/idle-timer

Version:

Activity detection for React.js

2 lines (1 loc) 22.4 kB
var Ht=Object.create;var Te=Object.defineProperty;var Nt=Object.getOwnPropertyDescriptor;var _t=Object.getOwnPropertyNames;var Ut=Object.getPrototypeOf,Ft=Object.prototype.hasOwnProperty;var Wt=(s,e)=>()=>(e||s((e={exports:{}}).exports,e),e.exports),Vt=(s,e)=>{for(var r in e)Te(s,r,{get:e[r],enumerable:!0})},Qe=(s,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of _t(e))!Ft.call(s,a)&&a!==r&&Te(s,a,{get:()=>e[a],enumerable:!(i=Nt(e,a))||i.enumerable});return s};var Ze=(s,e,r)=>(r=s!=null?Ht(Ut(s)):{},Qe(e||!s||!s.__esModule?Te(r,"default",{value:s,enumerable:!0}):r,s)),Bt=s=>Qe(Te({},"__esModule",{value:!0}),s);var it=Wt((be,st)=>{(function(s,e){typeof be=="object"&&typeof st<"u"?e(be):typeof define=="function"&&define.amd?define(["exports"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.fastUniqueNumbers={}))})(be,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})});var $t={};Vt($t,{BroadcastChannel:()=>ye,DEFAULT_EVENTS:()=>Re,IdleTimerComponent:()=>Pe,IdleTimerConsumer:()=>tt,IdleTimerContext:()=>ue,IdleTimerProvider:()=>et,createMocks:()=>ht,useIdleTimer:()=>K,useIdleTimerContext:()=>rt,withIdleTimer:()=>bt,workerTimers:()=>Q});module.exports=Bt($t);var Ee=require("react");var nt=require("react/jsx-runtime"),ue=(0,Ee.createContext)(null);function et(s){let e=K(s);return(0,nt.jsx)(ue.Provider,{value:e,children:s.children})}var tt=ue.Consumer;function rt(){return(0,Ee.useContext)(ue)}function Ie(){return Math.random().toString(36).substring(2)}var ce=Ze(it());var ot=s=>s.method!==void 0&&s.method==="call";var at=s=>typeof s.id=="number"&&typeof s.result=="boolean";var ut=s=>{let e=new Map([[0,()=>{}]]),r=new Map([[0,()=>{}]]),i=new Map,a=new Worker(s);return a.addEventListener("message",({data:f})=>{if(ot(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(at(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,ce.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,ce.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,ce.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,ce.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 ct=(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 lt=`(()=>{"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 ge=ct(ut,lt),ft=s=>ge().clearInterval(s),mt=s=>ge().clearTimeout(s),dt=(...s)=>ge().setInterval(...s),pt=(...s)=>ge().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},Q={setTimeout:pt,clearTimeout:mt,setInterval:dt,clearInterval:ft};function ht(){v.setTimeout=setTimeout,v.clearTimeout=clearTimeout,v.setInterval=setInterval,v.clearInterval=clearInterval,Q.setTimeout=setTimeout,Q.clearTimeout=clearTimeout,Q.setInterval=setInterval,Q.clearInterval=clearInterval}function vt(s){v.setTimeout=s.setTimeout,v.clearTimeout=s.clearTimeout,v.setInterval=s.setInterval,v.clearInterval=s.clearInterval}var Z={},He=class{name;closed=!1;mc=new MessageChannel;constructor(e){this.name=e,Z[e]=Z[e]||[],Z[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),Z[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=Z[this.name].indexOf(this);Z[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)}},ye=typeof window>"u"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:He;function Tt(s=0){return new Promise(e=>v.setTimeout(e,s))}var we=class{options;channel;token=Ie();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 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(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 Le=class{channel;options;elector;token=Ie();registry=new Map;allIdle=!1;isLastActive=!1;constructor(e){let{channelName:r}=e;if(this.options=e,this.channel=new ye(r),this.registry.set(this.token,1),e.leaderElection){let i={fallbackInterval:2e3,responseTime:100,onLeader:this.options.onLeader};this.elector=new we(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()}};var n=Ze(require("react"));function Et(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 It=P?document:null,Re=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange","focus"];var d=()=>Date.now();function ke(s,e){let r=0;return function(...i){let a=new Date().getTime();if(!(a-r<e))return r=a,s(...i)}}var ee=2147483647;function K({timeout:s=1e3*60*20,promptTimeout:e=0,promptBeforeIdle:r=0,element:i=It,events:a=Re,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:Se=200,startOnMount:J=!0,startManually:x=!1,stopOnIdle:le=!1,crossTab:j=!1,name:_e="idle-timer",syncTimers:te=0,leaderElection:Ue=!1,disabled:A=!1}={}){let Fe=n.default.useRef(d()),fe=n.default.useRef(d()),y=n.default.useRef(null),me=n.default.useRef(null),w=n.default.useRef(null),V=n.default.useRef(0),re=n.default.useRef(0),D=n.default.useRef(0),R=n.default.useRef(0),m=n.default.useRef(!1),E=n.default.useRef(!1),H=n.default.useRef(!1),N=n.default.useRef(!0),ne=n.default.useRef(!1),_=n.default.useRef(null),o=n.default.useRef(null),C=n.default.useRef(s),B=n.default.useRef(0);n.default.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>=ee)throw new Error(`\u274C The value for the timeout property must fit in a 32 bit signed integer, ${ee}.`);if(e>=ee)throw new Error(`\u274C The value for the promptTimeout property must fit in a 32 bit signed integer, ${ee}.`);if(r>=ee)throw new Error(`\u274C The value for the promptBeforeIdle property must fit in a 32 bit signed integer, ${ee}.`);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 We=n.default.useRef(le);n.default.useEffect(()=>{We.current=le},[le]);let Ve=n.default.useRef(b),Be=n.default.useRef(M),se=n.default.useRef(i),Me=n.default.useRef([...new Set([...a,...b,...M]).values()]),S=n.default.useRef(A);n.default.useEffect(()=>{S.current=A,!N.current&&(A?ve():x||Y())},[A]);let ie=n.default.useRef(W);n.default.useEffect(()=>{ie.current=W},[W]);let de=n.default.useRef(f);n.default.useEffect(()=>{de.current=f},[f]);let pe=n.default.useRef(c);n.default.useEffect(()=>{pe.current=c},[c]);let G=n.default.useRef(h);n.default.useEffect(()=>{G.current=h},[h]);let he=n.default.useRef(u);n.default.useEffect(()=>{he.current=u},[u]);let X=n.default.useRef(p);n.default.useEffect(()=>{X.current=p},[p]);let U=n.default.useMemo(()=>{let t=(I,z)=>he.current(I,z);return O>0?Et(t,O):g>0?ke(t,g):t},[g,O]),Ge=n.default.useRef();n.default.useEffect(()=>{j&&te&&(Ge.current=ke(()=>{o.current.active()},te))},[j,te]);let k=()=>{_.current!==null&&(v.clearTimeout(_.current),_.current=null)},q=(t,I=!0)=>{k(),_.current=v.setTimeout(oe,t||C.current),I&&(w.current=d())},qe=t=>{!E.current&&!m.current&&(de.current(t,L),ie.current({type:"active",prompted:!0},L)),R.current=0,D.current=d(),E.current=!0,q(B.current,!1)},$e=(t,I)=>{k(),m.current||(pe.current(null,L),ie.current({type:"idle"},L)),m.current=!0,y.current=d(),me.current=`${t}::${I}`,We.current?$():E.current&&(D.current=0,E.current=!1)},Ae=t=>{k(),(m.current||E.current)&&(G.current(t,L),ie.current({type:"active",prompted:!1},L)),E.current=!1,D.current=0,m.current=!1,V.current+=d()-y.current,re.current+=d()-y.current,F(),q()},oe=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():qe(t);return}o.current?o.current.idle():$e(t.type),console.debug(`Page idle - ${t?t.type:""}`,d());return}o.current?o.current.active():Ae(t),console.debug(`Page active - ${t?t.type:""}`,d())},Oe=t=>{if(!J&&!w.current&&(w.current=d(),G.current(null,L)),U(t,L),E.current)return;if(k(),Be.current.includes(t.type)||t.type==="visibilitychange"&&i&&i.visibilityState==="hidden"){m.current||oe(t);return}if(!m.current&&Ve.current.includes(t.type)){oe(t);return}let I=d()-w.current;if(m.current&&!le||!m.current&&I>=C.current){oe(t);return}H.current=!1,R.current=0,D.current=0,q(),j&&te&&Ge.current()},ae=n.default.useRef(Oe);n.default.useEffect(()=>{let t=ne.current;t&&$(),Se>0?ae.current=ke(Oe,Se):ae.current=Oe,t&&F()},[Se,g,O,he,j,te]);let yt=t=>{if(!J&&!w.current&&(w.current=d(),G.current()),U(t),!E.current&&(k(),!m.current&&t.type==="blur")){oe(t);return}},wt=n.default.useRef(yt),F=()=>{P&&se.current&&(ne.current||(Me.current.forEach(t=>{se.current.addEventListener(t,ae.current,{capture:!0,passive:!0})}),window.addEventListener("blur",wt.current,{capture:!0,passive:!0}),ne.current=!0))},$=(t=!1)=>{P&&se.current&&(ne.current||t)&&(Me.current.forEach(I=>{se.current.removeEventListener(I,ae.current,{capture:!0})}),window.removeEventListener("blur",ae.current,{capture:!0}),ne.current=!1)},Y=n.default.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]),xe=n.default.useCallback(t=>S.current?!1:(k(),F(),fe.current=d(),re.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]),Ye=n.default.useCallback(t=>S.current?!1:(k(),F(),(m.current||E.current)&&Ae(),m.current=!1,E.current=!1,H.current=!1,R.current=0,D.current=0,fe.current=d(),o.current&&!t&&o.current.activate(),q(),!0),[_,m,E,S,C,o]),ve=n.default.useCallback((t=!1)=>S.current||H.current?!1:(R.current=Je(),H.current=!0,$(),k(),o.current&&!t&&o.current.pause(),!0),[_,S,o]),De=n.default.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]),Lt=n.default.useCallback((t,I)=>(o.current?(I&&X.current(t,L),o.current.message(t)):I&&X.current(t,L),!0),[p]),Rt=n.default.useCallback(()=>m.current,[m]),kt=n.default.useCallback(()=>E.current,[E]),Pt=n.default.useCallback(()=>o.current?o.current.isLeader:null,[o]),Ct=n.default.useCallback(()=>o.current?o.current.isLastActive:null,[o]),St=n.default.useCallback(()=>o.current?o.current.token:null,[o]),Je=n.default.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]),je=n.default.useCallback(()=>Math.round(d()-fe.current),[fe]),Xe=n.default.useCallback(()=>Math.round(d()-Fe.current),[Fe]),Mt=n.default.useCallback(()=>y.current?new Date(y.current):null,[y]),At=n.default.useCallback(()=>me.current?me.current:null,[me]),Ot=n.default.useCallback(()=>w.current?new Date(w.current):null,[w]),ze=n.default.useCallback(()=>m.current?Math.round(d()-y.current+V.current):Math.round(V.current),[y,V]),Ke=n.default.useCallback(()=>m.current?Math.round(d()-y.current+re.current):Math.round(re.current),[y,re]),xt=n.default.useCallback(()=>{let t=Math.round(je()-ze());return t>=0?t:0},[y,V]),Dt=n.default.useCallback(()=>{let t=Math.round(Xe()-Ke());return t>=0?t:0},[y,V]);n.default.useEffect(()=>{if(O>0&&g>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");l&&vt(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.default.useEffect(()=>{o.current&&o.current.close(),j?o.current=new Le({channelName:_e,leaderElection:Ue,onPrompt:()=>{qe()},onIdle:(t,I)=>{$e(`cross-tab-${t?t.type:""}`,I)},onActive:()=>{Ae()},onMessage:t=>{X.current(t,L)},onLeader:T,start:Y,reset:xe,activate:Ye,pause:ve,resume:De}):o.current=null},[j,_e,Ue,de,pe,G,X,Y,xe,ve,De]),n.default.useEffect(()=>{N.current||(k(),$(!0)),!(x||A)&&(J?Y():F())},[x,J,A,N]),n.default.useEffect(()=>{if(!N.current){let t=[...new Set([...a,...b,...M]).values()];if($(),Me.current=t,se.current=i,Ve.current=b,Be.current=M,x||A)return;J?Y():F()}},[i,JSON.stringify(a),JSON.stringify(b),N,x,J,A]),n.default.useEffect(()=>{N.current&&(N.current=!1)},[N]);let L={message:Lt,start:Y,reset:xe,activate:Ye,pause:ve,resume:De,isIdle:Rt,isPrompted:kt,isLeader:Pt,isLastActiveTab:Ct,getTabId:St,getRemainingTime:Je,getElapsedTime:je,getTotalElapsedTime:Xe,getLastIdleTime:Mt,getLastIdleEvent:At,getLastActiveTime:Ot,getIdleTime:ze,getTotalIdleTime:Ke,getActiveTime:xt,getTotalActiveTime:Dt,setOnPresenceChange:t=>{W=t,ie.current=t},setOnPrompt:t=>{f=t,de.current=t},setOnIdle:t=>{c=t,pe.current=t},setOnActive:t=>{h=t,G.current=t},setOnAction:t=>{u=t,he.current=t},setOnMessage:t=>{p=t,X.current=t}};return L}var Ce=require("react");var gt=require("react/jsx-runtime");function bt(s){return(0,Ce.forwardRef)(function(r,i){let a={...r},l=K(a);return typeof i=="function"?i(l):i&&(i.current=l),(0,gt.jsx)(s,{...r,...l})})}var Ne=class extends Ce.Component{},Pe=class extends Ne{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))}};