UNPKG

node-web-mvc

Version:
103 lines (102 loc) 2.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Hook_1 = __importDefault(require("./Hook")); class HotModule { /** * 构建一个热更新模块 * @param id 模块id */ constructor(id) { /** * 当前接受的accept */ this.hooks = { /** * 在更新后执行 */ accept: new Hook_1.default(), /** * 在执行accept前执行 */ pre: new Hook_1.default(), /** * 在执行完pre在accept之前执行 */ preend: new Hook_1.default(), /** * 再热更新完毕后 */ postend: new Hook_1.default(), /** * 新增文件后触发 */ created: new Hook_1.default(), /** * 热更新结束 */ done: new Hook_1.default(), }; this.id = id; this.reasons = []; } /** * 判断,是否有任意钩子监听 */ get hasAnyHooks() { const { accept, pre, preend, postend } = this.hooks; return accept.count > 0 || pre.count > 0 || preend.count > 0 || postend.count > 0; } /** * 判断当前执行,是否是从热更新触发 */ accept(handler) { this.hooks.accept.add(handler); return this; } /** * 监听预更新,在热更新前执行 */ preload(handler) { this.hooks.pre.add(handler); return this; } /** * 在pre钩子执行后执行 */ preend(handler) { this.hooks.preend.add(handler); return this; } created(handler) { this.hooks.created.add(handler); return this; } /** * 清除hooks * @param types 要清除的hooks类型 */ clean(...types) { types = types.length < 1 ? Object.keys(this.hooks) : types; types.forEach((name) => { const hook = this.hooks[name]; hook.clean(); }); return this; } /** * 热更新完毕 * @param params */ postend(handler) { this.hooks.postend.add(handler); return this; } allDone(handler) { this.hooks.done.add(handler); return this; } } exports.default = HotModule;