templates/js/library/init_bearer/init_bearer.js.twig line 1

Open in your IDE?
  1. class InitBearer {
  2.     EVENTS = {
  3.         ON_CHANGE: 'onChange'
  4.     };
  5.     initObjects = {};
  6.     options = {
  7.         option1: null,
  8.         option2: null
  9.     };
  10.     constructor(options = null) {
  11.         this.options = {...this.options, ...(options || {})};
  12.         this.eventDispatcher = new EventDispatcher();
  13.     }
  14.     set(id, object) {
  15.         this.initObjects[id] = object;
  16.     }
  17.     get(id) {
  18.         return this.initObjects[id];
  19.     }
  20.     isObjectsInited(ids) {
  21.         for (const id of ids) {
  22.             if (this.initObjects[id] === null) {
  23.                 return false;
  24.             }
  25.         }
  26.         return true;
  27.     }
  28.     wait(ids, timeoutMs = 10000) {
  29.         return new Promise((resolve, reject) => {
  30.             const startTime = Date.now();
  31.             setInterval(() => {
  32.                 if (this.isObjectsInited(ids)) {
  33.                     resolve(true);
  34.                 } else if (timeoutMs !== null && Date.now() - startTime > timeoutMs) {
  35.                     reject(new Error('Timeout waiting for all InitBearer objects to be initialized'));
  36.                 }
  37.             }, 500);
  38.         });
  39.     }
  40. }