添加 kit 子目录
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 对象管理
|
||||
* 懒加载对象
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user