UNPKG

@analytics/activity-utils

Version:
3 lines (2 loc) 1.77 kB
import{addListener as t}from"@analytics/listener-utils";import{onTabChange as n}from"analytics-plugin-tab-events";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var o in e)({}).hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},e.apply(null,arguments)}var o=["mousemove","mousedown","touchmove","touchstart","touchend","keydown"];function r(e,r){void 0===r&&(r={});var i=function(t,n){var e=this,o=!1;return function(r){o||(t.call(e,r),o=!0,setTimeout(function(){return o=!1},n))}}(e,r.throttle||1e4),u=[];function a(){var e=n(function(t){t||i({type:"tabVisible"})});return u=[e].concat(o.map(function(n){return t(document,n,i)})).concat(t(window,"load",i)).concat(t(window,"scroll",i,{capture:!0,passive:!0})),c}function c(){u.map(function(t){return t()})}return a(),function(){return c(),a}}function i(t,n){return void 0===n&&(n={}),a(e({onIdle:t},n))}function u(t,n){return void 0===n&&(n={}),a(e({onWakeUp:t},n))}function a(t){var n,e,o=t.onIdle,i=t.onWakeUp,u=t.onHeartbeat,a=t.timeout,l=void 0===a?1e4:a,f=t.throttle,s=void 0===f?2e3:f,d=!1,v=!1,m=new Date,p=function(){return clearTimeout(n)};function w(t){p(),u&&!d&&u(c(m,v),t),i&&d&&(d=!1,i(c(e,v),t),m=new Date),n=setTimeout(function(){d=!0,o&&(e=new Date,o(c(m,v),t))},l)}var h=r(w,{throttle:s});return w({type:"init"}),{disable:function(){v=!0,d=!1,p();var t=h();return function(){return v=!1,m=new Date,w({type:"load"}),t()}},getStatus:function(){return{isIdle:d,isDisabled:v,active:d?0:c(m,v),idle:d?c(e,v):0}}}}function c(t,n){return n?0:Math.round(((new Date).getTime()-t.getTime())/1e3)}export{r as onDomActivity,i as onIdle,a as onUserActivity,u as onWakeUp}; //# sourceMappingURL=analytics-util-activity.module.js.map