Skip to content

[context] update and connectedCallback ordering causes issues #63

@trusktr

Description

@trusktr

I wrote more details here:

Basically there is no guarantee that connectedCallback of provider and consumer will fire in the expected order, even if elements are pre-defined before any elements are created. The protocol needs to account for this somehow, in a robust way that makes it easy to follow anbd implement for end authors, so the problem is guaranteed to not happen.

Also this is probably one of the reasons people complain about custom elements. When you run into these issues and you're on a time crunch, the next best thing is to just switch backt to React/Vue/Svelte/etc, unless you have a higher-level requirement such as supporting multiple frameworks (f.e. making a UI component library in a library author context, rather than using Custom Elements for app code in an app author context).

In general, I wish there was some sort of guarantee we could place on upgrade order and lifecycle order for Custom Elements, both of which have been the single biggest pain point in writing robust elements.

The simplest solution I can think of for contexts is to use a polling strategy to wait for a context to become available, which as we all know is an undesirable solution:

class MyEl extends HTMLElement {
  // ...

  someContext

  connectedCallback() {
    const interval = setInterval(() => {
      this.someContext = getContext(this, 'some-context')

      if (this.someContext) clearInterval(interval)
    }, 100)
  }
}

We want the context to be available as soon as possible. But then imagine hundreds of elements running a tight interval loop dispatching bubbling composed events while an application is starting up.

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