browser-interaction-time
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -0,1 +1,27 @@ | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
/** | ||
@@ -71,5 +97,5 @@ * Checks if `value` is the | ||
/** Built-in value references. */ | ||
var Symbol = _root.Symbol; | ||
var Symbol$1 = _root.Symbol; | ||
var _Symbol = Symbol; | ||
var _Symbol = Symbol$1; | ||
@@ -546,6 +572,9 @@ /** Used for built-in method references. */ | ||
var documentIdleEvents = [ | ||
'wheel', | ||
'keydown', | ||
'keyup', | ||
'mousedown', | ||
'mousemove', | ||
'keyup', | ||
'keydown', | ||
'touchstart', | ||
'touchmove', | ||
'click', | ||
@@ -608,5 +637,6 @@ 'contextmenu' | ||
this.registerEventListeners = function () { | ||
var eventListenerOptions = { passive: true }; | ||
window.addEventListener('blur', _this.onBrowserTabInactive, eventListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, eventListenerOptions); | ||
var documentListenerOptions = { passive: true }; | ||
var windowListenerOptions = __assign({}, documentListenerOptions, { capture: true }); | ||
window.addEventListener('blur', _this.onBrowserTabInactive, windowListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, windowListenerOptions); | ||
var throttleResetIdleTime = throttle_1(_this.resetIdleTime, 2000, { | ||
@@ -617,6 +647,6 @@ leading: true, | ||
windowIdleEvents.forEach(function (event) { | ||
window.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
window.addEventListener(event, throttleResetIdleTime, windowListenerOptions); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
return document.addEventListener(event, throttleResetIdleTime, documentListenerOptions); | ||
}); | ||
@@ -623,0 +653,0 @@ }; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.browserInteractionTime = factory()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.browserInteractionTime = factory()); | ||
}(this, function () { 'use strict'; | ||
/** | ||
* Checks if `value` is the | ||
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) | ||
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is an object, else `false`. | ||
* @example | ||
* | ||
* _.isObject({}); | ||
* // => true | ||
* | ||
* _.isObject([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObject(_.noop); | ||
* // => true | ||
* | ||
* _.isObject(null); | ||
* // => false | ||
*/ | ||
function isObject(value) { | ||
var type = typeof value; | ||
return value != null && (type == 'object' || type == 'function'); | ||
} | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
var isObject_1 = isObject; | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
/** Detect free variable `global` from Node.js. */ | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal; | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var _freeGlobal = freeGlobal; | ||
/** | ||
* Checks if `value` is the | ||
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) | ||
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is an object, else `false`. | ||
* @example | ||
* | ||
* _.isObject({}); | ||
* // => true | ||
* | ||
* _.isObject([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObject(_.noop); | ||
* // => true | ||
* | ||
* _.isObject(null); | ||
* // => false | ||
*/ | ||
function isObject(value) { | ||
var type = typeof value; | ||
return value != null && (type == 'object' || type == 'function'); | ||
} | ||
/** Detect free variable `self`. */ | ||
var freeSelf = typeof self == 'object' && self && self.Object === Object && self; | ||
var isObject_1 = isObject; | ||
/** Used as a reference to the global object. */ | ||
var root = _freeGlobal || freeSelf || Function('return this')(); | ||
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
var _root = root; | ||
/** Detect free variable `global` from Node.js. */ | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal; | ||
/** | ||
* Gets the timestamp of the number of milliseconds that have elapsed since | ||
* the Unix epoch (1 January 1970 00:00:00 UTC). | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 2.4.0 | ||
* @category Date | ||
* @returns {number} Returns the timestamp. | ||
* @example | ||
* | ||
* _.defer(function(stamp) { | ||
* console.log(_.now() - stamp); | ||
* }, _.now()); | ||
* // => Logs the number of milliseconds it took for the deferred invocation. | ||
*/ | ||
var now = function() { | ||
return _root.Date.now(); | ||
}; | ||
var _freeGlobal = freeGlobal; | ||
var now_1 = now; | ||
/** Detect free variable `self`. */ | ||
var freeSelf = typeof self == 'object' && self && self.Object === Object && self; | ||
/** Built-in value references. */ | ||
var Symbol = _root.Symbol; | ||
/** Used as a reference to the global object. */ | ||
var root = _freeGlobal || freeSelf || Function('return this')(); | ||
var _Symbol = Symbol; | ||
var _root = root; | ||
/** Used for built-in method references. */ | ||
var objectProto = Object.prototype; | ||
/** | ||
* Gets the timestamp of the number of milliseconds that have elapsed since | ||
* the Unix epoch (1 January 1970 00:00:00 UTC). | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 2.4.0 | ||
* @category Date | ||
* @returns {number} Returns the timestamp. | ||
* @example | ||
* | ||
* _.defer(function(stamp) { | ||
* console.log(_.now() - stamp); | ||
* }, _.now()); | ||
* // => Logs the number of milliseconds it took for the deferred invocation. | ||
*/ | ||
var now = function() { | ||
return _root.Date.now(); | ||
}; | ||
/** Used to check objects for own properties. */ | ||
var hasOwnProperty = objectProto.hasOwnProperty; | ||
var now_1 = now; | ||
/** | ||
* Used to resolve the | ||
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | ||
* of values. | ||
*/ | ||
var nativeObjectToString = objectProto.toString; | ||
/** Built-in value references. */ | ||
var Symbol$1 = _root.Symbol; | ||
/** Built-in value references. */ | ||
var symToStringTag = _Symbol ? _Symbol.toStringTag : undefined; | ||
var _Symbol = Symbol$1; | ||
/** | ||
* A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the raw `toStringTag`. | ||
*/ | ||
function getRawTag(value) { | ||
var isOwn = hasOwnProperty.call(value, symToStringTag), | ||
tag = value[symToStringTag]; | ||
/** Used for built-in method references. */ | ||
var objectProto = Object.prototype; | ||
try { | ||
value[symToStringTag] = undefined; | ||
var unmasked = true; | ||
} catch (e) {} | ||
/** Used to check objects for own properties. */ | ||
var hasOwnProperty = objectProto.hasOwnProperty; | ||
var result = nativeObjectToString.call(value); | ||
if (unmasked) { | ||
if (isOwn) { | ||
value[symToStringTag] = tag; | ||
} else { | ||
delete value[symToStringTag]; | ||
/** | ||
* Used to resolve the | ||
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | ||
* of values. | ||
*/ | ||
var nativeObjectToString = objectProto.toString; | ||
/** Built-in value references. */ | ||
var symToStringTag = _Symbol ? _Symbol.toStringTag : undefined; | ||
/** | ||
* A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the raw `toStringTag`. | ||
*/ | ||
function getRawTag(value) { | ||
var isOwn = hasOwnProperty.call(value, symToStringTag), | ||
tag = value[symToStringTag]; | ||
try { | ||
value[symToStringTag] = undefined; | ||
var unmasked = true; | ||
} catch (e) {} | ||
var result = nativeObjectToString.call(value); | ||
if (unmasked) { | ||
if (isOwn) { | ||
value[symToStringTag] = tag; | ||
} else { | ||
delete value[symToStringTag]; | ||
} | ||
} | ||
return result; | ||
} | ||
return result; | ||
} | ||
var _getRawTag = getRawTag; | ||
var _getRawTag = getRawTag; | ||
/** Used for built-in method references. */ | ||
var objectProto$1 = Object.prototype; | ||
/** Used for built-in method references. */ | ||
var objectProto$1 = Object.prototype; | ||
/** | ||
* Used to resolve the | ||
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | ||
* of values. | ||
*/ | ||
var nativeObjectToString$1 = objectProto$1.toString; | ||
/** | ||
* Used to resolve the | ||
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | ||
* of values. | ||
*/ | ||
var nativeObjectToString$1 = objectProto$1.toString; | ||
/** | ||
* Converts `value` to a string using `Object.prototype.toString`. | ||
* | ||
* @private | ||
* @param {*} value The value to convert. | ||
* @returns {string} Returns the converted string. | ||
*/ | ||
function objectToString(value) { | ||
return nativeObjectToString$1.call(value); | ||
} | ||
/** | ||
* Converts `value` to a string using `Object.prototype.toString`. | ||
* | ||
* @private | ||
* @param {*} value The value to convert. | ||
* @returns {string} Returns the converted string. | ||
*/ | ||
function objectToString(value) { | ||
return nativeObjectToString$1.call(value); | ||
} | ||
var _objectToString = objectToString; | ||
var _objectToString = objectToString; | ||
/** `Object#toString` result references. */ | ||
var nullTag = '[object Null]', | ||
undefinedTag = '[object Undefined]'; | ||
/** `Object#toString` result references. */ | ||
var nullTag = '[object Null]', | ||
undefinedTag = '[object Undefined]'; | ||
/** Built-in value references. */ | ||
var symToStringTag$1 = _Symbol ? _Symbol.toStringTag : undefined; | ||
/** Built-in value references. */ | ||
var symToStringTag$1 = _Symbol ? _Symbol.toStringTag : undefined; | ||
/** | ||
* The base implementation of `getTag` without fallbacks for buggy environments. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the `toStringTag`. | ||
*/ | ||
function baseGetTag(value) { | ||
if (value == null) { | ||
return value === undefined ? undefinedTag : nullTag; | ||
/** | ||
* The base implementation of `getTag` without fallbacks for buggy environments. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the `toStringTag`. | ||
*/ | ||
function baseGetTag(value) { | ||
if (value == null) { | ||
return value === undefined ? undefinedTag : nullTag; | ||
} | ||
return (symToStringTag$1 && symToStringTag$1 in Object(value)) | ||
? _getRawTag(value) | ||
: _objectToString(value); | ||
} | ||
return (symToStringTag$1 && symToStringTag$1 in Object(value)) | ||
? _getRawTag(value) | ||
: _objectToString(value); | ||
} | ||
var _baseGetTag = baseGetTag; | ||
var _baseGetTag = baseGetTag; | ||
/** | ||
* Checks if `value` is object-like. A value is object-like if it's not `null` | ||
* and has a `typeof` result of "object". | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is object-like, else `false`. | ||
* @example | ||
* | ||
* _.isObjectLike({}); | ||
* // => true | ||
* | ||
* _.isObjectLike([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObjectLike(_.noop); | ||
* // => false | ||
* | ||
* _.isObjectLike(null); | ||
* // => false | ||
*/ | ||
function isObjectLike(value) { | ||
return value != null && typeof value == 'object'; | ||
} | ||
/** | ||
* Checks if `value` is object-like. A value is object-like if it's not `null` | ||
* and has a `typeof` result of "object". | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is object-like, else `false`. | ||
* @example | ||
* | ||
* _.isObjectLike({}); | ||
* // => true | ||
* | ||
* _.isObjectLike([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObjectLike(_.noop); | ||
* // => false | ||
* | ||
* _.isObjectLike(null); | ||
* // => false | ||
*/ | ||
function isObjectLike(value) { | ||
return value != null && typeof value == 'object'; | ||
} | ||
var isObjectLike_1 = isObjectLike; | ||
var isObjectLike_1 = isObjectLike; | ||
/** `Object#toString` result references. */ | ||
var symbolTag = '[object Symbol]'; | ||
/** `Object#toString` result references. */ | ||
var symbolTag = '[object Symbol]'; | ||
/** | ||
* Checks if `value` is classified as a `Symbol` primitive or object. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`. | ||
* @example | ||
* | ||
* _.isSymbol(Symbol.iterator); | ||
* // => true | ||
* | ||
* _.isSymbol('abc'); | ||
* // => false | ||
*/ | ||
function isSymbol(value) { | ||
return typeof value == 'symbol' || | ||
(isObjectLike_1(value) && _baseGetTag(value) == symbolTag); | ||
} | ||
/** | ||
* Checks if `value` is classified as a `Symbol` primitive or object. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`. | ||
* @example | ||
* | ||
* _.isSymbol(Symbol.iterator); | ||
* // => true | ||
* | ||
* _.isSymbol('abc'); | ||
* // => false | ||
*/ | ||
function isSymbol(value) { | ||
return typeof value == 'symbol' || | ||
(isObjectLike_1(value) && _baseGetTag(value) == symbolTag); | ||
} | ||
var isSymbol_1 = isSymbol; | ||
var isSymbol_1 = isSymbol; | ||
/** Used as references for various `Number` constants. */ | ||
var NAN = 0 / 0; | ||
/** Used as references for various `Number` constants. */ | ||
var NAN = 0 / 0; | ||
/** Used to match leading and trailing whitespace. */ | ||
var reTrim = /^\s+|\s+$/g; | ||
/** Used to match leading and trailing whitespace. */ | ||
var reTrim = /^\s+|\s+$/g; | ||
/** Used to detect bad signed hexadecimal string values. */ | ||
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; | ||
/** Used to detect bad signed hexadecimal string values. */ | ||
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; | ||
/** Used to detect binary string values. */ | ||
var reIsBinary = /^0b[01]+$/i; | ||
/** Used to detect binary string values. */ | ||
var reIsBinary = /^0b[01]+$/i; | ||
/** Used to detect octal string values. */ | ||
var reIsOctal = /^0o[0-7]+$/i; | ||
/** Used to detect octal string values. */ | ||
var reIsOctal = /^0o[0-7]+$/i; | ||
/** Built-in method references without a dependency on `root`. */ | ||
var freeParseInt = parseInt; | ||
/** Built-in method references without a dependency on `root`. */ | ||
var freeParseInt = parseInt; | ||
/** | ||
* Converts `value` to a number. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to process. | ||
* @returns {number} Returns the number. | ||
* @example | ||
* | ||
* _.toNumber(3.2); | ||
* // => 3.2 | ||
* | ||
* _.toNumber(Number.MIN_VALUE); | ||
* // => 5e-324 | ||
* | ||
* _.toNumber(Infinity); | ||
* // => Infinity | ||
* | ||
* _.toNumber('3.2'); | ||
* // => 3.2 | ||
*/ | ||
function toNumber(value) { | ||
if (typeof value == 'number') { | ||
return value; | ||
/** | ||
* Converts `value` to a number. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to process. | ||
* @returns {number} Returns the number. | ||
* @example | ||
* | ||
* _.toNumber(3.2); | ||
* // => 3.2 | ||
* | ||
* _.toNumber(Number.MIN_VALUE); | ||
* // => 5e-324 | ||
* | ||
* _.toNumber(Infinity); | ||
* // => Infinity | ||
* | ||
* _.toNumber('3.2'); | ||
* // => 3.2 | ||
*/ | ||
function toNumber(value) { | ||
if (typeof value == 'number') { | ||
return value; | ||
} | ||
if (isSymbol_1(value)) { | ||
return NAN; | ||
} | ||
if (isObject_1(value)) { | ||
var other = typeof value.valueOf == 'function' ? value.valueOf() : value; | ||
value = isObject_1(other) ? (other + '') : other; | ||
} | ||
if (typeof value != 'string') { | ||
return value === 0 ? value : +value; | ||
} | ||
value = value.replace(reTrim, ''); | ||
var isBinary = reIsBinary.test(value); | ||
return (isBinary || reIsOctal.test(value)) | ||
? freeParseInt(value.slice(2), isBinary ? 2 : 8) | ||
: (reIsBadHex.test(value) ? NAN : +value); | ||
} | ||
if (isSymbol_1(value)) { | ||
return NAN; | ||
} | ||
if (isObject_1(value)) { | ||
var other = typeof value.valueOf == 'function' ? value.valueOf() : value; | ||
value = isObject_1(other) ? (other + '') : other; | ||
} | ||
if (typeof value != 'string') { | ||
return value === 0 ? value : +value; | ||
} | ||
value = value.replace(reTrim, ''); | ||
var isBinary = reIsBinary.test(value); | ||
return (isBinary || reIsOctal.test(value)) | ||
? freeParseInt(value.slice(2), isBinary ? 2 : 8) | ||
: (reIsBadHex.test(value) ? NAN : +value); | ||
} | ||
var toNumber_1 = toNumber; | ||
var toNumber_1 = toNumber; | ||
/** Error message constants. */ | ||
var FUNC_ERROR_TEXT = 'Expected a function'; | ||
/** Error message constants. */ | ||
var FUNC_ERROR_TEXT = 'Expected a function'; | ||
/* Built-in method references for those with the same name as other `lodash` methods. */ | ||
var nativeMax = Math.max, | ||
nativeMin = Math.min; | ||
/* Built-in method references for those with the same name as other `lodash` methods. */ | ||
var nativeMax = Math.max, | ||
nativeMin = Math.min; | ||
/** | ||
* Creates a debounced function that delays invoking `func` until after `wait` | ||
* milliseconds have elapsed since the last time the debounced function was | ||
* invoked. The debounced function comes with a `cancel` method to cancel | ||
* delayed `func` invocations and a `flush` method to immediately invoke them. | ||
* Provide `options` to indicate whether `func` should be invoked on the | ||
* leading and/or trailing edge of the `wait` timeout. The `func` is invoked | ||
* with the last arguments provided to the debounced function. Subsequent | ||
* calls to the debounced function return the result of the last `func` | ||
* invocation. | ||
* | ||
* **Note:** If `leading` and `trailing` options are `true`, `func` is | ||
* invoked on the trailing edge of the timeout only if the debounced function | ||
* is invoked more than once during the `wait` timeout. | ||
* | ||
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | ||
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | ||
* | ||
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | ||
* for details over the differences between `_.debounce` and `_.throttle`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Function | ||
* @param {Function} func The function to debounce. | ||
* @param {number} [wait=0] The number of milliseconds to delay. | ||
* @param {Object} [options={}] The options object. | ||
* @param {boolean} [options.leading=false] | ||
* Specify invoking on the leading edge of the timeout. | ||
* @param {number} [options.maxWait] | ||
* The maximum time `func` is allowed to be delayed before it's invoked. | ||
* @param {boolean} [options.trailing=true] | ||
* Specify invoking on the trailing edge of the timeout. | ||
* @returns {Function} Returns the new debounced function. | ||
* @example | ||
* | ||
* // Avoid costly calculations while the window size is in flux. | ||
* jQuery(window).on('resize', _.debounce(calculateLayout, 150)); | ||
* | ||
* // Invoke `sendMail` when clicked, debouncing subsequent calls. | ||
* jQuery(element).on('click', _.debounce(sendMail, 300, { | ||
* 'leading': true, | ||
* 'trailing': false | ||
* })); | ||
* | ||
* // Ensure `batchLog` is invoked once after 1 second of debounced calls. | ||
* var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); | ||
* var source = new EventSource('/stream'); | ||
* jQuery(source).on('message', debounced); | ||
* | ||
* // Cancel the trailing debounced invocation. | ||
* jQuery(window).on('popstate', debounced.cancel); | ||
*/ | ||
function debounce(func, wait, options) { | ||
var lastArgs, | ||
lastThis, | ||
maxWait, | ||
result, | ||
timerId, | ||
lastCallTime, | ||
lastInvokeTime = 0, | ||
leading = false, | ||
maxing = false, | ||
trailing = true; | ||
/** | ||
* Creates a debounced function that delays invoking `func` until after `wait` | ||
* milliseconds have elapsed since the last time the debounced function was | ||
* invoked. The debounced function comes with a `cancel` method to cancel | ||
* delayed `func` invocations and a `flush` method to immediately invoke them. | ||
* Provide `options` to indicate whether `func` should be invoked on the | ||
* leading and/or trailing edge of the `wait` timeout. The `func` is invoked | ||
* with the last arguments provided to the debounced function. Subsequent | ||
* calls to the debounced function return the result of the last `func` | ||
* invocation. | ||
* | ||
* **Note:** If `leading` and `trailing` options are `true`, `func` is | ||
* invoked on the trailing edge of the timeout only if the debounced function | ||
* is invoked more than once during the `wait` timeout. | ||
* | ||
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | ||
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | ||
* | ||
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | ||
* for details over the differences between `_.debounce` and `_.throttle`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Function | ||
* @param {Function} func The function to debounce. | ||
* @param {number} [wait=0] The number of milliseconds to delay. | ||
* @param {Object} [options={}] The options object. | ||
* @param {boolean} [options.leading=false] | ||
* Specify invoking on the leading edge of the timeout. | ||
* @param {number} [options.maxWait] | ||
* The maximum time `func` is allowed to be delayed before it's invoked. | ||
* @param {boolean} [options.trailing=true] | ||
* Specify invoking on the trailing edge of the timeout. | ||
* @returns {Function} Returns the new debounced function. | ||
* @example | ||
* | ||
* // Avoid costly calculations while the window size is in flux. | ||
* jQuery(window).on('resize', _.debounce(calculateLayout, 150)); | ||
* | ||
* // Invoke `sendMail` when clicked, debouncing subsequent calls. | ||
* jQuery(element).on('click', _.debounce(sendMail, 300, { | ||
* 'leading': true, | ||
* 'trailing': false | ||
* })); | ||
* | ||
* // Ensure `batchLog` is invoked once after 1 second of debounced calls. | ||
* var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); | ||
* var source = new EventSource('/stream'); | ||
* jQuery(source).on('message', debounced); | ||
* | ||
* // Cancel the trailing debounced invocation. | ||
* jQuery(window).on('popstate', debounced.cancel); | ||
*/ | ||
function debounce(func, wait, options) { | ||
var lastArgs, | ||
lastThis, | ||
maxWait, | ||
result, | ||
timerId, | ||
lastCallTime, | ||
lastInvokeTime = 0, | ||
leading = false, | ||
maxing = false, | ||
trailing = true; | ||
if (typeof func != 'function') { | ||
throw new TypeError(FUNC_ERROR_TEXT); | ||
} | ||
wait = toNumber_1(wait) || 0; | ||
if (isObject_1(options)) { | ||
leading = !!options.leading; | ||
maxing = 'maxWait' in options; | ||
maxWait = maxing ? nativeMax(toNumber_1(options.maxWait) || 0, wait) : maxWait; | ||
trailing = 'trailing' in options ? !!options.trailing : trailing; | ||
} | ||
if (typeof func != 'function') { | ||
throw new TypeError(FUNC_ERROR_TEXT); | ||
} | ||
wait = toNumber_1(wait) || 0; | ||
if (isObject_1(options)) { | ||
leading = !!options.leading; | ||
maxing = 'maxWait' in options; | ||
maxWait = maxing ? nativeMax(toNumber_1(options.maxWait) || 0, wait) : maxWait; | ||
trailing = 'trailing' in options ? !!options.trailing : trailing; | ||
} | ||
function invokeFunc(time) { | ||
var args = lastArgs, | ||
thisArg = lastThis; | ||
function invokeFunc(time) { | ||
var args = lastArgs, | ||
thisArg = lastThis; | ||
lastArgs = lastThis = undefined; | ||
lastInvokeTime = time; | ||
result = func.apply(thisArg, args); | ||
return result; | ||
} | ||
lastArgs = lastThis = undefined; | ||
lastInvokeTime = time; | ||
result = func.apply(thisArg, args); | ||
return result; | ||
} | ||
function leadingEdge(time) { | ||
// Reset any `maxWait` timer. | ||
lastInvokeTime = time; | ||
// Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); | ||
// Invoke the leading edge. | ||
return leading ? invokeFunc(time) : result; | ||
} | ||
function leadingEdge(time) { | ||
// Reset any `maxWait` timer. | ||
lastInvokeTime = time; | ||
// Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); | ||
// Invoke the leading edge. | ||
return leading ? invokeFunc(time) : result; | ||
} | ||
function remainingWait(time) { | ||
var timeSinceLastCall = time - lastCallTime, | ||
timeSinceLastInvoke = time - lastInvokeTime, | ||
timeWaiting = wait - timeSinceLastCall; | ||
function remainingWait(time) { | ||
var timeSinceLastCall = time - lastCallTime, | ||
timeSinceLastInvoke = time - lastInvokeTime, | ||
timeWaiting = wait - timeSinceLastCall; | ||
return maxing | ||
? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) | ||
: timeWaiting; | ||
} | ||
return maxing | ||
? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) | ||
: timeWaiting; | ||
} | ||
function shouldInvoke(time) { | ||
var timeSinceLastCall = time - lastCallTime, | ||
timeSinceLastInvoke = time - lastInvokeTime; | ||
function shouldInvoke(time) { | ||
var timeSinceLastCall = time - lastCallTime, | ||
timeSinceLastInvoke = time - lastInvokeTime; | ||
// Either this is the first call, activity has stopped and we're at the | ||
// trailing edge, the system time has gone backwards and we're treating | ||
// it as the trailing edge, or we've hit the `maxWait` limit. | ||
return (lastCallTime === undefined || (timeSinceLastCall >= wait) || | ||
(timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); | ||
} | ||
// Either this is the first call, activity has stopped and we're at the | ||
// trailing edge, the system time has gone backwards and we're treating | ||
// it as the trailing edge, or we've hit the `maxWait` limit. | ||
return (lastCallTime === undefined || (timeSinceLastCall >= wait) || | ||
(timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); | ||
} | ||
function timerExpired() { | ||
var time = now_1(); | ||
if (shouldInvoke(time)) { | ||
return trailingEdge(time); | ||
function timerExpired() { | ||
var time = now_1(); | ||
if (shouldInvoke(time)) { | ||
return trailingEdge(time); | ||
} | ||
// Restart the timer. | ||
timerId = setTimeout(timerExpired, remainingWait(time)); | ||
} | ||
// Restart the timer. | ||
timerId = setTimeout(timerExpired, remainingWait(time)); | ||
} | ||
function trailingEdge(time) { | ||
timerId = undefined; | ||
function trailingEdge(time) { | ||
timerId = undefined; | ||
// Only invoke if we have `lastArgs` which means `func` has been | ||
// debounced at least once. | ||
if (trailing && lastArgs) { | ||
return invokeFunc(time); | ||
// Only invoke if we have `lastArgs` which means `func` has been | ||
// debounced at least once. | ||
if (trailing && lastArgs) { | ||
return invokeFunc(time); | ||
} | ||
lastArgs = lastThis = undefined; | ||
return result; | ||
} | ||
lastArgs = lastThis = undefined; | ||
return result; | ||
} | ||
function cancel() { | ||
if (timerId !== undefined) { | ||
clearTimeout(timerId); | ||
function cancel() { | ||
if (timerId !== undefined) { | ||
clearTimeout(timerId); | ||
} | ||
lastInvokeTime = 0; | ||
lastArgs = lastCallTime = lastThis = timerId = undefined; | ||
} | ||
lastInvokeTime = 0; | ||
lastArgs = lastCallTime = lastThis = timerId = undefined; | ||
} | ||
function flush() { | ||
return timerId === undefined ? result : trailingEdge(now_1()); | ||
} | ||
function flush() { | ||
return timerId === undefined ? result : trailingEdge(now_1()); | ||
} | ||
function debounced() { | ||
var time = now_1(), | ||
isInvoking = shouldInvoke(time); | ||
function debounced() { | ||
var time = now_1(), | ||
isInvoking = shouldInvoke(time); | ||
lastArgs = arguments; | ||
lastThis = this; | ||
lastCallTime = time; | ||
lastArgs = arguments; | ||
lastThis = this; | ||
lastCallTime = time; | ||
if (isInvoking) { | ||
if (isInvoking) { | ||
if (timerId === undefined) { | ||
return leadingEdge(lastCallTime); | ||
} | ||
if (maxing) { | ||
// Handle invocations in a tight loop. | ||
timerId = setTimeout(timerExpired, wait); | ||
return invokeFunc(lastCallTime); | ||
} | ||
} | ||
if (timerId === undefined) { | ||
return leadingEdge(lastCallTime); | ||
} | ||
if (maxing) { | ||
// Handle invocations in a tight loop. | ||
timerId = setTimeout(timerExpired, wait); | ||
return invokeFunc(lastCallTime); | ||
} | ||
return result; | ||
} | ||
if (timerId === undefined) { | ||
timerId = setTimeout(timerExpired, wait); | ||
} | ||
return result; | ||
debounced.cancel = cancel; | ||
debounced.flush = flush; | ||
return debounced; | ||
} | ||
debounced.cancel = cancel; | ||
debounced.flush = flush; | ||
return debounced; | ||
} | ||
var debounce_1 = debounce; | ||
var debounce_1 = debounce; | ||
/** Error message constants. */ | ||
var FUNC_ERROR_TEXT$1 = 'Expected a function'; | ||
/** Error message constants. */ | ||
var FUNC_ERROR_TEXT$1 = 'Expected a function'; | ||
/** | ||
* Creates a throttled function that only invokes `func` at most once per | ||
* every `wait` milliseconds. The throttled function comes with a `cancel` | ||
* method to cancel delayed `func` invocations and a `flush` method to | ||
* immediately invoke them. Provide `options` to indicate whether `func` | ||
* should be invoked on the leading and/or trailing edge of the `wait` | ||
* timeout. The `func` is invoked with the last arguments provided to the | ||
* throttled function. Subsequent calls to the throttled function return the | ||
* result of the last `func` invocation. | ||
* | ||
* **Note:** If `leading` and `trailing` options are `true`, `func` is | ||
* invoked on the trailing edge of the timeout only if the throttled function | ||
* is invoked more than once during the `wait` timeout. | ||
* | ||
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | ||
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | ||
* | ||
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | ||
* for details over the differences between `_.throttle` and `_.debounce`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Function | ||
* @param {Function} func The function to throttle. | ||
* @param {number} [wait=0] The number of milliseconds to throttle invocations to. | ||
* @param {Object} [options={}] The options object. | ||
* @param {boolean} [options.leading=true] | ||
* Specify invoking on the leading edge of the timeout. | ||
* @param {boolean} [options.trailing=true] | ||
* Specify invoking on the trailing edge of the timeout. | ||
* @returns {Function} Returns the new throttled function. | ||
* @example | ||
* | ||
* // Avoid excessively updating the position while scrolling. | ||
* jQuery(window).on('scroll', _.throttle(updatePosition, 100)); | ||
* | ||
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes. | ||
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false }); | ||
* jQuery(element).on('click', throttled); | ||
* | ||
* // Cancel the trailing throttled invocation. | ||
* jQuery(window).on('popstate', throttled.cancel); | ||
*/ | ||
function throttle(func, wait, options) { | ||
var leading = true, | ||
trailing = true; | ||
/** | ||
* Creates a throttled function that only invokes `func` at most once per | ||
* every `wait` milliseconds. The throttled function comes with a `cancel` | ||
* method to cancel delayed `func` invocations and a `flush` method to | ||
* immediately invoke them. Provide `options` to indicate whether `func` | ||
* should be invoked on the leading and/or trailing edge of the `wait` | ||
* timeout. The `func` is invoked with the last arguments provided to the | ||
* throttled function. Subsequent calls to the throttled function return the | ||
* result of the last `func` invocation. | ||
* | ||
* **Note:** If `leading` and `trailing` options are `true`, `func` is | ||
* invoked on the trailing edge of the timeout only if the throttled function | ||
* is invoked more than once during the `wait` timeout. | ||
* | ||
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | ||
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | ||
* | ||
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | ||
* for details over the differences between `_.throttle` and `_.debounce`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.1.0 | ||
* @category Function | ||
* @param {Function} func The function to throttle. | ||
* @param {number} [wait=0] The number of milliseconds to throttle invocations to. | ||
* @param {Object} [options={}] The options object. | ||
* @param {boolean} [options.leading=true] | ||
* Specify invoking on the leading edge of the timeout. | ||
* @param {boolean} [options.trailing=true] | ||
* Specify invoking on the trailing edge of the timeout. | ||
* @returns {Function} Returns the new throttled function. | ||
* @example | ||
* | ||
* // Avoid excessively updating the position while scrolling. | ||
* jQuery(window).on('scroll', _.throttle(updatePosition, 100)); | ||
* | ||
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes. | ||
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false }); | ||
* jQuery(element).on('click', throttled); | ||
* | ||
* // Cancel the trailing throttled invocation. | ||
* jQuery(window).on('popstate', throttled.cancel); | ||
*/ | ||
function throttle(func, wait, options) { | ||
var leading = true, | ||
trailing = true; | ||
if (typeof func != 'function') { | ||
throw new TypeError(FUNC_ERROR_TEXT$1); | ||
if (typeof func != 'function') { | ||
throw new TypeError(FUNC_ERROR_TEXT$1); | ||
} | ||
if (isObject_1(options)) { | ||
leading = 'leading' in options ? !!options.leading : leading; | ||
trailing = 'trailing' in options ? !!options.trailing : trailing; | ||
} | ||
return debounce_1(func, wait, { | ||
'leading': leading, | ||
'maxWait': wait, | ||
'trailing': trailing | ||
}); | ||
} | ||
if (isObject_1(options)) { | ||
leading = 'leading' in options ? !!options.leading : leading; | ||
trailing = 'trailing' in options ? !!options.trailing : trailing; | ||
} | ||
return debounce_1(func, wait, { | ||
'leading': leading, | ||
'maxWait': wait, | ||
'trailing': trailing | ||
}); | ||
} | ||
var throttle_1 = throttle; | ||
var throttle_1 = throttle; | ||
var windowIdleEvents = ['scroll', 'resize']; | ||
var documentIdleEvents = [ | ||
'mousemove', | ||
'keyup', | ||
'keydown', | ||
'touchstart', | ||
'click', | ||
'contextmenu' | ||
]; | ||
var BrowserInteractionTime = /** @class */ (function () { | ||
function BrowserInteractionTime(_a) { | ||
var timeIntervalEllapsedCallbacks = _a.timeIntervalEllapsedCallbacks, absoluteTimeEllapsedCallbacks = _a.absoluteTimeEllapsedCallbacks, checkCallbacksIntervalMs = _a.checkCallbacksIntervalMs, browserTabInactiveCallbacks = _a.browserTabInactiveCallbacks, browserTabActiveCallbacks = _a.browserTabActiveCallbacks, idleTimeoutMs = _a.idleTimeoutMs; | ||
var _this = this; | ||
this.onBrowserTabInactive = function (event) { | ||
// if running pause timer | ||
if (_this.isRunning()) { | ||
_this.stopTimer(); | ||
} | ||
_this.browserTabInactiveCallbacks.forEach(function (fn) { | ||
return fn(_this.getTimeInMilliseconds()); | ||
}); | ||
}; | ||
this.onBrowserTabActive = function (event) { | ||
// if not running start timer | ||
if (!_this.isRunning()) { | ||
_this.startTimer(); | ||
} | ||
_this.browserTabActiveCallbacks.forEach(function (fn) { | ||
return fn(_this.getTimeInMilliseconds()); | ||
}); | ||
}; | ||
this.onTimePassed = function () { | ||
// check all callbacks time and if passed execute callback | ||
_this.absoluteTimeEllapsedCallbacks.forEach(function (_a, index) { | ||
var callback = _a.callback, pending = _a.pending, timeInMilliseconds = _a.timeInMilliseconds; | ||
if (pending && timeInMilliseconds <= _this.getTimeInMilliseconds()) { | ||
callback(_this.getTimeInMilliseconds()); | ||
_this.absoluteTimeEllapsedCallbacks[index].pending = false; | ||
} | ||
}); | ||
_this.timeIntervalEllapsedCallbacks.forEach(function (_a, index) { | ||
var callback = _a.callback, timeInMilliseconds = _a.timeInMilliseconds, multiplier = _a.multiplier; | ||
if (timeInMilliseconds <= _this.getTimeInMilliseconds()) { | ||
callback(_this.getTimeInMilliseconds()); | ||
_this.timeIntervalEllapsedCallbacks[index].timeInMilliseconds = multiplier(timeInMilliseconds); | ||
} | ||
}); | ||
if (_this.currentIdleTimeMs >= _this.idleTimeoutMs && _this.isRunning()) { | ||
_this.idle = true; | ||
_this.stopTimer(); | ||
} | ||
else { | ||
_this.currentIdleTimeMs += _this.checkCallbacksIntervalMs; | ||
} | ||
}; | ||
this.resetIdleTime = function () { | ||
if (_this.idle) { | ||
_this.startTimer(); | ||
} | ||
_this.idle = false; | ||
_this.currentIdleTimeMs = 0; | ||
}; | ||
this.registerEventListeners = function () { | ||
var eventListenerOptions = { passive: true }; | ||
window.addEventListener('blur', _this.onBrowserTabInactive, eventListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, eventListenerOptions); | ||
var throttleResetIdleTime = throttle_1(_this.resetIdleTime, 2000, { | ||
leading: true, | ||
trailing: false | ||
}); | ||
windowIdleEvents.forEach(function (event) { | ||
window.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
}); | ||
}; | ||
this.unregisterEventListeners = function () { | ||
window.removeEventListener('blur', _this.onBrowserTabInactive); | ||
window.removeEventListener('focus', _this.onBrowserTabActive); | ||
windowIdleEvents.forEach(function (event) { | ||
return window.removeEventListener(event, _this.resetIdleTime); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.removeEventListener(event, _this.resetIdleTime); | ||
}); | ||
}; | ||
this.checkCallbacksOnInterval = function () { | ||
_this.checkCallbackIntervalId = window.setInterval(function () { | ||
_this.onTimePassed(); | ||
}, _this.checkCallbacksIntervalMs); | ||
}; | ||
this.startTimer = function () { | ||
if (!_this.checkCallbackIntervalId) { | ||
_this.checkCallbacksOnInterval(); | ||
} | ||
var last = _this.times[_this.times.length - 1]; | ||
if (last && last.stop === null) { | ||
return; | ||
} | ||
_this.times.push({ | ||
start: performance.now(), | ||
stop: null | ||
}); | ||
_this.running = true; | ||
}; | ||
this.stopTimer = function () { | ||
if (!_this.times.length) { | ||
return; | ||
} | ||
_this.times[_this.times.length - 1].stop = performance.now(); | ||
_this.running = false; | ||
}; | ||
this.addTimeIntervalEllapsedCallback = function (timeIntervalEllapsedCallback) { | ||
_this.timeIntervalEllapsedCallbacks.push(timeIntervalEllapsedCallback); | ||
}; | ||
this.addAbsoluteTimeEllapsedCallback = function (absoluteTimeEllapsedCallback) { | ||
_this.absoluteTimeEllapsedCallbacks.push(absoluteTimeEllapsedCallback); | ||
}; | ||
this.addBrowserTabInactiveCallback = function (browserTabInactiveCallback) { | ||
_this.browserTabInactiveCallbacks.push(browserTabInactiveCallback); | ||
}; | ||
this.addBrowserTabActiveCallback = function (browserTabActiveCallback) { | ||
_this.browserTabActiveCallbacks.push(browserTabActiveCallback); | ||
}; | ||
this.getTimeInMilliseconds = function () { | ||
return _this.times.reduce(function (acc, current) { | ||
if (current.stop) { | ||
acc = acc + (current.stop - current.start); | ||
} | ||
else { | ||
acc = acc + (performance.now() - current.start); | ||
} | ||
return acc; | ||
}, 0); | ||
}; | ||
this.isRunning = function () { | ||
return _this.running; | ||
}; | ||
this.reset = function () { | ||
_this.times = []; | ||
}; | ||
this.destroy = function () { | ||
_this.unregisterEventListeners(); | ||
if (_this.checkCallbackIntervalId) { | ||
window.clearInterval(_this.checkCallbackIntervalId); | ||
} | ||
}; | ||
this.running = false; | ||
this.times = []; | ||
this.idle = false; | ||
this.currentIdleTimeMs = 0; | ||
this.marks = {}; | ||
this.measures = {}; | ||
this.browserTabActiveCallbacks = browserTabActiveCallbacks || []; | ||
this.browserTabInactiveCallbacks = browserTabInactiveCallbacks || []; | ||
this.checkCallbacksIntervalMs = checkCallbacksIntervalMs || 100; | ||
this.idleTimeoutMs = idleTimeoutMs || 3000; // 3s | ||
this.timeIntervalEllapsedCallbacks = timeIntervalEllapsedCallbacks || []; | ||
this.absoluteTimeEllapsedCallbacks = absoluteTimeEllapsedCallbacks || []; | ||
this.registerEventListeners(); | ||
} | ||
BrowserInteractionTime.prototype.mark = function (key) { | ||
if (!this.marks[key]) { | ||
this.marks[key] = []; | ||
} | ||
this.marks[key].push({ time: this.getTimeInMilliseconds() }); | ||
}; | ||
BrowserInteractionTime.prototype.getMarks = function (name) { | ||
if (this.marks[name].length < 1) { | ||
return; | ||
} | ||
return this.marks[name]; | ||
}; | ||
BrowserInteractionTime.prototype.measure = function (name, startMarkName, endMarkName) { | ||
var startMarks = this.marks[startMarkName]; | ||
var startMark = startMarks[startMarks.length - 1]; | ||
var endMarks = this.marks[endMarkName]; | ||
var endMark = endMarks[endMarks.length - 1]; | ||
if (!this.measures[name]) { | ||
this.measures[name] = []; | ||
} | ||
this.measures[name].push({ | ||
name: name, | ||
startTime: startMark.time, | ||
duration: endMark.time - startMark.time | ||
}); | ||
}; | ||
BrowserInteractionTime.prototype.getMeasures = function (name) { | ||
if (!this.measures[name] && this.measures[name].length < 1) { | ||
return; | ||
} | ||
return this.measures[name]; | ||
}; | ||
return BrowserInteractionTime; | ||
}()); | ||
var windowIdleEvents = ['scroll', 'resize']; | ||
var documentIdleEvents = [ | ||
'wheel', | ||
'keydown', | ||
'keyup', | ||
'mousedown', | ||
'mousemove', | ||
'touchstart', | ||
'touchmove', | ||
'click', | ||
'contextmenu' | ||
]; | ||
var BrowserInteractionTime = /** @class */ (function () { | ||
function BrowserInteractionTime(_a) { | ||
var timeIntervalEllapsedCallbacks = _a.timeIntervalEllapsedCallbacks, absoluteTimeEllapsedCallbacks = _a.absoluteTimeEllapsedCallbacks, checkCallbacksIntervalMs = _a.checkCallbacksIntervalMs, browserTabInactiveCallbacks = _a.browserTabInactiveCallbacks, browserTabActiveCallbacks = _a.browserTabActiveCallbacks, idleTimeoutMs = _a.idleTimeoutMs; | ||
var _this = this; | ||
this.onBrowserTabInactive = function (event) { | ||
// if running pause timer | ||
if (_this.isRunning()) { | ||
_this.stopTimer(); | ||
} | ||
_this.browserTabInactiveCallbacks.forEach(function (fn) { | ||
return fn(_this.getTimeInMilliseconds()); | ||
}); | ||
}; | ||
this.onBrowserTabActive = function (event) { | ||
// if not running start timer | ||
if (!_this.isRunning()) { | ||
_this.startTimer(); | ||
} | ||
_this.browserTabActiveCallbacks.forEach(function (fn) { | ||
return fn(_this.getTimeInMilliseconds()); | ||
}); | ||
}; | ||
this.onTimePassed = function () { | ||
// check all callbacks time and if passed execute callback | ||
_this.absoluteTimeEllapsedCallbacks.forEach(function (_a, index) { | ||
var callback = _a.callback, pending = _a.pending, timeInMilliseconds = _a.timeInMilliseconds; | ||
if (pending && timeInMilliseconds <= _this.getTimeInMilliseconds()) { | ||
callback(_this.getTimeInMilliseconds()); | ||
_this.absoluteTimeEllapsedCallbacks[index].pending = false; | ||
} | ||
}); | ||
_this.timeIntervalEllapsedCallbacks.forEach(function (_a, index) { | ||
var callback = _a.callback, timeInMilliseconds = _a.timeInMilliseconds, multiplier = _a.multiplier; | ||
if (timeInMilliseconds <= _this.getTimeInMilliseconds()) { | ||
callback(_this.getTimeInMilliseconds()); | ||
_this.timeIntervalEllapsedCallbacks[index].timeInMilliseconds = multiplier(timeInMilliseconds); | ||
} | ||
}); | ||
if (_this.currentIdleTimeMs >= _this.idleTimeoutMs && _this.isRunning()) { | ||
_this.idle = true; | ||
_this.stopTimer(); | ||
} | ||
else { | ||
_this.currentIdleTimeMs += _this.checkCallbacksIntervalMs; | ||
} | ||
}; | ||
this.resetIdleTime = function () { | ||
if (_this.idle) { | ||
_this.startTimer(); | ||
} | ||
_this.idle = false; | ||
_this.currentIdleTimeMs = 0; | ||
}; | ||
this.registerEventListeners = function () { | ||
var documentListenerOptions = { passive: true }; | ||
var windowListenerOptions = __assign({}, documentListenerOptions, { capture: true }); | ||
window.addEventListener('blur', _this.onBrowserTabInactive, windowListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, windowListenerOptions); | ||
var throttleResetIdleTime = throttle_1(_this.resetIdleTime, 2000, { | ||
leading: true, | ||
trailing: false | ||
}); | ||
windowIdleEvents.forEach(function (event) { | ||
window.addEventListener(event, throttleResetIdleTime, windowListenerOptions); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.addEventListener(event, throttleResetIdleTime, documentListenerOptions); | ||
}); | ||
}; | ||
this.unregisterEventListeners = function () { | ||
window.removeEventListener('blur', _this.onBrowserTabInactive); | ||
window.removeEventListener('focus', _this.onBrowserTabActive); | ||
windowIdleEvents.forEach(function (event) { | ||
return window.removeEventListener(event, _this.resetIdleTime); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.removeEventListener(event, _this.resetIdleTime); | ||
}); | ||
}; | ||
this.checkCallbacksOnInterval = function () { | ||
_this.checkCallbackIntervalId = window.setInterval(function () { | ||
_this.onTimePassed(); | ||
}, _this.checkCallbacksIntervalMs); | ||
}; | ||
this.startTimer = function () { | ||
if (!_this.checkCallbackIntervalId) { | ||
_this.checkCallbacksOnInterval(); | ||
} | ||
var last = _this.times[_this.times.length - 1]; | ||
if (last && last.stop === null) { | ||
return; | ||
} | ||
_this.times.push({ | ||
start: performance.now(), | ||
stop: null | ||
}); | ||
_this.running = true; | ||
}; | ||
this.stopTimer = function () { | ||
if (!_this.times.length) { | ||
return; | ||
} | ||
_this.times[_this.times.length - 1].stop = performance.now(); | ||
_this.running = false; | ||
}; | ||
this.addTimeIntervalEllapsedCallback = function (timeIntervalEllapsedCallback) { | ||
_this.timeIntervalEllapsedCallbacks.push(timeIntervalEllapsedCallback); | ||
}; | ||
this.addAbsoluteTimeEllapsedCallback = function (absoluteTimeEllapsedCallback) { | ||
_this.absoluteTimeEllapsedCallbacks.push(absoluteTimeEllapsedCallback); | ||
}; | ||
this.addBrowserTabInactiveCallback = function (browserTabInactiveCallback) { | ||
_this.browserTabInactiveCallbacks.push(browserTabInactiveCallback); | ||
}; | ||
this.addBrowserTabActiveCallback = function (browserTabActiveCallback) { | ||
_this.browserTabActiveCallbacks.push(browserTabActiveCallback); | ||
}; | ||
this.getTimeInMilliseconds = function () { | ||
return _this.times.reduce(function (acc, current) { | ||
if (current.stop) { | ||
acc = acc + (current.stop - current.start); | ||
} | ||
else { | ||
acc = acc + (performance.now() - current.start); | ||
} | ||
return acc; | ||
}, 0); | ||
}; | ||
this.isRunning = function () { | ||
return _this.running; | ||
}; | ||
this.reset = function () { | ||
_this.times = []; | ||
}; | ||
this.destroy = function () { | ||
_this.unregisterEventListeners(); | ||
if (_this.checkCallbackIntervalId) { | ||
window.clearInterval(_this.checkCallbackIntervalId); | ||
} | ||
}; | ||
this.running = false; | ||
this.times = []; | ||
this.idle = false; | ||
this.currentIdleTimeMs = 0; | ||
this.marks = {}; | ||
this.measures = {}; | ||
this.browserTabActiveCallbacks = browserTabActiveCallbacks || []; | ||
this.browserTabInactiveCallbacks = browserTabInactiveCallbacks || []; | ||
this.checkCallbacksIntervalMs = checkCallbacksIntervalMs || 100; | ||
this.idleTimeoutMs = idleTimeoutMs || 3000; // 3s | ||
this.timeIntervalEllapsedCallbacks = timeIntervalEllapsedCallbacks || []; | ||
this.absoluteTimeEllapsedCallbacks = absoluteTimeEllapsedCallbacks || []; | ||
this.registerEventListeners(); | ||
} | ||
BrowserInteractionTime.prototype.mark = function (key) { | ||
if (!this.marks[key]) { | ||
this.marks[key] = []; | ||
} | ||
this.marks[key].push({ time: this.getTimeInMilliseconds() }); | ||
}; | ||
BrowserInteractionTime.prototype.getMarks = function (name) { | ||
if (this.marks[name].length < 1) { | ||
return; | ||
} | ||
return this.marks[name]; | ||
}; | ||
BrowserInteractionTime.prototype.measure = function (name, startMarkName, endMarkName) { | ||
var startMarks = this.marks[startMarkName]; | ||
var startMark = startMarks[startMarks.length - 1]; | ||
var endMarks = this.marks[endMarkName]; | ||
var endMark = endMarks[endMarks.length - 1]; | ||
if (!this.measures[name]) { | ||
this.measures[name] = []; | ||
} | ||
this.measures[name].push({ | ||
name: name, | ||
startTime: startMark.time, | ||
duration: endMark.time - startMark.time | ||
}); | ||
}; | ||
BrowserInteractionTime.prototype.getMeasures = function (name) { | ||
if (!this.measures[name] && this.measures[name].length < 1) { | ||
return; | ||
} | ||
return this.measures[name]; | ||
}; | ||
return BrowserInteractionTime; | ||
}()); | ||
return BrowserInteractionTime; | ||
return BrowserInteractionTime; | ||
})); | ||
//# sourceMappingURL=browser-interaction-time.umd.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,6 +17,9 @@ var throttle_1 = require("lodash/throttle"); | ||
var documentIdleEvents = [ | ||
'wheel', | ||
'keydown', | ||
'keyup', | ||
'mousedown', | ||
'mousemove', | ||
'keyup', | ||
'keydown', | ||
'touchstart', | ||
'touchmove', | ||
'click', | ||
@@ -68,5 +82,6 @@ 'contextmenu' | ||
this.registerEventListeners = function () { | ||
var eventListenerOptions = { passive: true }; | ||
window.addEventListener('blur', _this.onBrowserTabInactive, eventListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, eventListenerOptions); | ||
var documentListenerOptions = { passive: true }; | ||
var windowListenerOptions = __assign({}, documentListenerOptions, { capture: true }); | ||
window.addEventListener('blur', _this.onBrowserTabInactive, windowListenerOptions); | ||
window.addEventListener('focus', _this.onBrowserTabActive, windowListenerOptions); | ||
var throttleResetIdleTime = throttle_1.default(_this.resetIdleTime, 2000, { | ||
@@ -77,6 +92,6 @@ leading: true, | ||
windowIdleEvents.forEach(function (event) { | ||
window.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
window.addEventListener(event, throttleResetIdleTime, windowListenerOptions); | ||
}); | ||
documentIdleEvents.forEach(function (event) { | ||
return document.addEventListener(event, throttleResetIdleTime, eventListenerOptions); | ||
return document.addEventListener(event, throttleResetIdleTime, documentListenerOptions); | ||
}); | ||
@@ -83,0 +98,0 @@ }; |
{ | ||
"name": "browser-interaction-time", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "", | ||
@@ -90,17 +90,17 @@ "keywords": [], | ||
"devDependencies": { | ||
"@commitlint/cli": "^7.1.2", | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"@commitlint/config-conventional": "^8.0.0", | ||
"@commitlint/cli": "^8.0.0", | ||
"@types/jest": "^24.0.0", | ||
"@types/lodash": "^4.14.121", | ||
"@types/node": "^11.13.0", | ||
"@types/node": "^12.6.2", | ||
"colors": "^1.3.2", | ||
"commitizen": "^3.0.0", | ||
"commitizen": "^3.1.1", | ||
"coveralls": "^3.0.2", | ||
"cross-env": "^5.2.0", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"husky": "^1.0.1", | ||
"cz-conventional-changelog": "^3.0.2", | ||
"husky": "^3.0.0", | ||
"jest": "^24.4.0", | ||
"jest-config": "^24.4.0", | ||
"jest-extended": "^0.11.0", | ||
"lint-staged": "^8.0.0", | ||
"jest-extended": "^0.11.1", | ||
"lint-staged": "^9.1.0", | ||
"lodash.camelcase": "^4.3.0", | ||
@@ -118,9 +118,9 @@ "lodash.debounce": "^4.0.8", | ||
"rollup-plugin-sourcemaps": "^0.4.2", | ||
"rollup-plugin-typescript2": "^0.20.1", | ||
"semantic-release": "^15.13.2", | ||
"testcafe": "^1.0.0", | ||
"rollup-plugin-typescript2": "^0.22.0", | ||
"semantic-release": "^15.13.14", | ||
"testcafe": "^1.3.2", | ||
"testcafe-browser-provider-browserstack": "^1.5.0", | ||
"testcafe-live": "^0.1.4", | ||
"travis-deploy-once": "^5.0.9", | ||
"ts-jest": "^24.0.0", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.0.1", | ||
@@ -127,0 +127,0 @@ "tslint": "^5.11.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
162816
1691
1