perf: for useConfigKey
This commit is contained in:
parent
86ae2c7f70
commit
3ee69a4f81
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/store",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.3",
|
||||
"main": "dist/store.js",
|
||||
"module": "dist/store.js",
|
||||
"types": "dist/store.d.ts",
|
||||
|
@ -5,11 +5,9 @@ import * as Load from '@kevisual/load/browser';
|
||||
import { Page } from './page.ts';
|
||||
|
||||
// bind to window, 必须要的获取全局的环境变量
|
||||
const { useConfigKey, useConfigKeySync, useContextKey, useContextKeySync } = WebEnv;
|
||||
const { useConfigKey, useContextKey } = WebEnv;
|
||||
window.useConfigKey = useConfigKey;
|
||||
window.useConfigKeySync = useConfigKeySync;
|
||||
window.useContextKey = useContextKey;
|
||||
window.useContextKeySync = useContextKeySync;
|
||||
// @ts-ignore
|
||||
window.webEnv = WebEnv;
|
||||
// @ts-ignore
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { getPathKey } from './utils/path-key.ts';
|
||||
import { BaseLoad } from '@kevisual/load';
|
||||
|
||||
type GlobalEnv = {
|
||||
name?: string;
|
||||
@ -21,27 +22,41 @@ export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
|
||||
// 从全局环境变量中获取指定的key值,如果没有则初始化一个, key不存在,返回Env对象
|
||||
export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => {
|
||||
const _env = useEnv({}, initKey);
|
||||
if (key && _env[key]) {
|
||||
// 已经存在,直接返回
|
||||
if (key && typeof _env[key] !== 'undefined') {
|
||||
return _env[key];
|
||||
}
|
||||
// 不存在,但是有初始化函数,初始化的返回,同步函数,删除了重新加载?
|
||||
if (key && init) {
|
||||
_env[key] = init();
|
||||
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) {
|
||||
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') => {
|
||||
@ -61,9 +76,6 @@ export const useContextKey = <T = any>(key: string, init?: () => T): T => {
|
||||
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?: () => {}) => {
|
||||
const { id } = getPathKey();
|
||||
@ -82,10 +94,6 @@ export const useConfigKey = <T = any>(key: string, init?: () => T): T => {
|
||||
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?: () => {}) => {
|
||||
const { id } = getPathKey();
|
||||
return useConfigKey(id, init);
|
||||
|
Loading…
x
Reference in New Issue
Block a user