上下文 的node或则浏览器当中,如果global中不存在context,则赋值到global,如果存在,则获取对应的。
import { useContextKey } from '@kevisual/context';
const v = useContextKey('a', () => '123'); // 如果是第一次初始化,返回123,否则返回之前初始化的值
type GlobalContext<T = SimpleObject> = {
name?: string;
[key: string]: any;
} & T;
type InitFn<T> = () => T | Promise<T>;
type Init<T> = T | InitFn<T> | null;
type AsyncResult<T, ASYNC extends boolean = false> = ASYNC extends true ? Promise<T> : T;
type SimpleObject = Record<string, any>;
declare const useContext: <T = SimpleObject>(initContext?: GlobalContext<T>, isOverwrite?: boolean) => Required<GlobalContext<T>>;
declare const useContextKey: <T = any, ASYNC extends boolean = false>(key: string, init?: Init<T>, isNew?: boolean) => AsyncResult<T, ASYNC>;
declare const use: <T = any, ASYNC extends boolean = false>(key: string, init?: Init<T>, isNew?: boolean) => AsyncResult<T, ASYNC>;
declare const useConfig: <T = SimpleObject>(initConfig?: Partial<GlobalContext<T>>, isOverwrite?: boolean) => Required<GlobalContext<Partial<GlobalContext<T>>>>;
declare const useConfigKey: <T = any, ASYNC extends boolean = false>(key: string, init?: Init<T>, isNew?: boolean) => AsyncResult<T, ASYNC>;
/**
* 从环境变量获取
* @param envKey
* @param initKey
* @returns
*/
declare const useKey: <T = string>(envKey: T, initKey?: string) => any;
export { use, useConfig, useConfigKey, useContext, useContextKey, useKey };