UNPKG

@meta2d/core

Version:

@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .

47 lines 1.43 kB
const debounces = new WeakMap(); const throttles = new WeakMap(); export function debounce(fn, delay, params) { let cache = debounces.get(fn); if (cache) { clearTimeout(cache.timer); } else { cache = {}; debounces.set(fn, cache); } return new Promise((resolve, reject) => { cache.timer = setTimeout(async () => { resolve(await fn(params)); debounces.delete(fn); }, delay); }); } export async function throttle(fn, delay, params) { const now = new Date().getTime(); const start = debounces.get(fn); throttles.set(fn, now); if (start && now - start < delay) { return; } return await fn(params); } export class InstanceDebouncer { static instances = new WeakMap(); static debounce(instance, methodName, wait = 300) { const key = Symbol(`${instance.constructor.name}.${methodName}`); if (!this.instances.has(instance)) { this.instances.set(instance, {}); } const instanceData = this.instances.get(instance); return (...args) => { if (instanceData[key]) { clearTimeout(instanceData[key]); } instanceData[key] = setTimeout(() => { instance[methodName](...args); delete instanceData[key]; }, wait); }; } } //# sourceMappingURL=debounce.js.map