Skip to content

Only the first context with a value set can be provided #2

@knowler

Description

@knowler

First of all, I love the shape of this library—it’s exactly what I want for using the Context Protocol in my custom elements.

I did however notice a bug where providers can effectively only support one context (the first one that’s set with setContextValue().

I believe this is because when the event listener is added it’s keeping the context value from the first call rather than from each setContextValue() call, then it bails early when comparing with the event’s context.

const setContextValue = <T>(
/** The provider to match. */
provider: EventTarget,
/** The context key to match. */
context: ExtractContext<T>,
/** The data to provide. */
value: ContextType<T>,
): void => {
const consumers = UPSERT(CONSUMERS_BY_PROVIDER, provider, () => {
provider.addEventListener(
"context-request",
// @ts-expect-error while `EventTarget` listeners cannot be typed
(event: ContextRequestEvent<T>) => {
if (event.context !== context) return

I might get around to a fix.

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