jprichardson/electron-window

Preventing the removal of window on close

Opened this issue · 1 comments

tterb commented

Hey jprichardson,
I've been utilizing your package on an app that I've been developing, though I've run into an issue when trying to add a dialog that prompts the user if there are unsaved changes in a window.
I've managed to get everything working properly by adding the logic to close event listener, though it appears that at that point electron-window has already removed the window reference.

Is there a way to preserve the reference until after the close event is emitted or restore the window reference without creating a totally new window?

The problem is https://github.com/jprichardson/electron-window/blob/master/lib/main.js#L49

The code should be using the closed event, which per docs:

Event: 'closed'
Emitted when the window is closed. After you have received this event you should remove the reference to the window and avoid using it any more.

Recommend changing close to closed, to avoid garbage collecting the window before it has decisively actually closed (especially in this case where a user like @tterb may want to prevent the default behaviour (event.preventDefault is allowed on the close event to programmatically prevent a window from closing)).

Workaround for now is to use beforeunload in the window per docs:

Usually you would want to use the beforeunload handler to decide whether the window should be closed, which will also be called when the window is reloaded. In Electron, returning any value other than undefined would cancel the close.