Skip to content

A Promise for running logic after an Element's attributes have been constructed. #18

@trusktr

Description

@trusktr

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

  1. Attributes exist (thanks, custom-attributes already has a MutationObserver, no need to make another one).
  2. 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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions