UNPKG

react-idle-timer

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