Skip to content

observing the composed tree synchronously #3

@trusktr

Description

@trusktr

I think this might be a good foundation to start to observe the composed tree synchronously with. What I want to do is run logic synchronously when

  • an element is composed to its composed parent via normal connection (element is appended to parent, parent has no ShadowRoot, the normal parent is the composed parent)
  • an element is composed to a composed parent by becoming the child node of a ShadowRoot (element is appended to a ShadowRoot, the composed parent of the element is the ShadowRoot's host)
  • an element is composed to a composed parent by being slotted to a slot in a ShadowRoot of the element's normal parent, where the slot is a child of an element in the ShadowRoot (the composed parent is the parent node of the slot element)
  • an element is composed to a composed parent by being slotted to a slot in a ShadowRoot of the element's normal parent, where the slot is a child of the ShadowRoot directly (the composed parent is the host of the ShadowRoot)

and similar for all uncomposed reactions.

I have an initial version of this in my CompositionTracker class mixin. It has some limintations: currently it only works with a tree of elements that all extend from the same base class (i.e. it only detects composition with Lume elements, but not composition of a Lume element into any arbitrary non-Lume element because the implementation relies on custom element callbacks which built-in elements don't have, but that's where realdom comes in).

Also after trying a bunch of things, my code in that area has become a bit messy, and some details that should be in CompositionTracker are currently also here in SharedAPI. I need to clean it up and make CompositionTracker full generic, and I'd like for it to detect composition for a custom element regardless if the custom element is composed to a custom element or built-in element.

There's a problem with slotchange events: we do not get a set of mutation records, we can only detect the final nodes that are slotted, and for example we cannot run observations for nodes that are both removed and added within the same tick:

But I believe that with realdom we can synchronously track the set of possible mutations that would lead to a slotchange event being fired, and instead of relying on slotchange we can fire our own handlers at those moments.

With slotchange events, just as with the MutationObserver ordering problem,

there is also the chance that when a node is unslotted from one slot and slotted to another, the slotted reaction may fire before the unslotted reaction, leaving things in a broken state. Because of this, some edge cases in Lume are for sure broken (I haven't added tests for these cases yet, but when Lume goes mainstream, I really don't want anyone to run into such obscure problems).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions