@ocap/util
Version:
utils shared across multiple forge js libs, works in both node.js and browser
37 lines (36 loc) • 948 B
JavaScript
import EventEmitter from 'events';
export class Ready extends EventEmitter {
constructor() {
super();
this.ready = false;
this.readyCallbacks = [];
this.readyMarks = {};
}
markReady(mark) {
if (this.ready) {
return;
}
if (mark === undefined) {
this.ready = true;
this.readyCallbacks.forEach((x) => x());
this.emit('ready');
}
else {
// console.log('Ready.markReady', this.name, mark);
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);
}
}
}