All files / helpers windowManager.js

100% Statements 56/56
81.08% Branches 30/37
100% Functions 13/13
100% Lines 56/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x         1x 1x 1x 1x 1x       19x 1x   18x 1x   17x 17x     1x 1x   17x       19x 1x   18x 1x   17x   17x   1x 1x   17x       6x 1x     5x 5x 5x     5x 2x 2x 1x 1x       5x 1x 1x 1x   1x 1x   1x     5x 5x 5x   5x       5x       2x 1x   1x       1x 1x   2x 2x     1x       1x 1x            
const MAX_LISTENERS = 15;
class WindowManager {
 
 
  constructor() {
    this.windows = {};
    this.nameReferences = {};
    this.IDMap = {};
    this.loadedListeners = [];
    this.closedListeners = [];
  }
 
  subscribeWindowLoadedListener(fn) {
    if (!fn || typeof fn !== 'function') {
      throw Error(`${fn} is not a valid callback function`);
    }
    if (this.loadedListeners.length >= MAX_LISTENERS) {
      throw Error(`amount of listeners should not more than ${MAX_LISTENERS} `);
    }
    this.loadedListeners.push(fn);
    const listeners = this.loadedListeners;
    // return an unsubcrible function
    function unsubcrible() {
      const index = listeners.indexOf(fn);
      listeners.splice(index, 1);
    }
    return unsubcrible;
  }
 
  subscribeWindowClosedListener(fn) {
    if (!fn || typeof fn !== 'function') {
      throw Error(`${fn} is not a valid callback function`);
    }
    if (this.closedListeners.length >= MAX_LISTENERS) {
      throw Error(`amount of listeners should not more than ${MAX_LISTENERS} `);
    }
    this.closedListeners.push(fn);
    // return an unsubcrible function
    const listeners = this.closedListeners;
    function unsubcrible() {
      const index = listeners.indexOf(fn);
      listeners.splice(index, 1);
    }
    return unsubcrible;
  }
 
  add(window, name = null, onContentloaded) {
    if (!name || typeof name !== 'string') {
      throw new Error('"name" has to be a valid string');
    }
 
    const newID = Symbol(name);
    this.windows[newID] = window;
    this.IDMap[window.id] = newID;
 
 
    window.on('closed', () => {
      delete this.windows[newID];
      for (let i = 0; i < this.closedListeners.length; i += 1) {
        const listener = this.closedListeners[i];
        listener(newID, name);
      }
    });
 
    window.webContents.on('did-finish-load', () => {
      for (let i = 0; i < this.loadedListeners.length; i += 1) {
        const listener = this.loadedListeners[i];
        listener(newID, name);
      }
      Eif (onContentloaded && typeof onContentloaded === 'function') {
        onContentloaded();
      }
      window.show();
    });
 
    Eif (name) {
      this.nameReferences[name] = this.nameReferences[name] || [];
      this.nameReferences[name].push(newID);
    }
    return newID;
  }
 
  get(windowID) {
    return this.windows[windowID] || null;
  }
 
  getByInternalID(internalID) {
    if (this.IDMap[internalID]) {
      return this.windows[this.IDMap[internalID]] || null;
    }
    return null;
  }
 
  getAll(name) {
    const toReturn = [];
    (this.nameReferences[name] || []).forEach(
      (ID) => {
        Eif (this.get(ID)) {
          toReturn.push(this.get(ID));
        }
      });
    return toReturn;
  }
 
  close(windowID) {
    Eif (this.windows[windowID]) {
      this.windows[windowID].close();
    }
  }
}
 
export default new WindowManager();