/** * 对象管理 * 懒加载对象 */ 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 } } }