context-protocol
Version:
A fully-typed implementation of the context-protocol, with support for subscriptions.
32 lines (31 loc) • 1.63 kB
TypeScript
import type { Context, ContextCallback, ContextType, EventTargetConstructor, ExtractContext } from "./context.ts";
import { ContextRequestEvent, createContext } from "./context.ts";
/** Registers a callback to be invoked whenever data is delivered to the specified consumer and context. */
declare const addContextListener: <T>(
/** The consumer to match. */
consumer: EventTarget,
/** The context key to match. */
context: ExtractContext<T>,
/** The callback to invoke. */
callback: ContextCallback<ContextType<T>>,
/** Whether the callback should subscribe. */
subscribe?: boolean) => void;
/** Removes a callback from being invoked whenever data is delivered to the matching consumer and context. */
declare const removeContextListener: <T>(
/** The consumer to match. */
consumer: EventTarget,
/** The context key to match. */
context: ExtractContext<T>,
/** The callback to release. */
callback: ContextCallback<ContextType<T>>) => void;
/** Delivers data to the specified consumer and context. */
declare 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;
/** Retrieves data from the specified consumer and context. */
declare const getContextValue: <T>(consumer: EventTarget, context: ExtractContext<T>) => ContextType<T> | undefined;
export { type Context, type ContextCallback, type ContextType, type EventTargetConstructor, type ExtractContext, ContextRequestEvent, addContextListener, createContext, getContextValue, removeContextListener, setContextValue, };