redux-active
Version:
Manages users active & idle state
2 lines (1 loc) • 944 B
JavaScript
const IS_IDLE="@@active/IS_IDLE",IS_ACTIVE="@@active/IS_ACTIVE",injectThrottle=e=>({eventTarget:t=window,eventThrottleTimeout:r=250,eventTypes:o=["click","focus","keydown","mousemove","resize","scroll","touchmove","wheel"],idleCheckInterval:i=1e3,idleTimeout:a=6e4,stateSelector:c=(e=>e.isActive),throttle:l=e||require("lodash/throttle")}={})=>{let n=+new Date;return({dispatch:e,getState:d})=>{if(void 0===c(d()))return console.error("Error: redux-active has detected a problem with your stateSelector configuration!"),e=>t=>e(t);setInterval(()=>{+new Date-n>a&&c(d())&&e({type:IS_IDLE,payload:{idleTimeout:a}})},i);const I=l(t=>{n=+new Date,c(d())||e({type:IS_ACTIVE,payload:{idleTimeout:a,eventType:t.type}})},r);return o.forEach(e=>t.addEventListener(e,I,!0)),e=>t=>e(t)}},activeReducer=(e=!0,{type:t})=>t!==IS_IDLE&&(t===IS_ACTIVE||e),createActiveMiddleware=injectThrottle();export{createActiveMiddleware,activeReducer,IS_ACTIVE,IS_IDLE};