In event handler, how can I determine the window sending the event? (this is remote.BrowserWindow())


I have a multi-window application in electron. I have a re-entrant routine to handle the event…

var oldwin = application.oldwindow
var application.window = new browserwindow()
    add application.window to list of windows
    application.window.on('closed', function(){
             .remove_window_from list(.......)

sometime later the ‘closed’ event fires for oldwin. BUT inside the event handler
application.window is the newly created window value… it is NOT the window that caused the event.
I need the window that caused the event… how do I find it?

this is because someone can manually close the window (oldwin), asynchronous to the code above.


If you create the closed handler on every window, shouldn’t it handle its own closed event and remove itself from the list?


yes, but IN the handler, who am I?

application.window… = 1 , set handler for 'closed’
application.window = 2 , set handler for ‘closed’.

asynchronously later, closed event occurs, handler is called…

for what window?

application.window=2, actual window that was closed = 1.

i am looking for ‘this’ or ‘self’ IN the handler… so I know what object is being processed by the event.

there is a ‘this’ in the event context, but it is NOT the electron BrowserWindow object…


ok, i fixed this…

  1. I remove the hander so that the intentional close does not fire the closed event window.removeAllListeners(‘closed’)
  2. save the current window object
  3. create the new window object
  4. set the new window objects ;on-ready’ handler
  5. set the new windows ‘closed’ handler… and pass the super object (application), instead of the window.

in the on ready, resize the new window
close the old window
show the new window

if the window gets closed between viewer refreshes, then the close handler removes the super object from the list of super objects.