perf: for useConfigKey

This commit is contained in:
熊潇 2025-03-01 17:35:43 +08:00
parent 86ae2c7f70
commit 3ee69a4f81
3 changed files with 30 additions and 24 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/store", "name": "@kevisual/store",
"version": "0.0.2", "version": "0.0.3",
"main": "dist/store.js", "main": "dist/store.js",
"module": "dist/store.js", "module": "dist/store.js",
"types": "dist/store.d.ts", "types": "dist/store.d.ts",

View File

@ -5,11 +5,9 @@ import * as Load from '@kevisual/load/browser';
import { Page } from './page.ts'; import { Page } from './page.ts';
// bind to window, 必须要的获取全局的环境变量 // bind to window, 必须要的获取全局的环境变量
const { useConfigKey, useConfigKeySync, useContextKey, useContextKeySync } = WebEnv; const { useConfigKey, useContextKey } = WebEnv;
window.useConfigKey = useConfigKey; window.useConfigKey = useConfigKey;
window.useConfigKeySync = useConfigKeySync;
window.useContextKey = useContextKey; window.useContextKey = useContextKey;
window.useContextKeySync = useContextKeySync;
// @ts-ignore // @ts-ignore
window.webEnv = WebEnv; window.webEnv = WebEnv;
// @ts-ignore // @ts-ignore

View File

@ -1,4 +1,5 @@
import { getPathKey } from './utils/path-key.ts'; import { getPathKey } from './utils/path-key.ts';
import { BaseLoad } from '@kevisual/load';
type GlobalEnv = { type GlobalEnv = {
name?: string; name?: string;
@ -21,27 +22,41 @@ export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
// 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象 // 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象
export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => { export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => {
const _env = useEnv({}, initKey); const _env = useEnv({}, initKey);
if (key && _env[key]) { // 已经存在,直接返回
if (key && typeof _env[key] !== 'undefined') {
return _env[key]; return _env[key];
} }
// 不存在,但是有初始化函数,初始化的返回,同步函数,删除了重新加载?
if (key && init) { if (key && init) {
_env[key] = init(); _env[key] = init();
return _env[key]; return _env[key];
} }
return _env as any;
};
export const useEnvKeySync = async <T = any>(key: string, init?: () => Promise<T> | null, initKey = 'conifg'): Promise<T> => {
const _env = useEnv({}, initKey);
if (key && init) {
_env[key] = await init();
return _env[key];
}
if (key) { if (key) {
return _env[key]; // 加载
const baseLoad = new BaseLoad();
const voidFn = async () => {
return _env[key];
};
const checkFn = async () => {
const loadRes = await baseLoad.load(voidFn, {
key,
isReRun: true,
checkSuccess: () => _env[key],
timeout: 5 * 60 * 1000,
interval: 1000,
//
});
if (loadRes.code !== 200) {
console.error('load key error');
return null;
}
return _env[key];
};
return checkFn() as T;
} }
return _env as any; // 不存在,没有初始化函数
console.error('key is empty ');
return null;
}; };
export const usePageEnv = (init?: () => {}, initKey = 'conifg') => { export const usePageEnv = (init?: () => {}, initKey = 'conifg') => {
@ -61,9 +76,6 @@ export const useContextKey = <T = any>(key: string, init?: () => T): T => {
return useEnvKey(key, init, 'context'); return useEnvKey(key, init, 'context');
}; };
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
return useEnvKeySync(key, init, 'context');
};
export const usePageContext = (init?: () => {}) => { export const usePageContext = (init?: () => {}) => {
const { id } = getPathKey(); const { id } = getPathKey();
@ -82,10 +94,6 @@ export const useConfigKey = <T = any>(key: string, init?: () => T): T => {
return useEnvKey(key, init, 'config'); return useEnvKey(key, init, 'config');
}; };
export const useConfigKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
return useEnvKeySync(key, init, 'config');
};
export const usePageConfig = (init?: () => {}) => { export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey(); const { id } = getPathKey();
return useConfigKey(id, init); return useConfigKey(id, init);