Re: [w3c/webcomponents] How to avoid memory leaks when detecting custom elements? (#674)

> What happens when the node is then get inserted back into the document?

Then the node can allocate new memory again, using `whenDefined` again.

> It is the case that DOM methods such as appendChild that insert a node momentarily removes the node before inserting it back into the document.

This is exactly why in my web components I implemented `init` and `deinit` methods in my custom elements. `init` is fired the first time an element is connected. `deinit` is fired _if and only if_ the element was removed _but not reconnected_ in the same synchronous tick. If the tick finishes, and the element is not back in the DOM, then `deinit` is fired in the next microtask (simple implementation [here](https://github.com/trusktr/infamous/blob/8b10dd84c5006aa81edfd720fee7061892ce64b3/src/html/WebComponent.js#L115-L123) and [here](https://github.com/trusktr/infamous/blob/8b10dd84c5006aa81edfd720fee7061892ce64b3/src/html/WebComponent.js#L133-L157)).

> I don't think that's the behavior you want.

> This would mean that if you ever move a node from one place to another, it would reject the promise. I don't think that's the behavior you want.

You're right, hence my `init` and `deinit`. When I clean up is not of concern, rather that I should _be able to cleanup_ if I want to.

> > In this case, if the elements are never defined, then there's something wrong, and I can throw a warning or error after a timeout.
> The problem is that you'd never know that.

But if someone is trying to render a WebGL scene with my elements, and the elements are not register after, say, a minute, there may likely be something wrong. I can _at least_ show a warning in this case.

But that's irrelevant to this argument, because if my elements are unmounted (and my `deinit` methods called) then I'd like to clean up no matter what rather than unexpected logic firing in the future.

Importantly: rather than unexpected logic firing in the future. (and without leaks)

> FWIW, a web page can have a code like `setTimeout(loadCustomElementDefinition(), 1000000 * Math.random())`

This is true, but despite this fact, we all still reserve the right to clean memory up because as a browser dev, you also don't understand every single use case that an HTML/JS dev could have (f.e. mine). Everyone writing C++ has this option. Why can't HTML/JS devs?

> Worse yet, this could happen in response to network latency, errors, etc...

When the library is ready to try again, it can easily call `whenDefined` again. That's no problem.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/674#issuecomment-335635374

Received on Tuesday, 10 October 2017 23:17:17 UTC