43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
interface UseContext<T> {
|
|
/**
|
|
* Get the current context. Throws if no context is set.
|
|
*/
|
|
use: () => T;
|
|
/**
|
|
* Get the current context. Returns `null` when no context is set.
|
|
*/
|
|
tryUse: () => T | null;
|
|
/**
|
|
* Set the context as Singleton Pattern.
|
|
*/
|
|
set: (instance?: T, replace?: Boolean) => void;
|
|
/**
|
|
* Clear current context.
|
|
*/
|
|
unset: () => void;
|
|
/**
|
|
* Exclude a synchronous function with the provided context.
|
|
*/
|
|
call: <R>(instance: T, callback: () => R) => R;
|
|
/**
|
|
* Exclude an asynchronous function with the provided context.
|
|
* Requires installing the transform plugin to work properly.
|
|
*/
|
|
callAsync: <R>(instance: T, callback: () => R | Promise<R>) => Promise<R>;
|
|
}
|
|
declare function createContext<T = any>(): UseContext<T>;
|
|
interface ContextNamespace {
|
|
get: <T>(key: string) => UseContext<T>;
|
|
}
|
|
declare function createNamespace<T = any>(): {
|
|
get(key: any): UseContext<T>;
|
|
};
|
|
declare const defaultNamespace: ContextNamespace;
|
|
declare const getContext: <T>(key: string) => UseContext<T>;
|
|
declare const useContext: <T>(key: string) => () => T;
|
|
type AsyncFunction<T> = () => Promise<T>;
|
|
declare function executeAsync<T>(function_: AsyncFunction<T>): [Promise<T>, () => void];
|
|
declare function withAsyncContext<T = any>(function_: AsyncFunction<T>, transformed?: boolean): AsyncFunction<T>;
|
|
|
|
export { ContextNamespace, UseContext, createContext, createNamespace, defaultNamespace, executeAsync, getContext, useContext, withAsyncContext };
|