-
Notifications
You must be signed in to change notification settings - Fork 879
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
[labs/context] ContextProvider added after hostConnected does not work with ContextRoot #3251
Comments
What's the use case for adding a ContextProvider lazily? |
@justinfagnani This is the bug I mentioned yesterday, added to your sprint. The solution is hopefully as simple as flipping the
I was experimenting with fetching a JSON file containing values to provide as context. Because the keys aren't known at element construction time, it looped over the JSON keys after the fetch resolved and created |
@kevinpschaaf I wonder if an even better solution here is to make ContextProvider able to listen for a dynamic set of multiple context keys. Then you could create one ContextProvider up front, and adjust what it provides after the fact. |
@nachoab what's your use case for lazy adding a ContextProvider? |
I was experimenting with contexts on a microservices frontend, in which lazily loaded packages could register providers on the app-shell. |
@justinfagnani We should explore that, but I think we should also just fix this, since it's really just a bug. The listeners should be setup before connecting it to the host which might start firing events. The fix is to just flip the |
Which package(s) are affected?
Context (@lit-labs/context)
Description
Attaching a
ContextRoot
is designed to allow aContextProvider
to come into existence after a consumer requests the data.We have a test for this by registering an element that has a
ContextProvider
after the consumer is registered.However, another use case is for a
ContextProvider
controller to be added lazily to an element, after it is already registered/connected. The key difference here is whenhostConnected
runs: When callingaddController
on an already connected element,hostConnected
will be run synchronous to that call; when callingaddController
during an element construction,hostConnected
will be called later, when the element'sconnectedCallback
lifecycle runs.This difference manifests in a bug due to the fact that the
ContextProvider
controller'scontext-request
listener is added afteraddController
runs:lit/packages/labs/context/src/lib/controllers/context-provider.ts
Lines 46 to 54 in 00b8c56
However during the
hostConnected
callback (which might be called synchronously toaddController
as mentioned above), we fire thecontext-provider
that, when used in conjunction withContextRoot
, results in acontext-request
event being fired back to the provider:lit/packages/labs/context/src/lib/controllers/context-provider.ts
Lines 76 to 79 in 00b8c56
As such, the
context-request
event can be missed by theContextProvider
. The solution is hopefully as simple as flipping theaddController()
andattachListeners()
lines.Reproduction
https://lit.dev/playground/#gist=f709d0dc42035197bb2b4fc4ec65449a
Workaround
This workaround makes the above repro work as expectd:
Is this a regression?
No or unsure. This never worked, or I haven't tried before.
Affected versions
@lit-labs/context 0.1.3
Browser/OS/Node environment
Browser: any
The text was updated successfully, but these errors were encountered: