react-idle-timer
Version:
Activity detection for React.js
3 lines (2 loc) • 10.5 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=require("prop-types"))&&"object"==typeof e&&"default"in e?e.default:e;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e){return(i=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function u(e,t){return(u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function s(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function c(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?s(e):t}function l(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=i(e);if(t){var o=i(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return c(this,n)}}var a="object"===("undefined"==typeof window||"undefined"==typeof window?"undefined":r(window)),f=a?document:{},d=["mousemove","keydown","wheel","DOMMouseScroll","mousewheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];function v(e,t){var n;return function(){for(var r=arguments.length,o=new Array(r),i=0;i<r;i++)o[i]=arguments[i];n&&clearTimeout(n),n=setTimeout((function(){e.apply(void 0,o),n=null}),t)}}function p(e,t){var n=0;return function(){var r=(new Date).getTime();if(!(r-n<t))return n=r,e.apply(void 0,arguments)}}var h=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&u(e,t)}(f,t.Component);var n,r,i,c=l(f);function f(e){var t;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,f),(t=c.call(this,e)).state={idle:!1,oldDate:+new Date,lastActive:+new Date,lastIdle:null,idleTime:0,remaining:null,pageX:null,pageY:null},t.tId=null,t.eventsBound=!1,e.debounce>0&&e.throttle>0)throw new Error("onAction can either be throttled or debounced (not both)");return e.debounce>0?t._onAction=v(e.onAction,e.debounce):e.throttle>0?t._onAction=p(e.onAction,e.throttle):e.onAction?t._onAction=e.onAction:t._onAction=function(){},e.eventsThrottle>0?t._handleEvent=p(t._handleEvent.bind(s(t)),e.eventsThrottle):t._handleEvent=t._handleEvent.bind(s(t)),e.startOnMount||(t.state.idle=!0),t._toggleIdleState=t._toggleIdleState.bind(s(t)),t.reset=t.reset.bind(s(t)),t.pause=t.pause.bind(s(t)),t.resume=t.resume.bind(s(t)),t.getRemainingTime=t.getRemainingTime.bind(s(t)),t.getElapsedTime=t.getElapsedTime.bind(s(t)),t.getLastActiveTime=t.getLastActiveTime.bind(s(t)),t.isIdle=t.isIdle.bind(s(t)),t}return n=f,(r=[{key:"componentDidMount",value:function(){this._bindEvents(),this.props.startOnMount&&this.reset()}},{key:"componentDidUpdate",value:function(e){e.debounce!==this.props.debounce&&(this._onAction=v(this._onAction,this.props.debounce)),e.throttle!==this.props.throttle&&(this._onAction=p(this._onAction,this.props.throttle)),e.eventsThrottle!==this.props.eventsThrottle&&(this._handleEvent=p(this._handleEvent,this.props.eventsThrottle))}},{key:"componentWillUnmount",value:function(){clearTimeout(this.tId),this._unbindEvents(!0)}},{key:"render",value:function(){return this.props.children||null}},{key:"_bindEvents",value:function(){var e=this;if(a){var t=this.props,n=t.element,r=t.events,o=t.passive,i=t.capture;this.eventsBound||(r.forEach((function(t){n.addEventListener(t,e._handleEvent,{capture:i,passive:o})})),this.eventsBound=!0)}}},{key:"_unbindEvents",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(a){var n=this.props,r=n.element,o=n.events,i=n.passive,u=n.capture;(this.eventsBound||t)&&(o.forEach((function(t){r.removeEventListener(t,e._handleEvent,{capture:u,passive:i})})),this.eventsBound=!1)}}},{key:"_toggleIdleState",value:function(e){var t=this;this.setState((function(e){return{idle:!e.idle,lastIdle:e.idle?e.lastIdle:+new Date-t.props.timeout,idleTime:e.idle?e.idleTime+ +new Date-e.lastIdle:e.idleTime}}),(function(){var n=t.props,r=n.onActive,o=n.onIdle,i=n.stopOnIdle;t.state.idle?(i&&(clearTimeout(t.tId),t.tId=null,t._unbindEvents()),o(e)):i||(t._bindEvents(),r(e))}))}},{key:"_handleEvent",value:function(e){var t=this.state,n=t.remaining,r=t.pageX,o=t.pageY,i=t.idle,u=this.props,s=u.timeout,c=u.stopOnIdle;if(this._onAction(e),!n){if("mousemove"===e.type){if(e.pageX===r&&e.pageY===o)return;if(void 0===e.pageX&&void 0===e.pageY)return;if(this.getElapsedTime()<200)return}clearTimeout(this.tId),this.tId=null;var l=+new Date-this.getLastActiveTime();(i&&!c||!i&&l>s)&&this._toggleIdleState(e),this.setState({lastActive:+new Date,pageX:e.pageX,pageY:e.pageY}),i&&c||(this.tId=setTimeout(this._toggleIdleState,s))}}},{key:"reset",value:function(){clearTimeout(this.tId),this.tId=null,this._bindEvents(),this.setState({idle:!1,oldDate:+new Date,lastActive:+new Date,remaining:null});var e=this.props.timeout;this.tId=setTimeout(this._toggleIdleState,e)}},{key:"pause",value:function(){null===this.state.remaining&&(this._unbindEvents(),clearTimeout(this.tId),this.tId=null,this.setState({remaining:this.getRemainingTime()}))}},{key:"resume",value:function(){var e=this.state,t=e.remaining,n=e.idle;null!==t&&(this._bindEvents(),n||(this.tId=setTimeout(this._toggleIdleState,t),this.setState({remaining:null,lastActive:+new Date})))}},{key:"getRemainingTime",value:function(){var e=this.state,t=e.remaining,n=e.lastActive,r=this.props.timeout;if(null!==t)return t<0?0:t;var o=r-(+new Date-n);return o<0?0:o}},{key:"getElapsedTime",value:function(){var e=this.state.oldDate;return+new Date-e}},{key:"getLastIdleTime",value:function(){return this.state.lastIdle}},{key:"getTotalIdleTime",value:function(){var e=this.state,t=e.idle,n=e.lastIdle,r=e.idleTime;return t?+new Date-n+r:r}},{key:"getLastActiveTime",value:function(){return this.state.lastActive}},{key:"getTotalActiveTime",value:function(){return this.getElapsedTime()-this.getTotalIdleTime()}},{key:"isIdle",value:function(){return this.state.idle}}])&&o(n.prototype,r),i&&o(n,i),f}();function m(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.timeout,r=void 0===n?12e5:n,o=e.element,i=void 0===o?f:o,u=e.events,s=void 0===u?d:u,c=e.onIdle,l=void 0===c?function(){}:c,h=e.onActive,m=void 0===h?function(){}:h,g=e.onAction,b=void 0===g?function(){}:g,y=e.debounce,T=void 0===y?0:y,I=e.throttle,w=void 0===I?0:I,_=e.eventsThrottle,E=void 0===_?200:_,A=e.startOnMount,D=void 0===A||A,O=e.stopOnIdle,R=void 0!==O&&O,S=e.capture,k=void 0===S||S,M=e.passive,j=void 0===M||M,L=t.useRef(!1),P=t.useRef(!0),X=t.useRef(+new Date),Y=t.useRef(null),x=t.useRef(null),B=t.useRef(null),q=t.useRef(null),C=t.useRef(null),U=t.useRef(null),W=t.useRef(0),z=t.useRef(l),F=t.useRef(m),G=t.useRef(b),H=function(e){var t=!P.current;P.current=t,t?(R&&(clearTimeout(q.current),q.current=null,N()),U.current=+new Date-r,z.current(e)):R||(W.current+=+new Date-U.current,K(),F.current(e))},J=function(e){if(G.current(e),!Y.current){if("mousemove"===e.type){if(e.pageX===x&&e.pageY===B)return;if(void 0===e.pageX&&void 0===e.pageY)return;if(V()<200)return}clearTimeout(q.current),q.current=null;var t=+new Date-ee();(P.current&&!R||!P.current&&t>r)&&H(e),C.current=+new Date,x.current=e.pageX,B.current=e.pageY,P.current&&R||(q.current=setTimeout(H,r))}},K=function(){a&&(L.current||(s.forEach((function(e){i.addEventListener(e,J,{capture:k,passive:j})})),L.current=!0))},N=function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];a&&(L.current||e)&&(s.forEach((function(e){i.removeEventListener(e,J,{capture:k,passive:j})})),L.current=!1)},Q=function(){if(null!==Y.current)return Y.current<0?0:Y.current;var e=r-(+new Date-C.current);return e<0?0:e},V=function(){return+new Date-X.current},Z=function(){return U.current},$=function(){return P.current?+new Date-U.current+W.current:W.current},ee=function(){return C.current},te=function(){return V()-$()},ne=function(){return P.current},re=function(){clearTimeout(q.current),q.current=null,K(),P.current=!1,X.current=+new Date,C.current=+new Date,Y.current=null,q.current=setTimeout(H,r)},oe=function(){null===Y.current&&(N(),clearTimeout(q.current),q.current=null,Y.current=Q())},ie=function(){null!==Y.current&&(K(),P.current||(q.current=setTimeout(H,Y.current),Y.current=null,C.current=+new Date))};return t.useEffect((function(){if(T>0&&w>0)throw new Error("onAction can either be throttled or debounced (not both)");return E>0&&(J=p(J,E)),K(),D&&re(),function(){clearTimeout(q.current),N(!0)}}),[]),t.useEffect((function(){z.current=l}),[l]),t.useEffect((function(){F.current=m}),[m]),t.useEffect((function(){G.current=T>0?v(b,T):w>0?p(b,w):b}),[b]),{isIdle:ne,pause:oe,reset:re,resume:ie,getLastIdleTime:Z,getTotalIdleTime:$,getLastActiveTime:ee,getTotalActiveTime:te,getElapsedTime:V,getRemainingTime:Q}}h.propTypes={timeout:n.number,events:n.arrayOf(n.string),onIdle:n.func,onActive:n.func,onAction:n.func,debounce:n.number,throttle:n.number,eventsThrottle:n.number,element:n.oneOfType([n.object,n.element]),startOnMount:n.bool,stopOnIdle:n.bool,passive:n.bool,capture:n.bool},h.defaultProps={timeout:12e5,element:f,events:d,onIdle:function(){},onActive:function(){},onAction:function(){},debounce:0,throttle:0,eventsThrottle:200,startOnMount:!0,stopOnIdle:!1,capture:!0,passive:!0},m.propTypes={timeout:n.number,events:n.arrayOf(n.string),onIdle:n.func,onActive:n.func,onAction:n.func,debounce:n.number,throttle:n.number,eventsThrottle:n.number,element:n.oneOfType([n.object,n.element]),startOnMount:n.bool,stopOnIdle:n.bool,passive:n.bool,capture:n.bool},m.defaultProps={timeout:12e5,element:f,events:d,onIdle:function(){},onActive:function(){},onAction:function(){},debounce:0,throttle:0,eventsThrottle:200,startOnMount:!0,stopOnIdle:!1,capture:!0,passive:!0},exports.default=h,exports.useIdleTimer=m;
//# sourceMappingURL=index.min.js.map