UNPKG

react-idle-timer

Version:
3 lines (2 loc) • 7.21 kB
"use strict";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;const s="object"==("undefined"==typeof window?"undefined":typeof window),i=s?document:{},r=["mousemove","keydown","wheel","DOMMouseScroll","mouseWheel","mousedown","touchstart","touchmove","MSPointerDown","MSPointerMove","visibilitychange"];function o(e,t){let n;return function(...s){n&&clearTimeout(n),n=setTimeout(()=>{e(...s),n=null},t)}}function u(e,t){let n=0;return function(...s){const i=(new Date).getTime();if(!(i-n<t))return n=i,e(...s)}}class l extends t.Component{constructor(e){if(super(e),this.state={idle:!1,oldDate:+new Date,lastActive:+new Date,remaining:null,pageX:null,pageY:null},this.tId=null,this.eventsBound=!1,e.debounce>0&&e.throttle>0)throw new Error("onAction can either be throttled or debounced (not both)");e.debounce>0?this._onAction=o(e.onAction,e.debounce):e.throttle>0?this._onAction=u(e.onAction,e.throttle):e.onAction?this._onAction=e.onAction:this._onAction=()=>{},e.eventsThrottle>0?this._handleEvent=u(this._handleEvent.bind(this),e.eventsThrottle):this._handleEvent=this._handleEvent.bind(this),e.startOnMount||(this.state.idle=!0),this._toggleIdleState=this._toggleIdleState.bind(this),this.reset=this.reset.bind(this),this.pause=this.pause.bind(this),this.resume=this.resume.bind(this),this.getRemainingTime=this.getRemainingTime.bind(this),this.getElapsedTime=this.getElapsedTime.bind(this),this.getLastActiveTime=this.getLastActiveTime.bind(this),this.isIdle=this.isIdle.bind(this)}componentDidMount(){this._bindEvents();const{startOnMount:e}=this.props;e&&this.reset()}componentDidUpdate(e){e.debounce!==this.props.debounce&&(this._onAction=o(this._onAction,this.props.debounce)),e.throttle!==this.props.throttle&&(this._onAction=u(this._onAction,this.props.throttle)),e.eventsThrottle!==this.props.eventsThrottle&&(this._handleEvent=u(this._handleEvent,this.props.eventsThrottle))}componentWillUnmount(){clearTimeout(this.tId),this._unbindEvents(!0)}render(){const{children:e}=this.props;return e||null}_bindEvents(){if(!s)return;const{element:e,events:t,passive:n,capture:i}=this.props;this.eventsBound||(t.forEach(t=>{e.addEventListener(t,this._handleEvent,{capture:i,passive:n})}),this.eventsBound=!0)}_unbindEvents(e=!1){if(!s)return;const{element:t,events:n,passive:i,capture:r}=this.props;(this.eventsBound||e)&&(n.forEach(e=>{t.removeEventListener(e,this._handleEvent,{capture:r,passive:i})}),this.eventsBound=!1)}_toggleIdleState(e){this.setState(e=>({idle:!e.idle}),()=>{const{onActive:t,onIdle:n,stopOnIdle:s}=this.props,{idle:i}=this.state;i?(s&&(clearTimeout(this.tId),this.tId=null,this._unbindEvents()),n(e)):s||(this._bindEvents(),t(e))})}_handleEvent(e){const{remaining:t,pageX:n,pageY:s,idle:i}=this.state,{timeout:r,stopOnIdle:o}=this.props;if(this._onAction(e),t)return;if("mousemove"===e.type){if(e.pageX===n&&e.pageY===s)return;if(void 0===e.pageX&&void 0===e.pageY)return;if(this.getElapsedTime()<200)return}clearTimeout(this.tId),this.tId=null;const u=+new Date-this.getLastActiveTime();(i&&!o||!i&&u>r)&&this._toggleIdleState(e),this.setState({lastActive:+new Date,pageX:e.pageX,pageY:e.pageY}),i&&o||(this.tId=setTimeout(this._toggleIdleState,r))}reset(){clearTimeout(this.tId),this.tId=null,this._bindEvents(),this.setState({idle:!1,oldDate:+new Date,lastActive:+new Date,remaining:null});const{timeout:e}=this.props;this.tId=setTimeout(this._toggleIdleState,e)}pause(){const{remaining:e}=this.state;null===e&&(this._unbindEvents(),clearTimeout(this.tId),this.tId=null,this.setState({remaining:this.getRemainingTime()}))}resume(){const{remaining:e,idle:t}=this.state;null!==e&&(this._bindEvents(),t||(this.tId=setTimeout(this._toggleIdleState,e),this.setState({remaining:null,lastActive:+new Date})))}getRemainingTime(){const{remaining:e,lastActive:t}=this.state,{timeout:n}=this.props;if(null!==e)return e<0?0:e;const s=n-(+new Date-t);return s<0?0:s}getElapsedTime(){const{oldDate:e}=this.state;return+new Date-e}getLastActiveTime(){const{lastActive:e}=this.state;return e}isIdle(){const{idle:e}=this.state;return e}}function c({timeout:e=12e5,element:n=i,events:l=r,onIdle:c=(()=>{}),onActive:a=(()=>{}),onAction:h=(()=>{}),debounce:d=0,throttle:p=0,eventsThrottle:m=200,startOnMount:v=!0,stopOnIdle:f=!1,capture:g=!0,passive:b=!0}={}){const T=t.useRef(!1),I=t.useRef(!0),A=t.useRef(+new Date),E=t.useRef(+new Date),_=t.useRef(null),w=t.useRef(null),D=t.useRef(null),O=t.useRef(null),R=t.useRef(c),y=t.useRef(a),S=t.useRef(h),M=e=>{const t=!I.current;I.current=t,t?(f&&(clearTimeout(O.current),O.current=null,Y()),R.current(e)):f||(X(),y.current(e))};let L=t=>{if(S.current(t),_.current)return;if("mousemove"===t.type){if(t.pageX===w&&t.pageY===D)return;if(void 0===t.pageX&&void 0===t.pageY)return;if(B()<200)return}clearTimeout(O.current),O.current=null;const n=+new Date-P();(I.current&&!f||!I.current&&n>e)&&M(t),E.current=+new Date,w.current=t.pageX,D.current=t.pageY,I.current&&f||(O.current=setTimeout(M,e))};const X=()=>{s&&(T.current||(l.forEach(e=>{n.addEventListener(e,L,{capture:g,passive:b})}),T.current=!0))},Y=(e=!1)=>{s&&(T.current||e)&&(l.forEach(e=>{n.removeEventListener(e,L,{capture:g,passive:b})}),T.current=!1)},j=()=>{if(null!==_.current)return _.current<0?0:_.current;const t=e-(+new Date-E.current);return t<0?0:t},B=()=>+new Date-A.current,P=()=>E.current,x=()=>{clearTimeout(O.current),O.current=null,X(),I.current=!1,A.current=+new Date,E.current=+new Date,_.current=null,O.current=setTimeout(M,e)};return t.useEffect(()=>{if(d>0&&p>0)throw new Error("onAction can either be throttled or debounced (not both)");return m>0&&(L=u(L,m)),X(),v&&x(),()=>{clearTimeout(O.current),Y(!0)}},[]),t.useEffect(()=>{R.current=c},[c]),t.useEffect(()=>{y.current=a},[a]),t.useEffect(()=>{d>0&&(S.current=o(h,d)),p>0&&(S.current=u(h,p)),S.current=h},[h]),{isIdle:()=>I.current,pause:()=>{null===_.current&&(Y(),clearTimeout(O.current),O.current=null,_.current=j())},reset:x,resume:()=>{null!==_.current&&(X(),I.current||(O.current=setTimeout(M,_.current),_.current=null,E.current=+new Date))},getLastActiveTime:P,getElapsedTime:B,getRemainingTime:j}}l.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},l.defaultProps={timeout:12e5,element:i,events:r,onIdle:()=>{},onActive:()=>{},onAction:()=>{},debounce:0,throttle:0,eventsThrottle:200,startOnMount:!0,stopOnIdle:!1,capture:!0,passive:!0},c.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},c.defaultProps={timeout:12e5,element:i,events:r,onIdle:()=>{},onActive:()=>{},onAction:()=>{},debounce:0,throttle:0,eventsThrottle:200,startOnMount:!0,stopOnIdle:!1,capture:!0,passive:!0},exports.default=l,exports.useIdleTimer=c; //# sourceMappingURL=index.min.js.map