You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[reactive-element] Utility for tracking update complete of children #3538
How hard would it be to add a lifecycle method or promise that lets us know:
when all defined LitElements that are descendants of this LitElement have been rendered and
are also in an updateComplete state?
Alternatives and Workarounds
Running into this issue has made me more aware that LitElements are basically async which is why statefulness is more-or-less required. However, standard HTML is rendered and ready (from a code standpoint) after it has been appended to the DOM, and offering this promise would allow interoperability with that modality of working with elements and DOM trees.
Utilizing getUpdateComplete, If I wanted a general solution, where I don't need to explicitly name child nodes (some of which may or may not exist based on render state), would I just need to crawl all the leaf LitElement nodes and wrap their updateComplete in a Promise.all?
Very interesting... so I was working on the following in order to get all descendants and stop at LitElements:
functiongetDescendants(parent,customElementClass){constdescendants=[];// Use a depth-first search (DFS) algorithm to traverse the DOM treeconsttraverse=node=>{if(nodeinstanceofcustomElementClass){descendants.push(node);return;// Stop the traversal when a custom element node is encountered}for(leti=0;i<node.childNodes.length;i++){traverse(node.childNodes[i]);}}traverse(parent);returndescendants;}
true if the host and every ReactiveElement child is updated
false if one of those is not yet updated
so let's
first get the host's state, then after,
get the list of children which are ReactiveElements.
We duck type by checking for the presence of updateComplete. choose a different way to filter this list! We must filter this list so that only a list of updateComplete promises is returned, for the next step. then,
we resolve every promise in the list and return true if they are all truthy, and
functiongetDuckTypedDescendants(parent,qualifyingPropertyName){constdescendants=[];// Use a depth-first search (DFS) algorithm to traverse the DOM treeconsttraverse=node=>{if(node.hasOwnProperty(qualifyingPropertyName)){descendants.push(node);return;// Stop the traversal when a custom element node is encountered}for(leti=0;i<node.childNodes.length;i++){traverse(node.childNodes[i]);}};traverse(parent);returndescendants;}// ...overridegetUpdateComplete(){constsup=awaitsuper.getUpdateComplete();constkids=awaitPromise.all(Array.from(getDuckTypedDescendants(this,'updateComplete'),node=>node.updateComplete).filter(x=>!!x));returnsup&&kids.every(x=>!!x);}
...under many circumstances the best way to be sure an entire tree of Lit elements has rendered is to await an animation frame after updateComplete on the topmost element. Lit does render asynchronously, but since it uses microtasks for scheduling, you can rely on the tree being rendered within a single frame
My response:
That definitely sounds more performant than walking all the descendants and awaiting their updateComplete promises... if there are multiple levels of custom components, will they all render within one animation frame, or does each level require an animation frame?
All your elements would need to extend this ExtendedLitElement class for use, if you're already using a base extended class like this, you could just add the method to it.
updateComplete is admittedly a bit of a middling API, as it only resolves once the immediate component has updated, and not the full tree below it that may have been invalidated; we've held off implementing a fully correct updateCompleteIncludingDescendants type of API both due to performance concerns and the fact that it could still only be correct assuming only Lit components exclusively are used, absent a widely-adopted community protocol.
I think we could possibly address this with a combination of a mixin and a directive that let's you mark child elements as important for updates. Something like:
changed the title [-]updateComplete doesn't guarantee that descendant defined LitElements have been rendered[/-][+][reactive-element] Utility for tracking update complete of children[/+]on Jan 20, 2025
Hey Team, trying to custom instrument performance Spans for observability on my Lit components. So would really appreciate this becoming available!
Now I'm using for the InstrumentedLitElemen both a MutationObserver and recursive updateComplete checks to ensure spans only end when the entire subtree is rendered. Not sure if that's ideal
Activity
bennypowers commentedon Dec 20, 2022
https://lit.dev/docs/components/lifecycle/#getUpdateComplete
dliebner commentedon Dec 20, 2022
Utilizing
getUpdateComplete, If I wanted a general solution, where I don't need to explicitly name child nodes (some of which may or may not exist based on render state), would I just need to crawl all the leaf LitElement nodes and wrap theirupdateCompletein aPromise.all?bennypowers commentedon Dec 20, 2022
dliebner commentedon Dec 20, 2022
Very interesting... so I was working on the following in order to get all descendants and stop at LitElements:
Could I do:
I don't understand the boolean casting on updateComplete. Why don't you just stop at
Array.from?EDIT: Okay, actually the
filtermakes sense, it removes theundefineds. Now I'm trying to figure out whyeveryis used in the return...bennypowers commentedon Dec 20, 2022
our goal is to return a promise that resolves to
trueif the host and everyReactiveElementchild is updatedfalseif one of those is not yet updatedso let's
ReactiveElements.We duck type by checking for the presence of
updateComplete. choose a different way to filter this list! We must filter this list so that only a list ofupdateCompletepromises is returned, for the next step. then,dliebner commentedon Dec 20, 2022
When would an
updateCompletenot be truthy? Would the promise returned by the function never resolve in this case?bennypowers commentedon Dec 20, 2022
updateComplete is
Promise<boolean>it would not be truthy when another update was triggered during the previous update cycle. See the docs for more info
dliebner commentedon Dec 20, 2022
Ah, I see now. Thanks.
dliebner commentedon Dec 20, 2022
Updated DFS descendant walker to use duck typing:
dliebner commentedon Dec 20, 2022
In the discord, according to @graynorton:
My response:
dliebner commentedon Dec 20, 2022
Just wanted to contribute one last bit of code, optional implementation:
All your elements would need to extend this ExtendedLitElement class for use, if you're already using a base extended class like this, you could just add the method to it.
kevinpschaaf commentedon Jan 11, 2023
updateCompleteis admittedly a bit of a middling API, as it only resolves once the immediate component has updated, and not the full tree below it that may have been invalidated; we've held off implementing a fully correctupdateCompleteIncludingDescendantstype of API both due to performance concerns and the fact that it could still only be correct assuming only Lit components exclusively are used, absent a widely-adopted community protocol.Based on this, we added a bit more nuance over when to use
updateCompletevs. alternatives in the docs in lit/lit.dev#1029 (see https://lit.dev/docs/components/lifecycle/#updatecomplete), since depending on the use case, there are other/better alternatives than that. Hope that helps.justinfagnani commentedon Jan 20, 2025
I think we could possibly address this with a combination of a mixin and a directive that let's you mark child elements as important for updates. Something like:
justinfagnani commentedon Jan 20, 2025
Since
updateCompleteworks as it does by design, I'm going to change the title to request a utility to track child updates as well.[-]updateComplete doesn't guarantee that descendant defined LitElements have been rendered[/-][+][reactive-element] Utility for tracking update complete of children[/+]Angelodaniel commentedon Jun 3, 2025
Hey Team, trying to custom instrument performance Spans for observability on my Lit components. So would really appreciate this becoming available!
Now I'm using for the InstrumentedLitElemen both a MutationObserver and recursive updateComplete checks to ensure spans only end when the entire subtree is rendered. Not sure if that's ideal
https://github.com/Angelodaniel/lit-components-custom-instrumentation
Update README.md