Fix viewport context loss by changing event listener
When the window.location.href of the viewer tab is modified, but the link is not a valid one, we still see the viewport context disappear, because IModelApp.shutdown() is called beforeunload (i.e regardless of whether a user is redirected, or a tab is closed, or not). Changed the event listener to be a unload event, but this is not intended to be the long-term solution, because it is deprecated
I explored using the visibilitychanged event, but the problem with that is IModelApp will be shutdown when a user switches to a different tab. Checking the window.visiblityState and whether it is visible or hidden is the way to do it, but browsers aren't able to differentiate whether a hidden visibilityState is from the browser closing/refreshing a tab or because it is switched. So in conclusion, there is no good long term solution right now...
Fixes https://github.com/iTwin/itwinjs-backlog/issues/1116
Long term we should still look into switching over to the visibility APIs for cleanup:
- Better resource management when multiple tabs are opened.
- Unreliable
beforeunload- depending on what is done in the handler we might lose user/app state https://www.igvita.com/2015/11/20/dont-lose-user-and-app-state-use-page-visibility/