UNPKG

@tldraw/utils

Version:

tldraw infinite canvas SDK (private utilities).

59 lines (58 loc) 2.05 kB
class Timers { timeouts = /* @__PURE__ */ new Map(); intervals = /* @__PURE__ */ new Map(); rafs = /* @__PURE__ */ new Map(); constructor() { this.setTimeout = this.setTimeout.bind(this); this.setInterval = this.setInterval.bind(this); this.requestAnimationFrame = this.requestAnimationFrame.bind(this); this.dispose = this.dispose.bind(this); } /** @public */ setTimeout(contextId, handler, timeout, ...args) { const id = window.setTimeout(handler, timeout, args); const current = this.timeouts.get(contextId) ?? []; this.timeouts.set(contextId, [...current, id]); return id; } /** @public */ setInterval(contextId, handler, timeout, ...args) { const id = window.setInterval(handler, timeout, args); const current = this.intervals.get(contextId) ?? []; this.intervals.set(contextId, [...current, id]); return id; } /** @public */ requestAnimationFrame(contextId, callback) { const id = window.requestAnimationFrame(callback); const current = this.rafs.get(contextId) ?? []; this.rafs.set(contextId, [...current, id]); return id; } /** @public */ dispose(contextId) { this.timeouts.get(contextId)?.forEach((id) => clearTimeout(id)); this.intervals.get(contextId)?.forEach((id) => clearInterval(id)); this.rafs.get(contextId)?.forEach((id) => cancelAnimationFrame(id)); this.timeouts.delete(contextId); this.intervals.delete(contextId); this.rafs.delete(contextId); } disposeAll() { for (const contextId of this.timeouts.keys()) { this.dispose(contextId); } } forContext(contextId) { return { setTimeout: (handler, timeout, ...args) => this.setTimeout(contextId, handler, timeout, args), setInterval: (handler, timeout, ...args) => this.setInterval(contextId, handler, timeout, args), requestAnimationFrame: (callback) => this.requestAnimationFrame(contextId, callback), dispose: () => this.dispose(contextId) }; } } export { Timers }; //# sourceMappingURL=timers.mjs.map