-
Notifications
You must be signed in to change notification settings - Fork 7
Description
This DOM issue is giving me a hard time, because I'm trying to detect attributes on custom element construction so that I can detect side effects of custom attributes, and I can only get it to work with a setTimeout(..., 0) (has to be a macrotask).
So I have something like
constructor() {
super()
setTimeout( () => this.doSomethingWithPreExistingAttributes(), 0 )
}I see that in custom-attributes, the MutationObserver allows us to detect when an element finally has attributes.
I can probably replace my setTimeout with my own MutationObserver, but maybe it'd be nice to have a Promise-based API for detecting custom attributes instances, which is what I'm interested in, so that not only are the attributes existing, but the custom attributes classes surely instantiated.
API might look like
customAttributes.whenReady(element).then(...)Inside a Custom Element constructor, this could be like
customAttributes.whenReady(this).then(...)so at this point I know
- Attributes exist (thanks, custom-attributes already has a MutationObserver, no need to make another one).
- Custom Attributes are already instantiated and possible initial sideeffects are already completed. Currently it may be possible to register a MutationObserver on an element before the one in custom-attributes, which will mean we will know when attributes are ready, but will have to still write a deferral hack to ensure that Custom Attributes are constructed.
What do you think?