I use a VaadinRequestInterceptor to initialize some objects on request start and to clean up these objects at request end. The objects are needed in my code that updates the UI.
With push enabled I have the problem, that UI updates are pushed after calling the requestEnd method of the interceptor. This is because in VaadinService.requestEnd the interceptors are called before VaadinSession.unlock, which will push the changes to the clients and starts the communication via the DataCommunicator. The DataCommunicator calls my UI code, which is trying to use the objects, which are cleaned at this time.
So there are the following questions:
What is the use case of VaadinRequestInterceptor, if its requestEnd method is called before the request and its UI updates are done?
Is there a way to do a cleanup of objects at the end of a requests after all UI actions are done? I have only found a solution overriding VaadinServlet and VaadinService, which I do not like.
VaadinRequestInterceptor API was added as a part of a preparation for Micrometer integration in the first Vaadin 24 versions.
The support for synchronous UI updates was added back then, but the support for asynchronous UI updates wasn’t and stuck in the PR queue (external contribution, author left the implementation to us, but our priorities changed and we had no capacity to proceed), and still waiting for better times, see feat: Vaadin command interceptor by marcingrzejszczak · Pull Request #17738 · vaadin/flow · GitHub.
To summarise, this API is a semi-official, i.e. present in the core, but not documented anywhere, and not completed for async updates, there is simply no methods yet that can be hooked to make some post actions after them in the VaadinRequestInterceptor. However, there might be other way to do that, I need to check/try.
Thank you for explanation.
The interceptors work almost in the way, I expected but the only problem was, that requestEnd() was called to early in the process. If I add my own code in VaadinService I do the clean up after all that is done in VaadinService#requestEnd and it works.
But if the API is semi-official and not developed I will stay by the solution overriding VaadinServlet and VaadinService.
requestEnd() is indeed invoked too early.
It’s perhaps better to invoke it after session unlock, rather than add a new method.
I cannot say when exactly, but we are going to revisit this topic and finally merge the PR, so stay tuned. The documentation then will be updated to make this API official.