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