class InitBearer {
EVENTS = {
ON_CHANGE: 'onChange'
};
initObjects = {};
options = {
option1: null,
option2: null
};
constructor(options = null) {
this.options = {...this.options, ...(options || {})};
this.eventDispatcher = new EventDispatcher();
}
set(id, object) {
this.initObjects[id] = object;
}
get(id) {
return this.initObjects[id];
}
isObjectsInited(ids) {
for (const id of ids) {
if (this.initObjects[id] === null) {
return false;
}
}
return true;
}
wait(ids, timeoutMs = 10000) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
setInterval(() => {
if (this.isObjectsInited(ids)) {
resolve(true);
} else if (timeoutMs !== null && Date.now() - startTime > timeoutMs) {
reject(new Error('Timeout waiting for all InitBearer objects to be initialized'));
}
}, 500);
});
}
}