/** * @license * Copyright 2021 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ import { ContextCallback, ContextRequestEvent, } from '../context-request-event.js'; import type {Context, ContextType} from '../create-context.js'; import type { ReactiveController, ReactiveControllerHost, } from '@lit/reactive-element'; export interface Options> { context: C; callback?: (value: ContextType, dispose?: () => void) => void; subscribe?: boolean; ref?: HTMLElement; } /** * A ReactiveController which adds context consuming behavior to a custom * element by dispatching `context-request` events. * * 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. */ export class ContextConsumer< C extends Context, HostElement extends ReactiveControllerHost & HTMLElement, > implements ReactiveController { protected host: HostElement; protected ref: HTMLElement; private context: C; private callback?: (value: ContextType, dispose?: () => void) => void; private subscribe = false; private provided = false; value?: ContextType = undefined; constructor(host: HostElement, options: Options); /** @deprecated Use new ContextConsumer(host, options) */ constructor( host: HostElement, context: C, callback?: (value: ContextType, dispose?: () => void) => void, subscribe?: boolean, ref?: HTMLElement ); constructor( host: HostElement, contextOrOptions: C | Options, callback?: (value: ContextType, dispose?: () => void) => void, subscribe?: boolean, ref?: HTMLElement ) { this.host = host; // This is a potentially fragile duck-type. It means a context object can't // have a property name context and be used in positional argument form. if ((contextOrOptions as Options).context !== undefined) { const options = contextOrOptions as Options; this.context = options.context; this.callback = options.callback; this.subscribe = options.subscribe ?? false; this.ref = options.ref as HTMLElement ?? host } else { this.context = contextOrOptions as C; this.callback = callback; this.subscribe = subscribe ?? false; this.ref = ref ?? host } this.host.addController?.(this); } private unsubscribe?: () => void; hostConnected(): void { this.dispatchRequest(); } hostDisconnected(): void { if (this.unsubscribe) { this.unsubscribe(); this.unsubscribe = undefined; } } dispatchRequest() { this.ref.dispatchEvent( new ContextRequestEvent( this.context, this.host, this._callback, this.subscribe ) ); } // This function must have stable identity to properly dedupe in ContextRoot // if this element connects multiple times. private _callback: ContextCallback> = (value, unsubscribe) => { // some providers will pass an unsubscribe function indicating they may provide future values if (this.unsubscribe) { // if the unsubscribe function changes this implies we have changed provider if (this.unsubscribe !== unsubscribe) { // cleanup the old provider this.provided = false; this.unsubscribe(); } // if we don't support subscription, immediately unsubscribe if (!this.subscribe) { this.unsubscribe(); } } // store the value so that it can be retrieved from the controller this.value = value; // schedule an update in case this value is used in a template this.host.requestUpdate(); // only invoke callback if we are either expecting updates or have not yet // been provided a value if (!this.provided || this.subscribe) { this.provided = true; if (this.callback) { this.callback(value, unsubscribe); } } this.unsubscribe = unsubscribe; }; }