Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[context] update and connectedCallback ordering causes issues #63

Open
trusktr opened this issue Aug 3, 2024 · 3 comments
Open

[context] update and connectedCallback ordering causes issues #63

trusktr opened this issue Aug 3, 2024 · 3 comments

Comments

@trusktr
Copy link

trusktr commented Aug 3, 2024

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.

@Westbrook
Copy link
Collaborator

This issue references work that became a context-provider event from the Lit team that looks to support late registered contexts that may be of use in this situation. Their usage can be found here.

@trusktr
Copy link
Author

trusktr commented Aug 13, 2024

Ah, I see. So the concept is basically to add a listener on a node that is known to certainly exist no matter the sub trees, for example document.

Perhaps we should standardize something like context-provider in the community protocol.

@Westbrook
Copy link
Collaborator

Agreed. Maybe we can get @augustjk to share back some of the findings on the Lit side as per #50. If not, it would be great to see an PR with this addition were you to have a little open time, Joe... 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants