add context
This commit is contained in:
parent
ee9f84a64c
commit
691d5a1d3e
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/use-config",
|
"name": "@kevisual/use-config",
|
||||||
"version": "1.0.18",
|
"version": "1.0.19",
|
||||||
"types": "dist/config.d.ts",
|
"types": "dist/config.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run clean && tsup",
|
"build": "npm run clean && tsup",
|
||||||
@ -20,7 +20,8 @@
|
|||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.15.30",
|
"@kevisual/context": "^0.0.3",
|
||||||
|
"@types/node": "^24.0.1",
|
||||||
"dotenv": "^16.5.0",
|
"dotenv": "^16.5.0",
|
||||||
"fast-glob": "^3.3.3",
|
"fast-glob": "^3.3.3",
|
||||||
"json-schema-to-ts": "^3.1.1",
|
"json-schema-to-ts": "^3.1.1",
|
||||||
|
@ -1,22 +1,6 @@
|
|||||||
import { BaseLoad } from '@kevisual/load';
|
|
||||||
|
|
||||||
type GlobalContext<U = any> = {
|
type GlobalContext<U = any> = {
|
||||||
redis?: any;
|
redis?: any;
|
||||||
} & U;
|
} & U;
|
||||||
|
|
||||||
export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T => {
|
|
||||||
const context: GlobalContext = (global as any).context;
|
|
||||||
const _context = context || initContext;
|
|
||||||
if (!context) {
|
|
||||||
if (_context) {
|
|
||||||
(global as any).context = _context;
|
|
||||||
} else {
|
|
||||||
global.context = {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return global.context;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取上下文,当key存在时,返回key对应的值,不存在时,返回init函数的值
|
* 获取上下文,当key存在时,返回key对应的值,不存在时,返回init函数的值
|
||||||
* 若init不存在,key存在,直接进行循环检测,直到key存在,需要await
|
* 若init不存在,key存在,直接进行循环检测,直到key存在,需要await
|
||||||
@ -25,45 +9,10 @@ export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T =>
|
|||||||
* @param init
|
* @param init
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const useContextKey = <T = any>(key: string, init?: () => T): T => {
|
export { useContextKey, useContext } from '@kevisual/context';
|
||||||
const _context = useContext({});
|
|
||||||
if (key && typeof _context[key] !== 'undefined') {
|
|
||||||
return _context[key];
|
|
||||||
}
|
|
||||||
if (key && init) {
|
|
||||||
// 没有初始化过,初始化函数
|
|
||||||
_context[key] = (init as () => T)();
|
|
||||||
return _context[key] as any;
|
|
||||||
}
|
|
||||||
// 其他情况,默认已经初始化过
|
|
||||||
if (key) {
|
|
||||||
// 加载
|
|
||||||
const baseLoad = new BaseLoad();
|
|
||||||
const voidFn = async () => {
|
|
||||||
return _context[key];
|
|
||||||
};
|
|
||||||
const checkFn = async () => {
|
|
||||||
const loadRes = await baseLoad.load(voidFn, {
|
|
||||||
key,
|
|
||||||
isReRun: true,
|
|
||||||
checkSuccess: () => _context[key],
|
|
||||||
timeout: 5 * 60 * 1000,
|
|
||||||
interval: 1000,
|
|
||||||
//
|
|
||||||
});
|
|
||||||
if (loadRes.code !== 200) {
|
|
||||||
console.error('load key error');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return _context[key];
|
|
||||||
};
|
|
||||||
return checkFn() as any;
|
|
||||||
}
|
|
||||||
console.error('key is empty');
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const setContextKey = <T>(key: string, value: T) => {
|
export const setContextKey = <T>(key: string, value: T) => {
|
||||||
|
// @ts-ignore
|
||||||
const _context = useContext<GlobalContext<T>>({});
|
const _context = useContext<GlobalContext<T>>({});
|
||||||
_context[key] = value;
|
_context[key] = value;
|
||||||
return _context;
|
return _context;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user