@capsitech/idle-timer
Version:
Activity detection for React.js
2 lines (1 loc) • 31.2 kB
JavaScript
var Er=Object.create;var At=Object.defineProperty;var Tr=Object.getOwnPropertyDescriptor;var Ir=Object.getOwnPropertyNames;var Or=Object.getPrototypeOf,Sr=Object.prototype.hasOwnProperty;var xr=function(t,r){return function(){return t&&(r=t(t=0)),r}};var Rr=function(t,r){return function(){return r||t((r={exports:{}}).exports,r),r.exports}};var Lr=function(t,r,e,n){if(r&&typeof r=="object"||typeof r=="function")for(var i=Ir(r),s=0,c=i.length,v;s<c;s++)v=i[s],!Sr.call(t,v)&&v!==e&&At(t,v,{get:function(l){return r[l]}.bind(null,v),enumerable:!(n=Tr(r,v))||n.enumerable});return t};var kr=function(t,r,e){return e=t!=null?Er(Or(t)):{},Lr(r||!t||!t.__esModule?At(e,"default",{value:t,enumerable:!0}):e,t)};function W(t){"@swc/helpers - typeof";return t&&typeof Symbol!="undefined"&&t.constructor===Symbol?"symbol":typeof t}var Ee=xr(function(){});var Ut=Rr(function(Te,Bt){Ee();(function(t,r){(typeof Te=="undefined"?"undefined":W(Te))==="object"&&typeof Bt!="undefined"?r(Te):typeof define=="function"&&define.amd?define(["exports"],r):(t=typeof globalThis!="undefined"?globalThis:t||self,r(t.fastUniqueNumbers={}))})(Te,function(t){"use strict";var r=function(w){return function(_){var m=w(_);return _.add(m),m}},e=function(w){return function(_,m){return w.set(_,m),m}},n=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,i=536870912,s=i*2,c=function(w,_){return function(m){var j=_.get(m),E=j===void 0?m.size:j<s?j+1:0;if(!m.has(E))return w(m,E);if(m.size<i){for(;m.has(E);)E=Math.floor(Math.random()*s);return w(m,E)}if(m.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;m.has(E);)E=Math.floor(Math.random()*n);return w(m,E)}},v=new WeakMap,l=e(v),u=c(l,v),d=r(u);t.addUniqueNumber=d,t.generateUniqueNumber=u})});import{createContext as Pr,useContext as Ar}from"react";var Ne=Pr(null);function Cr(t){var r=be(t);return React.createElement(Ne.Provider,{value:r},t.children)}var Mr=Ne.Consumer;function jr(){return Ar(Ne)}function Be(t,r){return r!=null&&typeof Symbol!="undefined"&&r[Symbol.hasInstance]?!!r[Symbol.hasInstance](t):Be(t,r)}function V(t,r){if(!Be(t,r))throw new TypeError("Cannot call a class as a function")}function Ct(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function se(t,r,e){return r&&Ct(t.prototype,r),e&&Ct(t,e),t}function h(t,r,e){return r in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function we(t,r){(r==null||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function Mt(t){if(Array.isArray(t))return we(t)}function jt(t){if(typeof Symbol!="undefined"&&t[Symbol.iterator]!=null||t["@@iterator"]!=null)return Array.from(t)}function Dt(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function Nt(t,r){if(t){if(typeof t=="string")return we(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);if(e==="Object"&&t.constructor&&(e=t.constructor.name),e==="Map"||e==="Set")return Array.from(e);if(e==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return we(t,r)}}function g(t){return Mt(t)||jt(t)||Nt(t)||Dt()}var p=function(t){return t[t.APPLY=0]="APPLY",t[t.TELL=1]="TELL",t[t.CLOSE=2]="CLOSE",t[t.REGISTER=3]="REGISTER",t[t.DEREGISTER=4]="DEREGISTER",t[t.IDLE=5]="IDLE",t[t.ACTIVE=6]="ACTIVE",t[t.PROMPT=7]="PROMPT",t[t.START=8]="START",t[t.RESET=9]="RESET",t[t.ACTIVATE=10]="ACTIVATE",t[t.PAUSE=11]="PAUSE",t[t.RESUME=12]="RESUME",t[t.MESSAGE=13]="MESSAGE",t}({});function Ue(){return Math.random().toString(36).substring(2)}Ee();var Ie=kr(Ut());var Gt=function(t){return t.method!==void 0&&t.method==="call"};var Ft=function(t){return typeof t.id=="number"&&typeof t.result=="boolean"};var Vt=function(t){var r=new Map([[0,function(){}]]),e=new Map([[0,function(){}]]),n=new Map,i=new Worker(t);i.addEventListener("message",function(u){var d=u.data;if(Gt(d)){var S=d.params,w=S.timerId,_=S.timerType;if(_==="interval"){var m=r.get(w);if((typeof m=="undefined"?"undefined":W(m))===void 0)throw new Error("The timer is in an undefined state.");if(typeof m=="number"){var j=n.get(m);if(j===void 0||j.timerId!==w||j.timerType!==_)throw new Error("The timer is in an undefined state.")}else typeof m=="function"&&m()}else if(_==="timeout"){var E=e.get(w);if((typeof E=="undefined"?"undefined":W(E))===void 0)throw new Error("The timer is in an undefined state.");if(typeof E=="number"){var ee=n.get(E);if(ee===void 0||ee.timerId!==w||ee.timerType!==_)throw new Error("The timer is in an undefined state.")}else typeof E=="function"&&(E(),e.delete(w))}}else if(Ft(d)){var z=d.id,te=n.get(z);if(te===void 0)throw new Error("The timer is in an undefined state.");var H=te.timerId,xe=te.timerType;n.delete(z),xe==="interval"?r.delete(H):e.delete(H)}else{var re=d.error.message;throw new Error(re)}});var s=function(u){if(typeof r.get(u)=="function"){var d=(0,Ie.generateUniqueNumber)(n);n.set(d,{timerId:u,timerType:"interval"}),r.set(u,d),i.postMessage({id:d,method:"clear",params:{timerId:u,timerType:"interval"}})}},c=function(u){if(typeof e.get(u)=="function"){var d=(0,Ie.generateUniqueNumber)(n);n.set(d,{timerId:u,timerType:"timeout"}),e.set(u,d),i.postMessage({id:d,method:"clear",params:{timerId:u,timerType:"timeout"}})}},v=function(u){for(var d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0,S=arguments.length,w=new Array(S>2?S-2:0),_=2;_<S;_++)w[_-2]=arguments[_];var m=(0,Ie.generateUniqueNumber)(r);return r.set(m,function(){u.apply(void 0,g(w)),typeof r.get(m)=="function"&&i.postMessage({id:null,method:"set",params:{delay:d,now:performance.timeOrigin+performance.now(),timerId:m,timerType:"interval"}})}),i.postMessage({id:null,method:"set",params:{delay:d,now:performance.timeOrigin+performance.now(),timerId:m,timerType:"interval"}}),m},l=function(u){for(var d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0,S=arguments.length,w=new Array(S>2?S-2:0),_=2;_<S;_++)w[_-2]=arguments[_];var m=(0,Ie.generateUniqueNumber)(e);return e.set(m,function(){return u.apply(void 0,g(w))}),i.postMessage({id:null,method:"set",params:{delay:d,now:performance.timeOrigin+performance.now(),timerId:m,timerType:"timeout"}}),m};return{clearInterval:s,clearTimeout:c,setInterval:v,setTimeout:l}};var Wt=function(t,r){var e=null;return function(){if(e!==null)return e;var n=new Blob([r],{type:"application/javascript; charset=utf-8"}),i=URL.createObjectURL(n);return e=t(i),setTimeout(function(){return URL.revokeObjectURL(i)}),e}};var qt='(()=>{"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 Yt,zt,Ge=Wt(Vt,qt),Ht=function(t){return Ge().clearInterval(t)},Jt=function(t){return Ge().clearTimeout(t)},Xt=function(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];return(Yt=Ge()).setInterval.apply(Yt,g(r))},Kt=function(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];return(zt=Ge()).setTimeout.apply(zt,g(r))};Ee();var A=(typeof window=="undefined"||typeof window=="undefined"?"undefined":W(window))==="object";var T={setTimeout:A?setTimeout.bind(window):setTimeout,clearTimeout:A?clearTimeout.bind(window):clearTimeout,setInterval:A?setInterval.bind(window):setInterval,clearInterval:A?clearInterval.bind(window):clearInterval},Oe={setTimeout:Kt,clearTimeout:Jt,setInterval:Xt,clearInterval:Ht};function Nr(){T.setTimeout=setTimeout,T.clearTimeout=clearTimeout,T.setInterval=setInterval,T.clearInterval=clearInterval,Oe.setTimeout=setTimeout,Oe.clearTimeout=clearTimeout,Oe.setInterval=setInterval,Oe.clearInterval=clearInterval}function $t(t){T.setTimeout=t.setTimeout,T.clearTimeout=t.clearTimeout,T.setInterval=t.setInterval,T.clearInterval=t.clearInterval}var ue={},Br=function(){"use strict";function t(r){V(this,t),h(this,"name",void 0),h(this,"closed",!1),h(this,"mc",new MessageChannel),this.name=r,ue[r]=ue[r]||[],ue[r].push(this),this.mc.port1.start(),this.mc.port2.start(),this.onStorage=this.onStorage.bind(this),window.addEventListener("storage",this.onStorage)}return se(t,[{key:"onStorage",value:function(e){if(e.storageArea===window.localStorage&&e.key.substring(0,this.name.length)===this.name&&e.newValue!==null){var n=JSON.parse(e.newValue);this.mc.port2.postMessage(n)}}},{key:"postMessage",value:function(e){var n=this;if(this.closed)throw new Error("InvalidStateError");var i=JSON.stringify(e),s="".concat(this.name,":").concat(String(Date.now())).concat(String(Math.random()));window.localStorage.setItem(s,i),T.setTimeout(function(){window.localStorage.removeItem(s)},500),ue[this.name].forEach(function(c){c!==n&&c.mc.port2.postMessage(JSON.parse(i))})}},{key:"close",value:function(){if(!this.closed){this.closed=!0,this.mc.port1.close(),this.mc.port2.close(),window.removeEventListener("storage",this.onStorage);var e=ue[this.name].indexOf(this);ue[this.name].splice(e,1)}}},{key:"onmessage",get:function(){return this.mc.port1.onmessage},set:function(e){this.mc.port1.onmessage=e}},{key:"onmessageerror",get:function(){return this.mc.port1.onmessageerror},set:function(e){this.mc.port1.onmessageerror=e}},{key:"addEventListener",value:function(e,n){return this.mc.port1.addEventListener(e,n)}},{key:"removeEventListener",value:function(e,n){return this.mc.port1.removeEventListener(e,n)}},{key:"dispatchEvent",value:function(e){return this.mc.port1.dispatchEvent(e)}}]),t}(),Xe=typeof window=="undefined"?void 0:typeof window.BroadcastChannel=="function"?window.BroadcastChannel:Br;function Qt(t,r,e,n,i,s,c){try{var v=t[s](c),l=v.value}catch(u){e(u);return}v.done?r(l):Promise.resolve(l).then(n,i)}function Zt(t){return function(){var r=this,e=arguments;return new Promise(function(n,i){var s=t.apply(r,e);function c(l){Qt(s,n,i,c,v,"next",l)}function v(l){Qt(s,n,i,c,v,"throw",l)}c(void 0)})}}function Ke(t,r){var e={label:0,sent:function(){if(s[0]&1)throw s[1];return s[1]},trys:[],ops:[]},n,i,s,c=Object.create((typeof Iterator=="function"?Iterator:Object).prototype);return c.next=v(0),c.throw=v(1),c.return=v(2),typeof Symbol=="function"&&(c[Symbol.iterator]=function(){return this}),c;function v(u){return function(d){return l([u,d])}}function l(u){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(e=0)),e;)try{if(n=1,i&&(s=u[0]&2?i.return:u[0]?i.throw||((s=i.return)&&s.call(i),0):i.next)&&!(s=s.call(i,u[1])).done)return s;switch(i=0,s&&(u=[u[0]&2,s.value]),u[0]){case 0:case 1:s=u;break;case 4:return e.label++,{value:u[1],done:!1};case 5:e.label++,i=u[1],u=[0];continue;case 7:u=e.ops.pop(),e.trys.pop();continue;default:if(s=e.trys,!(s=s.length>0&&s[s.length-1])&&(u[0]===6||u[0]===2)){e=0;continue}if(u[0]===3&&(!s||u[1]>s[0]&&u[1]<s[3])){e.label=u[1];break}if(u[0]===6&&e.label<s[1]){e.label=s[1],s=u;break}if(s&&e.label<s[2]){e.label=s[2],e.ops.push(u);break}s[2]&&e.ops.pop(),e.trys.pop();continue}u=r.call(t,e)}catch(d){u=[6,d],i=0}finally{n=s=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}}function er(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0;return new Promise(function(r){return T.setTimeout(r,t)})}var tr=function(){"use strict";function t(r,e){V(this,t),h(this,"options",void 0),h(this,"channel",void 0),h(this,"token",Ue()),h(this,"isLeader",!1),h(this,"isDead",!1),h(this,"isApplying",!1),h(this,"reApply",!1),h(this,"intervals",[]),h(this,"listeners",[]),h(this,"deferred",void 0),this.channel=r,this.options=e,this.apply=this.apply.bind(this),this.awaitLeadership=this.awaitLeadership.bind(this),this.sendAction=this.sendAction.bind(this)}return se(t,[{key:"apply",value:function(){var e=this;return Zt(function(){var n,i,s;return Ke(this,function(c){switch(c.label){case 0:if(e.isLeader)return[2,!1];if(e.isDead)return[2,!1];if(e.isApplying)return e.reApply=!0,[2,!1];e.isApplying=!0,n=!1,i=function(v){var l=v.data,u=l.token,d=l.action;u!==e.token&&(d===p.APPLY&&u>e.token&&(n=!0),d===p.TELL&&(n=!0))},e.channel.addEventListener("message",i),c.label=1;case 1:return c.trys.push([1,3,,4]),e.sendAction(p.APPLY),[4,er(e.options.responseTime)];case 2:return c.sent(),e.channel.removeEventListener("message",i),e.isApplying=!1,n?e.reApply?[2,e.apply()]:[2,!1]:(e.assumeLead(),[2,!0]);case 3:return s=c.sent(),[2,!1];case 4:return[2]}})})()}},{key:"awaitLeadership",value:function(){var e=this;if(this.isLeader)return Promise.resolve();var n=!1,i=null;return new Promise(function(s){var c=function(){if(!n){n=!0;try{T.clearInterval(i)}catch(u){}var l=e.intervals.indexOf(i);l>=0&&e.intervals.splice(l,1),e.channel.removeEventListener("message",v),s()}};i=T.setInterval(function(){e.apply().then(function(){e.isLeader&&c()})},e.options.fallbackInterval),e.intervals.push(i);var v=function(l){var u=l.data.action;u===p.CLOSE&&e.apply().then(function(){e.isLeader&&c()})};e.channel.addEventListener("message",v)})}},{key:"sendAction",value:function(e){this.channel.postMessage({action:e,token:this.token})}},{key:"assumeLead",value:function(){var e=this;this.isLeader=!0,this.options.onLeader();var n=function(i){var s=i.data.action;s===p.APPLY&&e.sendAction(p.TELL)};return this.channel.addEventListener("message",n),this.listeners.push(n),this.sendAction(p.TELL)}},{key:"waitForLeadership",value:function(){return this.deferred?this.deferred:(this.deferred=this.awaitLeadership(),this.deferred)}},{key:"close",value:function(){var e=this;if(!this.isDead){this.isDead=!0,this.isLeader=!1,this.sendAction(p.CLOSE);try{this.listeners.forEach(function(n){return e.channel.removeEventListener("message",n)}),this.intervals.forEach(function(n){return T.clearInterval(n)})}catch(n){}}}}]),t}();var rr=function(){"use strict";function t(r){var e=this;V(this,t),h(this,"channel",void 0),h(this,"options",void 0),h(this,"elector",void 0),h(this,"token",Ue()),h(this,"registry",new Map),h(this,"allIdle",!1),h(this,"isLastActive",!1);var n=r.channelName;if(this.options=r,this.channel=new Xe(n),this.registry.set(this.token,1),r.leaderElection){var i={fallbackInterval:2e3,responseTime:100,onLeader:this.options.onLeader};this.elector=new tr(this.channel,i),this.elector.waitForLeadership()}this.channel.addEventListener("message",function(s){var c=s.data,v=c.action,l=c.token,u=c.data,d=u===void 0?{}:u;switch(v){case p.REGISTER:e.registry.set(l,2);break;case p.DEREGISTER:e.registry.delete(l);break;case p.IDLE:e.idle(l,d.eventType);break;case p.ACTIVE:e.active(l);break;case p.PROMPT:e.prompt(l);break;case p.START:e.start(l);break;case p.RESET:e.reset(l);break;case p.ACTIVATE:e.activate(l);break;case p.PAUSE:e.pause(l);break;case p.RESUME:e.resume(l);break;case p.MESSAGE:e.options.onMessage(d);break}}),this.send(p.REGISTER)}return se(t,[{key:"isLeader",get:function(){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}},{key:"prompt",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;this.registry.set(e,0);var n=g(this.registry.values()).every(function(i){return i===0});e===this.token&&this.send(p.PROMPT),n&&this.options.onPrompt()}},{key:"idle",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token,n=arguments.length>1?arguments[1]:void 0;this.registry.set(e,2);var i=g(this.registry.values()).every(function(s){return s===2});e===this.token&&this.send(p.IDLE,n),!this.allIdle&&i&&(this.allIdle=!0,this.options.onIdle({type:n},e))}},{key:"active",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;this.allIdle=!1,this.registry.set(e,1);var n=g(this.registry.values()).some(function(i){return i===1});e===this.token&&this.send(p.ACTIVE),n&&this.options.onActive(),this.isLastActive=e===this.token}},{key:"start",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(p.START):this.options.start(!0),this.isLastActive=e===this.token}},{key:"reset",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(p.RESET):this.options.reset(!0),this.isLastActive=e===this.token}},{key:"activate",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;this.allIdle=!1,this.registry.set(e,1),e===this.token?this.send(p.ACTIVATE):this.options.activate(!0),this.isLastActive=e===this.token}},{key:"pause",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;e===this.token?this.send(p.PAUSE):this.options.pause(!0)}},{key:"resume",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.token;e===this.token?this.send(p.RESUME):this.options.resume(!0)}},{key:"message",value:function(e){try{this.channel.postMessage({action:p.MESSAGE,token:this.token,data:e})}catch(n){}}},{key:"send",value:function(e,n){try{this.channel.postMessage({action:e,token:this.token,data:{eventType:n}})}catch(i){}}},{key:"close",value:function(){this.options.leaderElection&&this.elector.close(),this.send(p.DEREGISTER),this.channel.close()}}]),t}();import a from"react";function nr(t,r){var e;function n(){for(var i=arguments.length,s=new Array(i),c=0;c<i;c++)s[c]=arguments[c];e&&clearTimeout(e),e=setTimeout(function(){t.apply(void 0,g(s)),e=null},r)}return n.cancel=function(){clearTimeout(e)},n}var or=A?document:null,$e=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange","focus"];var b=function(){return Date.now()};function Fe(t,r){var e=0;return function(){for(var n=arguments.length,i=new Array(n),s=0;s<n;s++)i[s]=arguments[s];var c=new Date().getTime();if(!(c-e<r))return e=c,t.apply(void 0,g(i))}}var ce=2147483647;function be(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{},r=t.timeout,e=r===void 0?1e3*60*20:r,n=t.promptTimeout,i=n===void 0?0:n,s=t.promptBeforeIdle,c=s===void 0?0:s,v=t.element,l=v===void 0?or:v,u=t.events,d=u===void 0?$e:u,S=t.timers,w=S===void 0?void 0:S,_=t.immediateEvents,m=_===void 0?[]:_,j=t.immediateIdleEvents,E=j===void 0?[]:j,ee=t.onPresenceChange,z=ee===void 0?function(){}:ee,te=t.onPrompt,H=te===void 0?function(){}:te,xe=t.onIdle,re=xe===void 0?function(){}:xe,rt=t.onActive,Re=rt===void 0?function(){}:rt,nt=t.onAction,Le=nt===void 0?function(){}:nt,ot=t.onMessage,fe=ot===void 0?function(){}:ot,it=t.onLeader,sr=it===void 0?function(){}:it,at=t.debounce,le=at===void 0?0:at,st=t.throttle,me=st===void 0?0:st,ut=t.eventsThrottle,We=ut===void 0?200:ut,ct=t.startOnMount,ne=ct===void 0?!0:ct,ft=t.startManually,N=ft===void 0?!1:ft,lt=t.stopOnIdle,ke=lt===void 0?!1:lt,mt=t.crossTab,oe=mt===void 0?!1:mt,dt=t.name,pt=dt===void 0?"idle-timer":dt,vt=t.syncTimers,de=vt===void 0?0:vt,ht=t.leaderElection,_t=ht===void 0?!1:ht,yt=t.disabled,D=yt===void 0?!1:yt,gt=a.useRef(b()),Pe=a.useRef(b()),x=a.useRef(null),Ae=a.useRef(null),R=a.useRef(null),J=a.useRef(0),pe=a.useRef(0),B=a.useRef(0),k=a.useRef(0),y=a.useRef(!1),I=a.useRef(!1),U=a.useRef(!1),G=a.useRef(!0),ve=a.useRef(!1),F=a.useRef(null),f=a.useRef(null),C=a.useRef(e),X=a.useRef(0);a.useEffect(function(){if(i&&console.warn("\u26A0\uFE0F IdleTimer -- The `promptTimeout` property has been deprecated in favor of `promptBeforeIdle`. It will be removed in the next major release."),c&&i)throw new Error("\u274C Both promptTimeout and promptBeforeIdle can not be set. The promptTimeout property will be deprecated in a future version.");if(e>=ce)throw new Error("\u274C The value for the timeout property must fit in a 32 bit signed integer, ".concat(ce,"."));if(i>=ce)throw new Error("\u274C The value for the promptTimeout property must fit in a 32 bit signed integer, ".concat(ce,"."));if(c>=ce)throw new Error("\u274C The value for the promptBeforeIdle property must fit in a 32 bit signed integer, ".concat(ce,"."));if(c>=e)throw new Error("\u274C The value for the promptBeforeIdle property must be less than the timeout property, ".concat(e,"."));if(c?(C.current=e-c,X.current=c):(C.current=e,X.current=i),!G.current){if(N||D)return;y.current&&(K.current(null,L),f.current&&f.current.active()),Z()}},[e,i,c,N,D]);var bt=a.useRef(ke);a.useEffect(function(){bt.current=ke},[ke]);var wt=a.useRef(m),Et=a.useRef(E),he=a.useRef(l),qe=a.useRef(g(new Set(g(d).concat(g(m),g(E))).values())),M=a.useRef(D);a.useEffect(function(){M.current=D,!G.current&&(D?De():N||Z())},[D]);var _e=a.useRef(z);a.useEffect(function(){_e.current=z},[z]);var Ce=a.useRef(H);a.useEffect(function(){Ce.current=H},[H]);var Me=a.useRef(re);a.useEffect(function(){Me.current=re},[re]);var K=a.useRef(Re);a.useEffect(function(){K.current=Re},[Re]);var je=a.useRef(Le);a.useEffect(function(){je.current=Le},[Le]);var ie=a.useRef(fe);a.useEffect(function(){ie.current=fe},[fe]);var q=a.useMemo(function(){var o=function(O,ae){return je.current(O,ae)};return le>0?nr(o,le):me>0?Fe(o,me):o},[me,le]),Tt=a.useRef();a.useEffect(function(){oe&&de&&(Tt.current=Fe(function(){f.current.active()},de))},[oe,de]);var P=function(){F.current!==null&&(T.clearTimeout(F.current),F.current=null)},$=function(o){var O=arguments.length>1&&arguments[1]!==void 0?arguments[1]:!0;P(),F.current=T.setTimeout(ye,o||C.current),O&&(R.current=b())},It=function(o){!I.current&&!y.current&&(Ce.current(o,L),_e.current({type:"active",prompted:!0},L)),k.current=0,B.current=b(),I.current=!0,$(X.current,!1)},Ot=function(o,O){P(),y.current||(Me.current(null,L),_e.current({type:"idle"},L)),y.current=!0,x.current=b(),Ae.current="".concat(o,"::").concat(O),bt.current?Q():I.current&&(B.current=0,I.current=!1)},Ye=function(o){P(),(y.current||I.current)&&(K.current(o,L),_e.current({type:"active",prompted:!1},L)),I.current=!1,B.current=0,y.current=!1,J.current+=b()-x.current,pe.current+=b()-x.current,Y(),$()},ye=function(o){var O=!y.current;if(O){q.cancel&&q.cancel();var ae=b()-R.current,wr=C.current+X.current<ae;if(!wr&&X.current>0&&!I.current){f.current?f.current.prompt():It(o);return}f.current?f.current.idle():Ot(o.type),console.debug("Page idle - ".concat(o?o.type:""),b());return}f.current?f.current.active():Ye(o),console.debug("Page active - ".concat(o?o.type:""),b())},ze=function(o){if(!ne&&!R.current&&(R.current=b(),K.current(null,L)),q(o,L),!I.current){if(P(),Et.current.includes(o.type)||o.type==="visibilitychange"&&l&&l.visibilityState==="hidden"){y.current||ye(o);return}if(!y.current&&wt.current.includes(o.type)){ye(o);return}var O=b()-R.current;if(y.current&&!ke||!y.current&&O>=C.current){ye(o);return}U.current=!1,k.current=0,B.current=0,$(),oe&&de&&Tt.current()}},ge=a.useRef(ze);a.useEffect(function(){var o=ve.current;o&&Q(),We>0?ge.current=Fe(ze,We):ge.current=ze,o&&Y()},[We,me,le,je,oe,de]);var ur=function(o){if(!ne&&!R.current&&(R.current=b(),K.current()),q(o),!I.current&&(P(),!y.current&&o.type==="blur")){ye(o);return}},cr=a.useRef(ur),Y=function(){A&&he.current&&(ve.current||(qe.current.forEach(function(o){he.current.addEventListener(o,ge.current,{capture:!0,passive:!0})}),window.addEventListener("blur",cr.current,{capture:!0,passive:!0}),ve.current=!0))},Q=function(){var o=arguments.length>0&&arguments[0]!==void 0?arguments[0]:!1;A&&he.current&&(ve.current||o)&&(qe.current.forEach(function(O){he.current.removeEventListener(O,ge.current,{capture:!0})}),window.removeEventListener("blur",ge.current,{capture:!0}),ve.current=!1)},Z=a.useCallback(function(o){return M.current?!1:(P(),Y(),y.current=!1,I.current=!1,U.current=!1,k.current=0,B.current=0,f.current&&!o&&f.current.start(),$(),!0)},[F,y,M,C,f]),He=a.useCallback(function(o){return M.current?!1:(P(),Y(),Pe.current=b(),pe.current+=b()-x.current,J.current=0,y.current=!1,I.current=!1,U.current=!1,k.current=0,B.current=0,f.current&&!o&&f.current.reset(),N||$(),!0)},[F,y,C,N,M,f]),St=a.useCallback(function(o){return M.current?!1:(P(),Y(),(y.current||I.current)&&Ye(),y.current=!1,I.current=!1,U.current=!1,k.current=0,B.current=0,Pe.current=b(),f.current&&!o&&f.current.activate(),$(),!0)},[F,y,I,M,C,f]),De=a.useCallback(function(){var o=arguments.length>0&&arguments[0]!==void 0?arguments[0]:!1;return M.current||U.current?!1:(k.current=xt(),U.current=!0,Q(),P(),f.current&&!o&&f.current.pause(),!0)},[F,M,f]),Je=a.useCallback(function(){var o=arguments.length>0&&arguments[0]!==void 0?arguments[0]:!1;return M.current||!U.current?!1:(U.current=!1,I.current||Y(),y.current||$(k.current),B.current&&(B.current=b()),f.current&&!o&&f.current.resume(),!0)},[F,C,M,k,f]),fr=a.useCallback(function(o,O){return f.current?(O&&ie.current(o,L),f.current.message(o)):O&&ie.current(o,L),!0},[fe]),lr=a.useCallback(function(){return y.current},[y]),mr=a.useCallback(function(){return I.current},[I]),dr=a.useCallback(function(){return f.current?f.current.isLeader:null},[f]),pr=a.useCallback(function(){return f.current?f.current.isLastActive:null},[f]),vr=a.useCallback(function(){return f.current?f.current.token:null},[f]),xt=a.useCallback(function(){if(U.current)return k.current;var o=k.current?k.current:X.current+C.current,O=R.current?b()-R.current:0,ae=Math.floor(o-O);return ae<0?0:Math.abs(ae)},[C,X,I,k,R]),Rt=a.useCallback(function(){return Math.round(b()-Pe.current)},[Pe]),Lt=a.useCallback(function(){return Math.round(b()-gt.current)},[gt]),hr=a.useCallback(function(){return x.current?new Date(x.current):null},[x]),_r=a.useCallback(function(){return Ae.current?Ae.current:null},[Ae]),yr=a.useCallback(function(){return R.current?new Date(R.current):null},[R]),kt=a.useCallback(function(){return y.current?Math.round(b()-x.current+J.current):Math.round(J.current)},[x,J]),Pt=a.useCallback(function(){return y.current?Math.round(b()-x.current+pe.current):Math.round(pe.current)},[x,pe]),gr=a.useCallback(function(){var o=Math.round(Rt()-kt());return o>=0?o:0},[x,J]),br=a.useCallback(function(){var o=Math.round(Lt()-Pt());return o>=0?o:0},[x,J]);a.useEffect(function(){if(le>0&&me>0)throw new Error("\u274C onAction can either be throttled or debounced, not both.");w&&$t(w);var o=function(){f.current&&f.current.close(),q.cancel&&q.cancel(),P(),Q(!0)};return A&&window.removeEventListener("beforeunload",o),function(){A&&window.removeEventListener("beforeunload",o),f.current&&f.current.close(),q.cancel&&q.cancel(),P(),Q(!0)}},[]),a.useEffect(function(){f.current&&f.current.close(),oe?f.current=new rr({channelName:pt,leaderElection:_t,onPrompt:function(){It()},onIdle:function(o,O){Ot("cross-tab-".concat(o?o.type:""),O)},onActive:function(){Ye()},onMessage:function(o){ie.current(o,L)},onLeader:sr,start:Z,reset:He,activate:St,pause:De,resume:Je}):f.current=null},[oe,pt,_t,Ce,Me,K,ie,Z,He,De,Je]),a.useEffect(function(){G.current||(P(),Q(!0)),!(N||D)&&(ne?Z():Y())},[N,ne,D,G]),a.useEffect(function(){if(!G.current){var o=g(new Set(g(d).concat(g(m),g(E))).values());if(Q(),qe.current=o,he.current=l,wt.current=m,Et.current=E,N||D)return;ne?Z():Y()}},[l,JSON.stringify(d),JSON.stringify(m),G,N,ne,D]),a.useEffect(function(){G.current&&(G.current=!1)},[G]);var L={message:fr,start:Z,reset:He,activate:St,pause:De,resume:Je,isIdle:lr,isPrompted:mr,isLeader:dr,isLastActiveTab:pr,getTabId:vr,getRemainingTime:xt,getElapsedTime:Rt,getTotalElapsedTime:Lt,getLastIdleTime:hr,getLastIdleEvent:_r,getLastActiveTime:yr,getIdleTime:kt,getTotalIdleTime:Pt,getActiveTime:gr,getTotalActiveTime:br,setOnPresenceChange:function(o){z=o,_e.current=o},setOnPrompt:function(o){H=o,Ce.current=o},setOnIdle:function(o){re=o,Me.current=o},setOnActive:function(o){Re=o,K.current=o},setOnAction:function(o){Le=o,je.current=o},setOnMessage:function(o){fe=o,ie.current=o}};return L}function Se(t){return Se=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},Se(t)}function Qe(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(r){}return(Qe=function(){return!!t})()}function ir(t){if(t===void 0)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}Ee();function ar(t,r){return r&&(W(r)==="object"||typeof r=="function")?r:ir(t)}function Ze(t,r,e){return r=Se(r),ar(t,Qe()?Reflect.construct(r,e||[],Se(t).constructor):r.apply(t,e))}function Ve(t,r){return Ve=Object.setPrototypeOf||function(n,i){return n.__proto__=i,n},Ve(t,r)}function et(t,r){if(typeof r!="function"&&r!==null)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&Ve(t,r)}function tt(t){for(var r=1;r<arguments.length;r++){var e=arguments[r]!=null?arguments[r]:{},n=Object.keys(e);typeof Object.getOwnPropertySymbols=="function"&&(n=n.concat(Object.getOwnPropertySymbols(e).filter(function(i){return Object.getOwnPropertyDescriptor(e,i).enumerable}))),n.forEach(function(i){h(t,i,e[i])})}return t}import{Component as Ur,forwardRef as Gr}from"react";function Fr(t){return Gr(function(e,n){var i=tt({},e),s=be(i);return typeof n=="function"?n(s):n&&(n.current=s),React.createElement(t,tt({},e,s))})}var Vr=function(t){"use strict";et(r,t);function r(){return V(this,r),Ze(this,r,arguments)}return r}(Ur),Wr=function(t){"use strict";et(r,t);function r(e){V(this,r);var n;return n=Ze(this,r,[e]),n.onPresenceChange&&e.setOnPresenceChange(n.onPresenceChange.bind(n)),n.onPrompt&&e.setOnPrompt(n.onPrompt.bind(n)),n.onIdle&&e.setOnIdle(n.onIdle.bind(n)),n.onActive&&e.setOnActive(n.onActive.bind(n)),n.onAction&&e.setOnAction(n.onAction.bind(n)),n.onMessage&&e.setOnMessage(n.onMessage.bind(n)),n}return r}(Vr);export{Xe as BroadcastChannel,$e as DEFAULT_EVENTS,Wr as IdleTimerComponent,Mr as IdleTimerConsumer,Ne as IdleTimerContext,Cr as IdleTimerProvider,Nr as createMocks,be as useIdleTimer,jr as useIdleTimerContext,Fr as withIdleTimer,Oe as workerTimers};