-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ondestroy in custom elements #1152
Comments
A like in the Context API tutorial, I used slotted custom element childs to set the main object. Also like a But while a child is removed from the dom, I would love to unset the property related to the child presence. Removing a Marker from the map for example. But without the |
Is there any workaround to get the info if a svelte custom element is disconnected? In case, where a custom element is dynamically created via some third party orchestrating app (e.g. as a reusable component on a cms driven view) it could easily lead to memory leaks. For example, in |
Can the reinsertion issue be manifested in a test? When does it occur? |
For anyone looking for a temporary solution to this problem, this is what I'm using: class SubElement extends SvelteElement { // extend the Svelte custom element here
disconnectedCallback() {
this.$destroy()
}
} This definitely seems like a tricky issue. Intuitively, as a web developer, I would expect that: document.body.appendChild(element)
document.body.removeChild(element) ... should result in no memory leaks. There are no more references to the My preference is to have the entire Svelte component created in |
@nolanlawson do you have a working example? I don't get your code... |
@nolanlawson hi, may I know how to apply this workaround? |
You have to add
in the SvelteElement class. A related thread here |
Update to @yannkost's comment. You don't actually have to modify Svelte internals. Doing this will be good enough import { SvelteElement } from "svelte/internal";
SvelteElement.prototype.disconnectedCallback = function () {
this.$destroy();
}; SO LONG AS that code runs before your custom elements are So you may need to tuck all of your web component imports behind a single /cc @nolanlawson |
@nolanlawson |
Marking as fixed by #4522 |
Trying to wrap my head round this while looking at #1117. I don't think
ondestroy
should be called for a custom element when it's detached (withdisconnectedCallback
) because it means something slightly different in custom element land — it can be a temporary thing (the element can be reinserted), whereasdestroy
is permanent. In other words,destroy
should still be called manually for custom elements inserted with e.g.document.body.innerHTML = '<x-app/>'
.But when we have a situation like this...
...and
x-thing
is a Svelte custom element, we probably do want to callondestroy
, since there's basically no sensible way to do that programmatically.Which means we need a reliable way to distinguish between Svelte custom elements and non-Svelte custom elements (where
destroy
could potentially mean something different, meaning duck typing isn't necessarily reliable).The text was updated successfully, but these errors were encountered: