/** * A Context object defines an optional initial value for a Context, as well as a name identifier for debugging purposes. */ export type Context = { name: string; initialValue?: T; }; /** * An unknown context type */ export type UnknownContext = Context; /** * A helper type which can extract a Context value type from a Context type */ export type ContextType = T extends Context ? Y : never; /** * A function which creates a Context value object */ export declare function createContext(name: string, initialValue?: T): Readonly>; /** * 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. */ export type ContextCallback = (value: ValueType, unsubscribe?: () => void) => void; /** * An event fired by a context requester to signal it desires a named context. * * 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` * function to the callback which requesters can invoke to indicate they no longer wish to receive these updates. */ export declare class ContextEvent extends Event { readonly context: T; readonly callback: ContextCallback>; readonly subscribe?: boolean | undefined; constructor(context: T, callback: ContextCallback>, subscribe?: boolean | undefined); } declare global { interface HTMLElementEventMap { /** * A 'context-request' event can be emitted by any element which desires * a context value to be injected by an external provider. */ 'context-request': ContextEvent; } } export type ContextConsumerSubscription = { unsubscribe: () => void; }; export declare function useContextConsumer(hostElement: T, context: C, onContextReceived: (context: ContextType, unsubscribe?: () => void) => void, subscribe?: boolean): ContextConsumerSubscription; export type ContextProvider = Context> = { emit: (context: ContextType) => void; }; export declare function useContextProvider, T extends HTMLElement = HTMLElement>(hostElement: T, context: C, contextPayload?: ContextType): ContextProvider;