UNPKG

@robicue/use-context

Version:

The official useContext implementation for the Robicue Hook Architecture

138 lines (137 loc) 5.93 kB
/** * The context type */ export declare type Context = object; /** * The states holder type */ export declare type States = Map<Hook<any, unknown, any>, any>; /** * A simple hook type */ export declare type Hook<A extends unknown[], R, C extends Context = Context> = (context: C, ...args: A) => R; /** * An extendable hook type */ export declare type ExtendedHook<A extends unknown[], R, C extends Context = Context> = (originalHook: Hook<A, R, C>, context: C, ...args: A) => R; /** * Checks if the provided value is a valid context object */ export declare const isContext: (value: Context) => boolean; /** * Checks if the specified context is a forked context */ export declare const isForked: (context: Context) => boolean; /** * Returns TRUE if the specified initializer is used in the context */ export declare const isUsed: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>) => boolean; /** * Set a new state into the context */ export declare const set: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>, state: R) => R; /** * Initializes a new state into the context */ export declare const init: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>, ...args: A) => R; /** * Uses or initializes a state in the context */ export declare const use: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>, ...args: A) => R; /** * Calls the initializer or gets a state in the context */ export declare const call: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>, ...args: A) => R; /** * Gets a state that has already been initialized */ export declare const get: <A extends unknown[], R, C extends object = object>(context: C, initializer: Hook<A, R, C>) => R; /** * Makes a fork of all forkable states the current context. * The forked context allows you to keep access to the states * of the current context, but newly initialized states, created * in the forked context, are not seen by hooks using the current context. */ export declare const fork: <C extends object = object>(context: Context, forkedContext?: C) => C; /** * Checks if the state of an initializer is inherited form a parent context */ export declare const isInherited: <A extends unknown[], R, C extends object = object>(context: Context, initializer: Hook<A, R, C>) => boolean; /** * Creates a hook function */ export declare const hook: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => (context: C, ...args: A) => R; /** * Creates a clone of a hook function. */ export declare const clone: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => (context: C, ...args: A) => R; /** * Creates an anchor hook factory */ export declare const factory: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => () => (context: C, ...args: A) => R; /** * Creates a hook that memorizes the result in the context */ export declare const anchor: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => (context: C, ...args: A) => R; /** * Creates a hook that can be extended */ export declare const extendable: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => (context: C, ...args: A) => R; /** * Extends a hook within a certain context */ export declare const extend: <A extends unknown[], R, C extends object = object>(context: C, func: Hook<A, R, C>, extension: ExtendedHook<A, R, C>) => void; /** * Creates a utility hook that memorizes the result of the context. * The context is optional here. If not provided, the specified * function itself will be used as context. */ export declare const util: <A extends unknown[], R>(func: (context: Context, ...args: A) => R) => (context?: object | undefined, ...args: A) => R; /** * Creates an hook that memorizes the result in the unforkable context */ export declare const buoy: <A extends unknown[], R, C extends object = object>(func: (context: C, ...args: A) => R) => (context: C, ...args: A) => R; /** * Major hook that can be used to: * - create a new context * - create a fork of a context * - get or initialize a contextual state */ export declare const useContext: <C extends object = object>(context?: C) => { context: C; /** * Returns TRUE if the specified initializer is used in the context */ isUsed<A extends unknown[], R>(initializer: Hook<A, R, C>): boolean; /** * Sets a new state into the context */ set<A_1 extends unknown[], R_1>(initializer: Hook<A_1, R_1, C>, state: R_1): R_1; /** * Initializes a new state into the context */ init<A_2 extends unknown[], R_2>(initializer: Hook<A_2, R_2, C>, ...args: A_2): R_2; /** * Uses or initializes a state in the context */ use<A_3 extends unknown[], R_3>(initializer: Hook<A_3, R_3, C>, ...args: A_3): R_3; /** * Gets a state that has already been initialized */ get<A_4 extends unknown[], R_4>(initializer: Hook<A_4, R_4, C>): R_4; /** * Extends a hook within a certain context */ extend<A_5 extends unknown[], R_5>(initializer: Hook<A_5, R_5, C>, extension: ExtendedHook<A_5, R_5, C>): void; /** * Makes a fork of the current context. * The forked context allows you to keep access to the states * of the current context, but newly initialized states, created * in the forked context, are not seen by hooks using the current context. */ fork(forkedContext?: Context): object; /** * Checks if the state of an initializer is inherited form a parent context */ isInherited<A_6 extends unknown[], R_6>(initializer: Hook<A_6, R_6, C>): boolean; };