UNPKG

function-performer

Version:

Performer providing API for debounce, throttle, deduplication and limiting functions

51 lines (50 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Debounce", { enumerable: true, get: function() { return Debounce; } }); 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 Debounce { execute(config, func, ...args) { let call = this._calls.get(func); if (call === undefined) { call = { timeout: null }; this._calls.set(func, call); } if (call.timeout !== null) { clearTimeout(call.timeout); call.timeout = null; } return new Promise((resolve)=>{ call.timeout = setTimeout(()=>{ this._calls.delete(func); resolve(func(...args)); }, config?.interval ?? this._interval); }); } constructor(config){ _define_property(this, "_calls", void 0); _define_property(this, "_interval", void 0); this._calls = new Map(); this._interval = config?.interval ?? 0; } }