inactivity-countdown-timer
Version:
A plain JS (Typescript) module that will countdown and timeout when users are inactive/idle.
2 lines • 6.65 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.InactivityLogout=e():t.InactivityLogout=e()}(window,(function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,i),o.l=!0,o.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 o in t)i.d(n,o,function(e){return t[e]}.bind(null,o));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=1)}([function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n,o={idleTimeoutTime:1e4,localStorageKey:"inactivity_logout_local_storage",resetEvents:["click","mousemove","keypress"],windowResetEvents:["load"],throttleDuration:0};!function(t){t.started="started",t.stopped="stopped"}(n=e.InactivityCountdownTimerStatus||(e.InactivityCountdownTimerStatus={}));var s=function(){function t(t,e){this.params=t,this.deps=e,this.resetEvents=[],this.windowResetEvents=[],this.countingDown=!1,this.status=n.stopped,this.logger=e&&e.logger||console,this.window=e&&e.window||window,this.document=e&&e.document||document,this.localStorage=this.detectAndAssignLocalStorage(e&&e.localStorage),t&&this.setup(t)}return Object.defineProperty(t.prototype,"started",{get:function(){return this.status===n.started},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"stopped",{get:function(){return this.status===n.stopped},enumerable:!0,configurable:!0}),t.prototype.setup=function(t){var e=this;this.cleanup(),Object.assign(this,o,t),this.ensureReasonableTimings(t),this.attacheEventListeners();return{start:function(){return e.start()}}},t.prototype.handleEvent=function(t){var e=(new Date).getTime();this.setLastResetTimeStamp(e),this.throttle()},t.prototype.start=function(){this.setLastResetTimeStamp((new Date).getTime()),this.startPrivate(this.internalTimeoutTime),this.status=n.started},t.prototype.stop=function(){this.window.clearInterval(this.idleIntervalId),this.status=n.stopped},t.prototype.cleanup=function(){this.detachEventListeners(),this.window.removeEventListener("storage",(function(){})),this.window.clearTimeout(this.throttleTimeoutId),this.stop()},t.prototype.ensureReasonableTimings=function(t){if(t&&void 0!==t.idleTimeoutTime&&!r(t.idleTimeoutTime)&&(this.logger.log("idleTimeoutTime passed was not a number, setting to 30 minutes"),this.idleTimeoutTime=18e5),t&&r(t.startCountDownTimerAt)?t.startCountDownTimerAt<this.idleTimeoutTime?(this.startCountDownTimerAt=t.startCountDownTimerAt,this.internalTimeoutTime=this.idleTimeoutTime-this.startCountDownTimerAt):(this.logger.log("startCountdown time must be smaller than idleTimeoutTime, setting to idleTimeoutTime"),this.startCountDownTimerAt=this.idleTimeoutTime,this.internalTimeoutTime=1e3):(this.startCountDownTimerAt=0,this.internalTimeoutTime=this.idleTimeoutTime),t&&r(t.throttleDuration)){var e=Math.floor(this.internalTimeoutTime/5);t.throttleDuration,t.throttleDuration>e&&(this.logger.log("throttle time must be smaller than 1/5th timeout time: "+this.internalTimeoutTime+" setting to "+e+"ms"),this.throttleDuration=e)}},t.prototype.throttle=function(){var t=this;this.throttleDuration>0&&(this.detachEventListeners(),this.throttleTimeoutId=this.window.setTimeout((function(){t.attacheEventListeners()}),this.throttleDuration))},t.prototype.attacheEventListeners=function(){for(var t=0;t<this.resetEvents.length;t++)this.document.addEventListener(this.resetEvents[t],this,!1);for(t=0;t<this.windowResetEvents.length;t++)this.window.addEventListener(this.windowResetEvents[t],this,!1)},t.prototype.detachEventListeners=function(){for(var t=0;t<this.resetEvents.length;t++)this.document.removeEventListener(this.resetEvents[t],this,!1);for(t=0;t<this.windowResetEvents.length;t++)this.window.removeEventListener(this.windowResetEvents[t],this,!1)},t.prototype.startPrivate=function(t){var e=this;this.currentTimerPrecision=t,this.idleIntervalId=this.window.setInterval((function(){e.checkIdleTime()}),t)},t.prototype.resetTimer=function(t){this.stop(),this.startPrivate(t)},t.prototype.timeout=function(){this.cleanup(),this.timeoutCallback&&this.timeoutCallback()},t.prototype.checkIdleTime=function(){var t=(new Date).getTime()-this.getLastResetTimeStamp(),e=this.idleTimeoutTime-t;this.checkTimerPrecision(e),this.handleCountDown(e),t>=this.idleTimeoutTime&&this.timeout()},t.prototype.handleCountDown=function(t){var e=t<=this.startCountDownTimerAt;e&&this.countDownCallback?(this.countingDown=!0,this.countDownCallback(Math.abs(Math.ceil(t/1e3)))):!e&&this.countingDown&&(this.countDownCancelledCallback&&this.countDownCancelledCallback(),this.countingDown=!1)},t.prototype.checkTimerPrecision=function(t){if(t<=this.startCountDownTimerAt)1e3!==this.currentTimerPrecision&&this.resetTimer(1e3);else{var e=t-this.startCountDownTimerAt;this.resetTimer(e)}},t.prototype.getLastResetTimeStamp=function(){if(this.localStorage){var t=this.localStorage.getItem(this.localStorageKey),e=parseInt(t,10);if(e)return e}return this.lastResetTimeStamp},t.prototype.setLastResetTimeStamp=function(t){this.localStorage&&this.localStorage.setItem(this.localStorageKey,t.toString()),this.lastResetTimeStamp=t},t.prototype.detectAndAssignLocalStorage=function(t){if(function(t){if(null===t)return!0;return Boolean(t)}(t))return t;this.window.addEventListener("storage",(function(){}));var e,i=(new Date).getTime().toString()+"detectAndAssignLocalStorage",n=localStorage;try{return n.setItem(i,i),e=n.getItem(i)===i,n.removeItem(i),e&&n}catch(t){return this.logger.log("LOCAL STORAGE IS NOT AVAILABLE FOR SYNCING TIMEOUT ACROSS TABS",t),null}},t}();function r(t){return"number"==typeof t&&!isNaN(t)}e.InactivityCountdownTimer=s,e.isNumberNotNan=r},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(0);e.InactivityCountdownTimer=n.InactivityCountdownTimer;var o=i(0);e.InactivityCountdownTimerStatus=o.InactivityCountdownTimerStatus}])}));
//# sourceMappingURL=main.js.map