redux-active
Version:
Manages users active & idle state
7 lines (5 loc) • 3.92 kB
JavaScript
/**
* redux-active v1.2.0
* (c) 2017 Christoph Werner <christoph@codepunkt.de>
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.reduxActive={})}(this,function(e){"use strict";function t(e,t,i){function o(t){var n=l,i=d;return l=d=void 0,b=t,s=e.apply(i,n)}function r(e){var n=e-y;return void 0===y||n>=t||n<0||m&&e-b>=v}function u(){var e=c();if(r(e))return f(e);p=setTimeout(u,function(e){var n=t-(e-y);return m?$(n,v-(e-b)):n}(e))}function f(e){return p=void 0,h&&l?o(e):(l=d=void 0,s)}function a(){var e=c(),n=r(e);if(l=arguments,d=this,y=e,n){if(void 0===p)return function(e){return b=e,p=setTimeout(u,t),g?o(e):s}(y);if(m)return p=setTimeout(u,t),o(y)}return void 0===p&&(p=setTimeout(u,t)),s}var l,d,v,s,p,y,b=0,g=!1,m=!1,h=!0;if("function"!=typeof e)throw new TypeError(A);return t=_(t)||0,n(i)&&(g=!!i.leading,v=(m="maxWait"in i)?M(_(i.maxWait)||0,t):v,h="trailing"in i?!!i.trailing:h),a.cancel=function(){void 0!==p&&clearTimeout(p),b=0,l=y=d=p=void 0},a.flush=function(){return void 0===p?s:f(c())},a}var n=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)},i="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},o="object"==typeof i&&i&&i.Object===Object&&i,r="object"==typeof self&&self&&self.Object===Object&&self,u=o||r||Function("return this")(),c=function(){return u.Date.now()},f=u.Symbol,a=Object.prototype,l=a.hasOwnProperty,d=a.toString,v=f?f.toStringTag:void 0,s=function(e){var t=l.call(e,v),n=e[v];try{e[v]=void 0;var i=!0}catch(e){}var o=d.call(e);return i&&(t?e[v]=n:delete e[v]),o},p=Object.prototype.toString,y=function(e){return p.call(e)},b="[object Null]",g="[object Undefined]",m=f?f.toStringTag:void 0,h=function(e){return null==e?void 0===e?g:b:m&&m in Object(e)?s(e):y(e)},w=function(e){return null!=e&&"object"==typeof e},T="[object Symbol]",j=function(e){return"symbol"==typeof e||w(e)&&h(e)==T},S=NaN,x=/^\s+|\s+$/g,E=/^[-+]0x[0-9a-f]+$/i,I=/^0b[01]+$/i,O=/^0o[0-7]+$/i,D=parseInt,_=function(e){if("number"==typeof e)return e;if(j(e))return S;if(n(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=n(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=e.replace(x,"");var i=I.test(e);return i||O.test(e)?D(e.slice(2),i?2:8):E.test(e)?S:+e},A="Expected a function",M=Math.max,$=Math.min,k=t,C="Expected a function",L="@@active/IS_IDLE",N="@@active/IS_ACTIVE",W=function(e){return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=t.eventTarget,i=void 0===n?window:n,o=t.eventThrottleTimeout,r=void 0===o?250:o,u=t.eventTypes,c=void 0===u?["click","focus","keydown","mousemove","resize","scroll","touchmove","wheel"]:u,f=t.idleCheckInterval,a=void 0===f?1e3:f,l=t.idleTimeout,d=void 0===l?6e4:l,v=t.stateSelector,s=void 0===v?function(e){return e.isActive}:v,p=t.throttle,y=void 0===p?e||require("lodash/throttle"):p,b=+new Date;return function(e){var t=e.dispatch,n=e.getState;if(void 0===s(n()))return console.error("Error: redux-active has detected a problem with your stateSelector configuration!"),function(e){return function(t){return e(t)}};setInterval(function(){+new Date-b>d&&s(n())&&t({type:L,payload:{idleTimeout:d}})},a);var o=y(function(e){b=+new Date,s(n())||t({type:N,payload:{idleTimeout:d,eventType:e.type}})},r);return c.forEach(function(e){return i.addEventListener(e,o,!0)}),function(e){return function(t){return e(t)}}}}}(function(e,t,i){var o=!0,r=!0;if("function"!=typeof e)throw new TypeError(C);return n(i)&&(o="leading"in i?!!i.leading:o,r="trailing"in i?!!i.trailing:r),k(e,t,{leading:o,maxWait:t,trailing:r})});e.createActiveMiddleware=W,e.activeReducer=function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],t=arguments[1].type;return t!==L&&(t===N||e)},e.IS_ACTIVE=N,e.IS_IDLE=L,Object.defineProperty(e,"__esModule",{value:!0})});