idle-tabs
Version:
A utility module for tracking user activity on different tabs of same website, with keepalive event, can be used for maintaining sessions where there is token based authentication to preserve or notify users.
1 lines • 4.58 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.webpackNumbers=e():t.webpackNumbers=e()}(window,function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var s=e[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(n,s,function(e){return t[e]}.bind(null,s));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";i.r(e);const n={get:()=>window.localStorage};function s(){var t={};this.setItem=function(e,i){t[e]=i},this.getItem=function(e){return void 0!==t[e]?t[e]:null},this.removeItem=function(e){t[e]=void 0}}var o=function(){try{var t=n.get();return t.setItem("idleStorage",""),t.removeItem("idleStorage"),t}catch(t){return new s}}();var r={set:function(t,e){o.setItem("idle."+t,JSON.stringify(e))},get:function(t){return JSON.parse(o.getItem("idle."+t))},remove:function(t){o.removeItem("idle."+t)},_wrapped:function(){return o}};const l=(t,e,i)=>{e.forEach(e=>{t.addEventListener(e,i)})},c=(t,e,i)=>{e.forEach(e=>{t.removeEventListener(e,i)})};var h=class{constructor(t,e,i){this.$document=i,this.$window=e,this.defaults={idleTime:20,events:["mousemove","keydown","mousedown","touchstart"],onIdle:()=>{},onActive:()=>{},onAlive:()=>{},onHide:()=>{},onShow:()=>{},keepTracking:!0,keepAlive:!0,startAtIdle:!1,recurIdleCall:!1,storePrefix:"idleTabs"},this.config={...this.defaults,...t},this.config.idle=60*this.config.idleTime*1e3,this.config.keepAliveTime=60*(this.config.idleTime-1)*1e3,this.idle=this.config.startAtIdle,this.visible=!this.config.startAtIdle,this.visibilityEvents=["visibilitychange","webkitvisibilitychange","mozvisibilitychange","msvisibilitychange"],this.lastId=null,this.aliveId=null,this.store=r,this.storeKey=`${this.config.storePrefix}`}notify(t){this.config.debug&&console.log(this.config.storePrefix,"IdleTabs: ",Date().slice(16,25),this.config.idle/6e4,t)}isExpired(){const t=Date.now();return this.store.get(this.storeKey)+this.config.idle-t<0}getIdleTime(t){const e=Date.now(),i=r.get(this.storeKey)||e;return t-(e-parseInt(i))}resetTimeout(t,e){let i=Date.now();if(this.store.set(this.storeKey,i),this.idle&&(e.onActive.call(),this.notify("active"),this.idle=!1),clearTimeout(t),this.config.keepTracking)return this.timeout(this.config)}check(){this.idle||(this.isExpired()?(this.idle=!0,this.config.onIdle.call(),this.notify("ideal")):(clearTimeout(this.lastId),this.lastId=this.timeout()))}timeout(){var t=this.config.recurIdleCall?setInterval:setTimeout;return t.bind(this),t(()=>{this.check()},this.getIdleTime(this.config.idle))}keepAlive(t){this.config.keepAlive&&(this.idle?clearTimeout(t):this.aliveId=setTimeout(()=>{this.config.onAlive.call(),this.notify("alive"),this.keepAlive(this.aliveId)},this.config.keepAliveTime))}reset(){this.keepAlive(),this.lastId=this.resetTimeout(this.lastId,this.config),this.notify("Reset")}stop(){const t=this.config.recurIdleCall?clearInterval:clearTimeout;c(this.$window,this.config.events,()=>{}),c(this.$document,this.visibilityEvents,()=>{}),t(this.lastId),clearTimeout(this.aliveId),this.notify("Stoped")}start(){this.keepAlive(),this.notify("initialized"),this.lastId=this.timeout(this.config),l(this.$window,this.config.events,()=>{this.lastId=this.resetTimeout(this.lastId,this.config)}),(this.config.onShow||this.config.onHide)&&l(this.$document,this.visibilityEvents,()=>{this.$document.hidden||this.$document.webkitHidden||this.$document.mozHidden||this.$document.msHidden?this.visible&&(this.visible=!1,this.config.onHide.call(),this.notify("hidden")):this.visible||(this.visible=!0,this.config.onShow.call(),this.notify("visible"))})}};let d;i.d(e,"idleTabs",function(){return u}),i.d(e,"default",function(){return u}),i.d(e,"IdleClass",function(){return h});const u=(t,e,i)=>(d instanceof h||(d=new h(t,e,i)),d)}])});