@ocap/util
Version:
utils shared across multiple forge js libs, works in both node.js and browser
33 lines (31 loc) • 696 B
JavaScript
import EventEmitter from "node:events";
//#region src/ready.ts
var Ready = class extends EventEmitter {
constructor() {
super();
this.ready = false;
this.readyCallbacks = [];
this.readyMarks = {};
}
markReady(mark) {
if (this.ready) return;
if (mark === void 0) {
this.ready = true;
this.readyCallbacks.forEach((x) => x());
this.emit("ready");
} else {
this.readyMarks[mark] = true;
this.ready = Object.values(this.readyMarks).every((x) => !!x);
if (this.ready) {
this.readyCallbacks.forEach((cb) => cb());
this.emit("ready");
}
}
}
onReady(cb) {
if (this.ready) cb();
else this.readyCallbacks.push(cb);
}
};
//#endregion
export { Ready };