I'm encountering a recurring problem while developing an Electron application using Typescript. The backend has a set of controllers, with the AppController managing file system interactions and WindowController handling basic window functions. Here's a simplified version of the code:
AppController.ts
export class AppController {
windowCtrl: WindowController
constructor() {
this.windowCtrl = new WindowController(Init Details);
this.windowCtrl.windowEvents.on('window:get-parent-directory',
() => {
console.log('reached this point');
});
}
}
WindowController.ts
export class WindowController {
public windowEvents: EventEmitter;
constructor(Init Details) {
this.windowEvents = new EventEmitter();
ipcMain.on('get-parent', getParentDirectory);
}
getParentDirectory() {
this.windowEvents.emit('window:get-parent-directory');
}
}
The windowEvents event emitter serves as the communication bridge between classes through messages. I've used EventEmitters in Angular before and checked Node documentation to avoid mistakes. Despite my research efforts, I keep facing issues like not initializing it in the constructor. Similar problems arose when working with BrowserWindow instances from Electron. Initializing in the constructor works, but accessing it within member functions returns undefined.
Any suggestions?