@newdash/newdash
Version:
javascript/typescript utility library
34 lines (33 loc) • 1.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const CONSTANTS_1 = require("./CONSTANTS");
const nativeNow_1 = __importDefault(require("./nativeNow"));
/**
* Creates a function that'll short out and invoke `identity` instead
* of `func` when it's called `HOT_COUNT` or more times in `HOT_SPAN`
* milliseconds.
*
* @private
* @param {Function} func The function to restrict.
* @returns {Function} Returns the new shortable function.
*/
function shortOut(func) {
var count = 0, lastCalled = 0;
return function () {
var stamp = (0, nativeNow_1.default)(), remaining = CONSTANTS_1.HOT_SPAN - (stamp - lastCalled);
lastCalled = stamp;
if (remaining > 0) {
if (++count >= CONSTANTS_1.HOT_COUNT) {
return arguments[0];
}
}
else {
count = 0;
}
return func.apply(undefined, arguments);
};
}
exports.default = shortOut;