rvx
Version:
A signal based rendering library
96 lines (95 loc) • 3.31 kB
TypeScript
/**
* A context for implicitly passing values along the call stack.
*
* If you need a global default value, use {@link DefaultContext} instead.
*/
export declare class Context<T> {
#private;
/**
* @param defaultValue The default value. This is used if the {@link current} value is `null` or `undefined`.
*/
constructor(defaultValue: T);
constructor(...defaultValue: T extends undefined ? [] : [T]);
/**
* Get or set the default value.
*
* This is used if the {@link current} value is `null` or `undefined`.
*/
default: T;
/**
* Get the current value for this context.
*/
get current(): T;
/**
* Run a function while injecting the specified value for this context.
*
* See {@link Inject `<Inject>`} when using JSX.
*
* @param value The value to inject.
* @param fn The function to run.
* @param args The function arguments.
* @returns The function's return value.
*/
inject<F extends (...args: any) => any>(value: T | null | undefined, fn: F, ...args: Parameters<F>): ReturnType<F>;
/**
* Shorthand for creating a context-value pair for this context.
*/
with(value: T | null | undefined): ContextState<T>;
/**
* Run a function in a new context window (ignoring all current contexts) while injecting the specified states.
*
* @param states The states to inject.
* @param fn The function to run.
* @param args The function arguments.
* @returns The function's return value.
*/
static window<F extends (...args: any) => any>(states: ContextState<unknown>[], fn: F, ...args: Parameters<F>): ReturnType<F>;
/**
* Run a function while injecting the specified states.
*
* See {@link Inject `<Inject>`} when using JSX.
*
* @param states The states to inject. When providing multiple values for the same context, the last one is used.
* @param fn The function to run.
* @param args The function arguments.
* @returns The function's return value.
*/
static inject<F extends (...args: any) => any>(states: ContextState<unknown>[], fn: F, ...args: Parameters<F>): ReturnType<F>;
/**
* Capture all current context states.
*/
static capture(): ContextState<unknown>[];
/**
* Capture all current context states and wrap a function to always run with only these states injected.
*
* @param fn The function to wrap.
* @returns The wrapped function.
*/
static wrap<T extends (...args: any) => any>(fn: T): T;
}
/**
* A context-value pair.
*/
export interface ContextState<T> {
context: Context<T>;
/**
* The value that is injected when using this state with {@link Inject `<Inject>`}, {@link Context.inject} or {@link Context.window}.
*/
value: T | null | undefined;
}
/**
* Component for injecting context values while rendering.
*
* See {@link Context.inject} when not using JSX.
*/
export declare function Inject<T>(props: {
/** The context to inject into. */
context: Context<T>;
/** The value to inject. */
value: T | null | undefined;
children: () => unknown;
} | {
/** The context states to inject. */
states: ContextState<unknown>[];
children: () => unknown;
}): unknown;