mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
85 lines (84 loc) • 2.41 kB
TypeScript
/**
* A context.
*/
export interface Context<T> {
/**
* Gets the context default value.
*
* @returns
*/
getDefault(): T;
/**
* Sets the context default value.
* @param value
*/
setDefault(value: T): void;
/**
* Sets the context default value resolver.
* @param valueFn
*/
setDefaultComputed(valueFn: () => T): void;
/**
* Gets the context value for a given node.
* @param node
*/
get(node: object): T;
/**
* Gets node that will provide the context value, or `undefined`
* if it comes from the default.
* @param node
*/
getProviderNode(node: object): object | undefined;
/**
* Sets the context value for a given node, effectively making it a provider.
* @param node
* @param value
*/
set(node: object, value: T): void;
/**
* Sets the context value resolver for a given node, effectively making it a provider.
* @param node
* @param valueFn
*/
setComputed(node: object, valueFn: () => T): void;
/**
* Unsets the context value for a given node, therefore it won't be a provider anymore.
* @param node
*/
unset(node: object): void;
/**
* Applies a value override while the given function is running and, if a node is returned,
* sets the node as a provider of the value.
*
* @template R
* @param fn Function to run.
* @param value Value to apply.
* @returns The value returned from the function.
*/
apply<R>(fn: () => R, value: T): R;
/**
* Applies a computed value override while the given function is running and, if a node is returned,
* sets the node as a provider of the computed value.
*
* @template R
* @param fn Function to run.
* @param valueFn Function that returns the value to apply.
* @returns The value returned from the function.
*/
applyComputed<R>(fn: () => R, valueFn: () => T): R;
}
/**
* Creates a new context with no default value, thus making its default value undefined.
*
* @template T Context value type.
* @returns
*/
export declare function createContext<T>(): Context<T | undefined>;
/**
* Creates a new context with a default value.
*
* @template T Context value type.
* @param defaultValue
* @returns
*/
export declare function createContext<T>(defaultValue: T): Context<T>;