In my (Vaadin 24.4.10) application some custom Vaadin Components implement onDetach
and release/close ressources that have been initialized in the constructor of those components.
This is to release memory early, e.g. when the user switches from one form to another form.
After onDetach
everything that happened in the constructor is rolled back and these components have only a very small memory usage.
Unfortunately there is a situation where this leads to a problem:
When Vaadin starts resynchronizing UI by client’s request, then ServerRpcHandler.handleRpc
calls ui.getInternals().getStateTree().prepareForResync()
. And this leads to a call of onDetach
and after that to a call of onAttach
.
So when a resynchronizing happens, then my custom components have cleaned up themselves and are more or less unusable without re-calling the constructor.
One approach would be to move everything from the constructor to the onAttach
method.
Another approach could be that the onDetach
implementation recognizes the resync and does not clean up everything (if anything at all).
I think that this approach is a lot less work and risk in my case than moving everything from the constructor to the onAttach
method, so I’d like to follow this approach.
The question / problem is: How can the onDetach
method recognize that it’s in the resync? StateNode.prepareForResync()
does not set a flag before it fires the detach listeners.
(A (temporary) workaround would be that I search in the StackTrace for the method prepareForResynch
. This is unsafe because in the future the method could be renamed. So I’m looking for a future-safe way of recognizing whether resynchronization is active in the onDetach
call.)
3