Re: [WICG/webcomponents] Need a callback for when children changed or parser finished parsing children (#809)

> ```js
> class MyElement extends HTMLElement {
>     childrenParsed(bool didFinish) { // didFinish will be false when this is getting called due to parser yields.
> }
> ```

At first I was thinking this is a nice addition, but the case where `childrenParser(false)` is identically the same as just calling `requestAnimationFrame()`, right? So I'm not sure what you gain. With something like `finishedParsingChildren()`, it would be guaranteed to only be called once, and only after all children are done being parsed, which is the thing that isn't easily possible today.



> An attempt at a concrete API for a Promise could be one in `elementinternals`:
> 
> ```js
> class MyElement extends HTMLElement {
>   #internals = this.attachInternals()
>   async connectedCallback() {
>     await this.#internals.fullyParsed();
>   }
> }
> ```

I like this approach also, perhaps better than the callback-that-gets-called-once approach! It achieves the same goal, and it's more clear that this promise just gets resolved once.

> @dgp1130 I'm not sure that's a valid point. Say there was a multi-second network hiccup in the middle of a large custom element, why shouldn't it be rendered including all the children it has thus far, similar to built-in elements? I think "children changed", possibly rate-limited with "request animation frame" depending on what you need to be doing, is still the right approach there.

You might be correct that "rendering what you have" is the right thing to do. But it might also **not** be the case. As pointed out here:

> Some layouts, such as flexbox, shift around as children parse-in, so it can be useful to eg hide elements during that phase.

Another example I can think of (raised by @rniwa [here](https://github.com/whatwg/dom/issues/831#issuecomment-608131292)) is declarative shadow dom. It's similar to the issue raised [above](https://github.com/WICG/webcomponents/issues/809#issuecomment-1460319527) about hydration. You want to be able to wait until you have parsed all of your child content, including declarative shadow roots, before activating the component.

Generally it seems like there are some real use cases for knowing when parsing is finished for your element and children. People seem willing to go to considerable lengths (see comments above) to achieve this behavior, and in many cases they likely have **bugs** in their attempts to do this, because it's tough to do correctly or reason about given the parser's yielding behavior. @annevk I therefore don't buy the argument that the best thing is to discourage this common pattern, and force people to create hacks that don't work correctly in all cases.

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

Message ID: <WICG/webcomponents/issues/809/1460808889@github.com>

Received on Wednesday, 8 March 2023 20:16:11 UTC