Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnloadObserver should always send an event before unloading #170

Closed
jflamy opened this issue Jun 22, 2020 · 8 comments · Fixed by #177
Closed

UnloadObserver should always send an event before unloading #170

jflamy opened this issue Jun 22, 2020 · 8 comments · Fixed by #177
Assignees
Labels
enhancement New feature or request
Projects

Comments

@jflamy
Copy link

jflamy commented Jun 22, 2020

I would like to use the unload observer to unregister components (my application uses an event bus, and I would like my components to be removed right away on close instead of waiting for the detach event).
It would be nice to be able to register/unregister components to be notified when navigating. Maybe just a coding example would do.

@vaadin-miki
Copy link
Owner

UnloadObserver is intended to provide a callback to the server when the user closes the tab (or browser window) or manually navigates away. Navigation lifecycle is a standard feature of Vaadin, as described here: https://vaadin.com/docs/flow/routing/tutorial-routing-lifecycle.html - so I would expect what you are trying to achieve can be done through that.

I admit I have not tested the component well enough, so it is very well possible that the event triggers when navigating to somewhere within the app or in some other weird cases. I will investigate and update this ticket accordingly.

@jflamy
Copy link
Author

jflamy commented Jun 22, 2020 via email

@vaadin-miki
Copy link
Owner

Alrighty. I think that with the fix of #173 it should be possible to get the parent component in which UnloadObserver resides. In that case you should be able to detach components manually e.g. by removing them from the layout or taking them from some other globally available place (not sure if there is a better approach, and I think having each component listen to unload events is in general a bad idea).

I will try to come up with changes to the demo app to support multiple views (see #174) and at the same time I can have the unload trigger e.g. log the components in the current view.

@jflamy
Copy link
Author

jflamy commented Jun 23, 2020 via email

@vaadin-miki
Copy link
Owner

While I am working on #173, I had an idea. Currently the only config for UnloadObserver is to either:

  • display a confirmation window and register an attempt (both successful or not)
  • do nothing

I think it would make sense to inform server also when the unload happened. I think this is what would help you achieve your goal of unloading components when the app is closed - no need for query here. Is my thinking correct?

@jflamy
Copy link
Author

jflamy commented Jun 24, 2020 via email

@vaadin-miki vaadin-miki changed the title Unload observer for multiple components UnloadObserver should always send an event before unloading Jun 26, 2020
@vaadin-miki vaadin-miki added the enhancement New feature or request label Jun 26, 2020
@vaadin-miki vaadin-miki added this to To do in Misc via automation Jun 26, 2020
@vaadin-miki vaadin-miki self-assigned this Jun 26, 2020
@vaadin-miki
Copy link
Owner

vaadin-miki commented Jun 26, 2020

Alright. I did some testing. It is impossible to contact the server on unload event, as it is not available. However, it is possible to call server without displaying a prompt to the user. So, UnloadObserver will do this by default, and the event now has a flag isBecauseOfQuerying which will be true when the user was shown prompt, and false when the event just happened and the page has been unloaded (unless there has been something else preventing it from unloading, of course).

This has a drawback: it is impossible to find out if the page has been closed because user has agreed to leave it. I think though it is acceptable as is. The drawback can be fixed by #176 if there is anyone willing to work on it, I am not skilled in JS enough to fix it.

Anyway, the above behaviour should fit your use case @jflamy - when the event is called the UnloadObserver is attached to some parent, so you can probably figure out everything you need. This will be included in version 0.7.1 which I hope to release early next week. Of course if you want, you could build the add-on yourself from development branch once this is merged, and then test it. It would be much appreciated :)

@jflamy
Copy link
Author

jflamy commented Jun 26, 2020 via email

vaadin-miki added a commit that referenced this issue Jun 26, 2020
@vaadin-miki vaadin-miki mentioned this issue Jun 26, 2020
Misc automation moved this from To do to Done Jun 26, 2020
vaadin-miki added a commit that referenced this issue Jun 26, 2020
UnloadObserver now broadcasts an event even when not querying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Misc
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants