Event before window is finally closed

I would like for windows to be notified before they are closed, so they can safely unsubscribe from event listeners or release resources.
I would also wish for the windows to be able to delay their closing so that they can take the time they need to release their resources.

Today, I’m trying to use the window:beforeunload event, but it does not always offer enough time to complete all the needed operations.

Thanks :slight_smile:

@sebastientromp which operation to don’t have time to finish using the window:beforeunload ? are you using a background controller? Can’t you move the “close” logic to it? Because it is the first to start and the last to close.

The close logic in on the background controller, but the other app windows still are in charge of loading / accessing resources if they need them. I send a message to the window before unloading them (this is my current workaround), but it feels a bit cumbersome to manage all this logic in the app itself.

Why not catch the click on the close button of the window, free the resources, and then continue your exit procedure (close the window immediately, notify the background window, etc.)

The window does not always close in reaction to a user input. The tracker for instance closes once the game ends, so it’s game-event-driven in that case.

You can add a listener to the overwolf.windows.onStateChanged().

The event is being fired for all the declared windows (background, in-game, desktop, etc.).
So you can subscribe to it even from the background controller.

When the window is closed, the event will be triggered. Don’t forget to check the window ID to make sure that the right window triggered the event.

Then, you can free all your resources.
Besides, you will also need to test the window name/id arguments that are passed to the event to see if it is relevant for your window.

The event will be triggered

I am not sure I understand it properly: what you mean is that the background controller should free the resources handled by the other app window?

My situation is this:

  • The background window always knows when a window is closed
  • However resources are sometimes handled by the app window, because in some cases it doesn’t make sense to centralize things in the app (eg event listeners / subscriptions)
  • When the app window is closed, the background has no way today to free the resources that have been reserved by the app window. And I’m not even sure I could hack my way around it, as I would have to modify the framework (Angular) itself.

This is why I would need the app window to be able to close the resources itself, and have the time to do so.

Sorry for the confusion :slight_smile:

Currently, you can’t do that. The right way is to let the background controller handle all this logic. By the way, what resources are we talking about? when you close the app, all the resources should be free anyway.

When I close the app, yes. But when I only close an app window, all its events listeners or topic subscriptions might stay open.

I can’t see any other solution right now. I discussed that with the R&D team, and we don’t think that it’s an issue. Do you know any memory leaks or other problems that cause obstacles? If so, can you please give us some info, steps to reproduce, etc.?

I am closing this issue for now.
You can always ask to reopen it by mentioning me with a @.