A ContextRoot can be used to gather unsatisfied context requests and re-dispatch them when new providers which satisfy matching context keys are available.
Import
import { ContextRoot } from'@lit-labs/context';
Details
This allows providers to be added to a DOM tree, or upgraded, after the consumers.
A provider should inspect the context property of the event to determine if it has a value that can satisfy the request, calling the callback with the requested value if so. If the requested context event contains a truthy subscribe value, then a provider can call the callback multiple times if the value is changed, if this is the case the provider should pass an unsubscribe method to the callback which consumers can invoke to indicate they no longer wish to receive these updates. If no subscribe value is present in the event, then the provider can assume that this is a 'one time' request for the context and can therefore not track the consumer.
A callback which is provided by a context requester and is called with the value satisfying the request. This callback can be called multiple times by context providers as the requested value is changed.
When the host element is connected to the document it will emit a context-request event with its context key. When the context request is satisfied the controller will invoke the callback, if present, and trigger a host update so it can respond to the new value. It will also call the dispose method given by the provider when the host element is disconnected.
This controller simply listens to the context-request event when the host is connected to the DOM and registers the received callbacks against its observable Context implementation.
Contexts are compared with with strict equality. If you want two separate createContext() calls to referer to the same context, then use a key that will by equal under strict equality like a string for Symbol.for():
If you want a context to be unique so that it's guaranteed to not collide with other contexts, use a key that's unique under strict equality, like a Symbol() or object.: