Is there a mechanism to detect when the current view has been closed off or navigated away from? Specifically, how do I know when the user had ‘navigated away’ so I know to stop the background threads associated with the component in the view.
sounds like you’re looking for either the BeforeLeaveEvent or the AfterNavigationEvent. They can be observed by implementing the BeforeLeaveObserver and the AfterNavigationObserver interfaces respectively; take a look at https://vaadin.com/docs/v10/flow/routing/tutorial-routing-lifecycle.html for some more details.
Yeah, there’s no way to get reliable events from a closing browser window or browser tab. I think you will need to handle any unclosed resources in a SessionDestroyListener (add with VaadinService.getCurrent().addSessionDestroyListener(e -> {}) )
I’ve implemented a sessionDestroyListener, which seems to be working, I just need to determine a reasonable heartbeat interval now.
To solve the page refresh problem, I save a reference to the threads based on Session ID, so before creating a new thread I can kill of any existing thread. (It doesn’t work for multiple UIs as it kills the thread for all UIs, but I’ll come back to that )