Files
kit.program/kit/src/context/index.js
T
2024-03-07 15:50:13 +08:00

33 lines
766 B
JavaScript

/**
* 对象管理
* 懒加载对象
*/
const config = require("../config");
module.exports = class Context {
constructor() {
this.beans = {}
this.standalones = {}
}
standalone(name, factory){
if ( this.standalones[name] ){
throw `重定义 ${name}`
}
if ( ! factory ) {
throw `定义 ${name} 时未提供必须参数 factory`
}
this.standalones[name] = factory
}
async get(name){
if ( this.beans[name] ) {
return this.beans[name]
}
if ( this.standalones[name] ){
this.beans[name] = await this.standalones[name]()
return this.beans[name]
} else {
return undefined
}
}
}