@vince-fugnitto Thanks for the advice! It didn’t work for me, unfortunately, but I did eventually manage to find a way to do the cleanup so I’ll write down my findings below for anybody who stumbles upon the same problem in the future.
At the moment of writing this, there’s no way to get notified when a workspace is closed (at least to my knowledge) so you can’t do any cleanup when that happens.
The alternative, then, is to do it when application exits. Now, if your cleanup includes deleting temporary files like it did in my case and it has to run on back end, you may be tempted to write a back end service to do this and call it from your front end (for example, in your front end application contribution’s
onWillStop method or in an event listener that listens to
onStateChanged event and calls the cleanup service when application state changes to
closing_window as was suggested in Vince’s comment).
Don’t do this, it won’t work. No asynchronous call can be done from either of these places and your back end service simply won’t get called.
Instead, what you need to do is write a back end application contribution (so, a class that implements
BackendApplicationContribution) and do your clean up in its
Keep in mind that you still can’t have any asynchronous code there, everything has to be synchronous!
When the application is stopped,
BackendApplication will gather all the backend application contributions in its
onStop method and call each contribution’s
onStop, if it’s implemented.
Obviously, “when the application is stopped” means different things depending on whether we’re talking about an electron app or a browser app. An electron app will get closed often, a browser app maybe not as often. If you’re writing a browser app, you may have to consider some other way of doing your cleanup. Then again, depending on your use case and implementation, this may be a non-issue for you.