| 6 | ipc: IpcRenderer; |
| 7 | id!: string; |
| 8 | constructor() { |
| 9 | this.emitter = new EventEmitter(); |
| 10 | this.ipc = electron.ipcRenderer; |
| 11 | if (window.__rpcId) { |
| 12 | setTimeout(() => { |
| 13 | this.id = window.__rpcId; |
| 14 | this.ipc.on(this.id, this.ipcListener); |
| 15 | this.emitter.emit('ready'); |
| 16 | }, 0); |
| 17 | } else { |
| 18 | this.ipc.on('init', (ev: IpcRendererEvent, uid: string) => { |
| 19 | // we cache so that if the object |
| 20 | // gets re-instantiated we don't |
| 21 | // wait for a `init` event |
| 22 | window.__rpcId = uid; |
| 23 | this.id = uid; |
| 24 | this.ipc.on(uid, this.ipcListener); |
| 25 | this.emitter.emit('ready'); |
| 26 | }); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | ipcListener = (event: any, {ch, data}: {ch: string; data: any}) => { |
| 31 | this.emitter.emit(ch, data); |