UNPKG

@analytics/activity-utils

Version:
3 lines (2 loc) 4.7 kB
var utilActivity=function(n){function t(){return t=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)({}).hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n},t.apply(null,arguments)}var e=function(){};"undefined"==typeof process||process;var r="undefined"!=typeof document;function o(n,t){return t.charAt(0)[n]()+t.slice(1)}"undefined"!=typeof Deno&&Deno,r&&"nodejs"===window.name||"undefined"!=typeof navigator&&void 0!==navigator.userAgent&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom"));var u=o.bind(null,"toUpperCase"),i=o.bind(null,"toLowerCase");function c(n,t){void 0===t&&(t=!0);var e=function(n){return d(n)?u("null"):"object"==typeof n?function(n){return l(n.constructor)?n.constructor.name:null}(n):Object.prototype.toString.call(n).slice(8,-1)}(n);return t?i(e):e}function a(n,t){return typeof t===n}var l=a.bind(null,"function"),f=a.bind(null,"string");function d(n){return null===n}function s(n,t){if("object"!=typeof t||d(t))return!1;if(t instanceof n)return!0;var e=c(new n(""));if(function(n){return n instanceof Error||f(n.message)&&n.constructor&&function(n){return"number"===c(n)&&!isNaN(n)}(n.constructor.stackTraceLimit)}(t))for(;t;){if(c(t)===e)return!0;t=Object.getPrototypeOf(t)}return!1}function v(n,t){var e=n instanceof Element||n instanceof HTMLDocument;return e&&t?function(n,t){return void 0===t&&(t=""),n&&n.nodeName===t.toUpperCase()}(n,t):e}function p(n){var t=[].slice.call(arguments,1);return function(){return n.apply(void 0,[].slice.call(arguments).concat(t))}}a.bind(null,"undefined"),a.bind(null,"boolean"),a.bind(null,"symbol"),s.bind(null,TypeError),s.bind(null,SyntaxError),p(v,"form"),p(v,"button"),p(v,"input"),p(v,"select");var m="EventListener";function w(n,t){if(f(n))return t?w(document.querySelectorAll(n)):(e=n).split(e.indexOf(",")>-1?",":" ").map(function(n){return n.trim()});var e;if(NodeList.prototype.isPrototypeOf(n)){for(var r=[],o=n.length>>>0;o--;)r[o]=n[o];return r}var u=function(n){return n?function(n){return"array"===c(n)}(n)?n:[n]:[]}(n);return t?u.map(function(n){return f(n)?w(n,!0):n}).flat():u}function y(n,t){var e;return function(){return n&&(e=n.apply(t||this,arguments),n=null),e}}var b=function(n,t,o,u){var i=o||e,c=u||!1;if(!r)return i;var a=w(t),l=w(n,!0);if(!l.length)throw new Error("noElements");if(!a.length)throw new Error("noEvent");var f=[];return function n(t){t&&(f=[]);for(var e=t?"add"+m:"remove"+m,r=0;r<l.length;r++){var o=l[r];f[r]=t?c&&c.once?y(i):i:f[r]||i;for(var u=0;u<a.length;u++)o[e]?o["on"+a[u]]=t?f[r]:null:o[e](a[u],f[r],c)}return n.bind(null,!t)}("Event")};function g(n,t){return r&&l(window[n])?(e=window[n],o=t,void 0===(u=window)&&(u=null),l(e)?function(){e.apply(u,arguments),o.apply(u,arguments)}:o):window[n]=t;var e,o,u}g.bind(null,"onerror"),g.bind(null,"onload");var h="undefined"==typeof window,E="hidden",D=["mousemove","mousedown","touchmove","touchstart","touchend","keydown"];function j(n,t){void 0===t&&(t={});var e=function(n,t){var e=this,r=!1;return function(o){r||(n.call(e,o),r=!0,setTimeout(function(){return r=!1},t))}}(n,t.throttle||1e4),r=[];function o(){var n=function(n){if(h)return!1;var t=h||E in document?E:["webkit","moz","ms","o"].reduce(function(n,t){var e=t+"Hidden";return!n&&e in document?e:n},null),e="".concat(t.replace(/[H|h]idden/,""),"visibilitychange"),r=function(){return n(Boolean(document[t]))},o=function(){return document.addEventListener(e,r)};return o(),function(){return document.removeEventListener(e,r),o}}(function(n){n||e({type:"tabVisible"})});return r=[n].concat(D.map(function(n){return b(document,n,e)})).concat(b(window,"load",e)).concat(b(window,"scroll",e,{capture:!0,passive:!0})),u}function u(){r.map(function(n){return n()})}return o(),function(){return u(),o}}function A(n){var t,e,r=n.onIdle,o=n.onWakeUp,u=n.onHeartbeat,i=n.timeout,c=void 0===i?1e4:i,a=n.throttle,l=void 0===a?2e3:a,f=!1,d=!1,s=new Date,v=function(){return clearTimeout(t)};function p(n){v(),u&&!f&&u(O(s,d),n),o&&f&&(f=!1,o(O(e,d),n),s=new Date),t=setTimeout(function(){f=!0,r&&(e=new Date,r(O(s,d),n))},c)}var m=j(p,{throttle:l});return p({type:"init"}),{disable:function(){d=!0,f=!1,v();var n=m();return function(){return d=!1,s=new Date,p({type:"load"}),n()}},getStatus:function(){return{isIdle:f,isDisabled:d,active:f?0:O(s,d),idle:f?O(e,d):0}}}}function O(n,t){return t?0:Math.round(((new Date).getTime()-n.getTime())/1e3)}return n.onDomActivity=j,n.onIdle=function(n,e){return void 0===e&&(e={}),A(t({onIdle:n},e))},n.onUserActivity=A,n.onWakeUp=function(n,e){return void 0===e&&(e={}),A(t({onWakeUp:n},e))},n}({}); //# sourceMappingURL=activity-utils.js.map