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

When elements are removed from the DOM #118

Open
dariomannu opened this issue Feb 17, 2024 · 1 comment
Open

When elements are removed from the DOM #118

dariomannu opened this issue Feb 17, 2024 · 1 comment

Comments

@dariomannu
Copy link

I haven't found this explicitly stated, perhaps it was obvious, but I guess when a mounted DOM element with some observable listeners attached gets unmounted, the observable would just complete?

However, elements can also just be detached from the DOM without being actually destroyed and then reattached again, maybe to a different parent node. In that case completing the observable on unmount might not be the best thing to do? Maybe only complete when the node becomes garbage-collectible, as in, removed from the DOM with no reference retained?

@domfarolino
Copy link
Collaborator

So we shouldn't think of an element as any special kind of event source, really. The same case is if some object that gets garbage collected holds onto a subscriber and occasionally pushes values to it. Once it's garbage collected, unless it decides to manually complete()/error() the Observable, that Observable will just be quiet from there on out.

I don't think the web platform really has a concept of a destructor that runs at the time of garbage collection, to perform some cleanup work. The closest I could find was FinalizationRegistry, which lets you run a clean-up callback at garbage collection time, but it isn't really used on the platform to register the kind of side effects you mentioned above, like completing an Observable returned by an EventTarget. It also seems like it shouldn't be relied upon: https://html.spec.whatwg.org/multipage/webappapis.html#hostenqueuefinalizationregistrycleanupjob:~:text=The%20timing%20and%20occurrence%20of%20cleanup%20work%20is%20implementation%2Ddefined%20in%20the%20JavaScript%20specification.

So I think the answer is: nothing special would happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants