UNPKG

@zenweb/core

Version:

ZenWeb Core Module - Module loader and Server

107 lines (106 loc) 2.98 kB
import { debug } from './util.js'; export const SETUP_AFTER = Symbol('zenweb#setupAfter'); export const SETUP_DESTROY = Symbol('zenweb#setupDestroy'); export class SetupHelper { /** * Core 实例 */ core; /** * 取得 Koa Application 实例 */ app; [SETUP_AFTER]; [SETUP_DESTROY]; /** * 模块名称 */ name; /** * 模块命名空间调试信息输出 */ debug; constructor(core, name) { this.core = core; this.app = core.app; this.name = name; this.debug = debug.extend(name); } /** * 定义核心属性 * @param prop * @param attributes * @returns */ defineCoreProperty(prop, attributes) { if (prop in this.core) { throw new Error(`define core property [${String(prop)}] duplicated`); } this.debug('defineCoreProperty: %s', prop); Object.defineProperty(this.core, prop, attributes); } /** * 定义上下文附加属性 * @param prop * @param attributes * @returns */ defineContextProperty(prop, attributes) { this._checkContextPropertyExists(prop); this.debug('defineContextProperty: %s', prop); Object.defineProperty(this.app.context, prop, attributes); } /** * 在 Context 中定义属性并缓存,当第一次调用属性时执行 get 方法,之后不再调用 get * @param prop 属性名称 * @param get 第一次访问时回调 */ defineContextCacheProperty(prop, get) { this._checkContextPropertyExists(prop); this.debug('defineContextCacheProperty: %s', prop); const CACHE = Symbol('zenweb#contextCacheProperty'); Object.defineProperty(this.app.context, prop, { get() { if (this[CACHE] === undefined) { this[CACHE] = get(this) || null; } return this[CACHE]; } }); } _checkContextPropertyExists(prop) { if (prop in this.app.context) { throw new Error(`define context property [${String(prop)}] duplicated`); } } /** * 检查模块是否存在 - 如果不存在则直接抛出异常 * @param name 模块名称 * @param msg 自定义错误信息 */ assertModuleExists(name, msg) { if (!this.core.moduleExists(name)) { throw new Error(msg || `missing module: ${name}`); } } /** * 所有模块初始化完成后执行回调 */ after(callback) { this[SETUP_AFTER] = callback; } /** * 销毁回调 * 服务停止时会调用方法,等待方法完成 */ destroy(callback) { this[SETUP_DESTROY] = callback; } /** * 使用全局中间件 */ middleware(middleware) { this.debug('middleware: %s', middleware.name || '-'); this.app.use(middleware); } }