@tldraw/utils
Version:
tldraw infinite canvas SDK (private utilities).
79 lines (78 loc) • 2.95 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var timers_exports = {};
__export(timers_exports, {
Timers: () => Timers
});
module.exports = __toCommonJS(timers_exports);
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)
};
}
}
//# sourceMappingURL=timers.js.map