function-performer
Version:
Performer providing API for debounce, throttle, deduplication and limiting functions
40 lines (39 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Limit", {
enumerable: true,
get: function() {
return Limit;
}
});
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Limit {
execute(config, func, ...args) {
const numberOfCalls = this._calls.get(func) ?? 0;
const maxNumberOfCalls = config?.max ?? this._max;
this._calls.set(func, numberOfCalls + 1);
if (numberOfCalls < maxNumberOfCalls) {
return func(...args);
}
}
constructor(config){
_define_property(this, "_calls", void 0);
_define_property(this, "_max", void 0);
this._calls = new Map();
this._max = config?.max ?? Infinity;
}
}