fix: 优化功能,去掉sync,当useContextKey中如果有promise自己维护Promise
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "@kevisual/use-config",
 | 
					  "name": "@kevisual/use-config",
 | 
				
			||||||
  "version": "1.0.8",
 | 
					  "version": "1.0.9",
 | 
				
			||||||
  "types": "dist/config.d.ts",
 | 
					  "types": "dist/config.d.ts",
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
    "build": "npm run clean && rollup -c",
 | 
					    "build": "npm run clean && rollup -c",
 | 
				
			||||||
@@ -57,5 +57,8 @@
 | 
				
			|||||||
      "import": "./dist/file-store.mjs",
 | 
					      "import": "./dist/file-store.mjs",
 | 
				
			||||||
      "types": "./dist/file-store.d.ts"
 | 
					      "types": "./dist/file-store.d.ts"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "@kevisual/load": "^0.0.4"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					import { BaseLoad } from '@kevisual/load';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type GlobalContext<U = any> = {
 | 
					type GlobalContext<U = any> = {
 | 
				
			||||||
  redis?: any;
 | 
					  redis?: any;
 | 
				
			||||||
} & U;
 | 
					} & U;
 | 
				
			||||||
@@ -15,28 +17,50 @@ export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T =>
 | 
				
			|||||||
  return global.context;
 | 
					  return global.context;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 获取上下文,当key存在时,返回key对应的值,不存在时,返回init函数的值
 | 
				
			||||||
 | 
					 * 若init不存在,key存在,直接进行循环检测,直到key存在,需要await
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param key
 | 
				
			||||||
 | 
					 * @param init
 | 
				
			||||||
 | 
					 * @returns
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
export const useContextKey = <T = any>(key: string, init?: () => T): T => {
 | 
					export const useContextKey = <T = any>(key: string, init?: () => T): T => {
 | 
				
			||||||
  const _context = useContext({});
 | 
					  const _context = useContext({});
 | 
				
			||||||
  if (key && typeof _context[key] !== 'undefined') {
 | 
					  if (key && typeof _context[key] !== 'undefined') {
 | 
				
			||||||
    return _context[key];
 | 
					    return _context[key];
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (key && init) {
 | 
					  if (key && init) {
 | 
				
			||||||
 | 
					    // 没有初始化过,初始化函数
 | 
				
			||||||
    _context[key] = (init as () => T)();
 | 
					    _context[key] = (init as () => T)();
 | 
				
			||||||
    return _context[key] as any;
 | 
					    return _context[key] as any;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return _context as any;
 | 
					  // 其他情况,默认已经初始化过
 | 
				
			||||||
};
 | 
					  if (key) {
 | 
				
			||||||
 | 
					    // 加载
 | 
				
			||||||
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
 | 
					    const baseLoad = new BaseLoad();
 | 
				
			||||||
  const _context = useContext({});
 | 
					    const voidFn = async () => {
 | 
				
			||||||
  if (key && typeof _context[key] !== 'undefined') {
 | 
					 | 
				
			||||||
      return _context[key];
 | 
					      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;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
  if (key && init) {
 | 
					      return _context[key];
 | 
				
			||||||
    _context[key] = await init();
 | 
					    };
 | 
				
			||||||
    return _context[key] as any;
 | 
					    return checkFn() as any;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return _context 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) => {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user