Reading through the electron js docs at the moment and on the IPC page, pattern 1(https://www.electronjs.org/docs/latest/tutorial/ipc#pattern-1-renderer-to-main-one-way) they gave a code example. Inside main.js they put the event listener inside the createWindow()
function and I don't see any reason why. I tried putting it outside and it just works fine. And considerations on that? Am I missing something?
It does not need to be included in the createWindow()
function scope. That is just the way the person wrote it in the Doc’s. In an attempt to keep it simple, I suspect they did not want to add an unnecessary number of files to the example. Instead, they sacrificed code separation for simplicity in understanding.
As your code base matures, code such as that shown in Pattern 1: Render to main (one-way) could be refactored by moving the ipcMain.on('set-title', () => { ... })
function into a script that handles the setting / rendering of the applications window frame.
Remember, most if not all examples given in the Electron Doc’s and API's sections could be refactored as your application grows, allowing for better separation of concerns and code readability.