Electron - Why Set Browserwindow Instance To Null On The Close Event
The electron documentation, provides the following code sample to create a new window: const {BrowserWindow} = require('electron'); let win = new BrowserWindow({width: 800, height
Solution 1:
It's not mandatory, but a good coding practice (in every language).
Docs of 'closed' mentions it in a little bit more details:
After you have received this event you should remove the reference to the window and avoid using it any more.
That is, when you destroy an object prefer setting it to an invalid value for avoiding function calls on a flawed/un-complete object.
Consider this example:
const {app, BrowserWindow} = require('electron')
let win = null
app.once('ready', () => {
win = newBrowserWindow()
win.on('closed', () => {
win = null
})
setInterval(() => {
if (win) win.loadURL('http://google.com')
else app.quit()
}, 3000)
app.on('window-all-closed', () => {})
})
The proper 'closed'
callback here helps to avoid future calls on destroyed object.
For electron's BrowserWindow
you can use isDestroyed()
method as well, which potentially makes the use of 'closed'
unnecessary but invalidating objects is a general technique while destroy queries are always up to the API.
Post a Comment for "Electron - Why Set Browserwindow Instance To Null On The Close Event"