UNPKG

node-web-mvc

Version:
72 lines (71 loc) 1.94 kB
"use strict"; /** * @module AbstractUpdater * @description 数据热更新抽象类 */ Object.defineProperty(exports, "__esModule", { value: true }); class AbstractUpdater { /** * 构造一个更新器 */ constructor(data, now, old) { // 根据构造函数创建实例 this.createInstance = (ctor, oldInstance) => new ctor(); // 从模块中导出需要的类型 this.useFn = (m) => m.exports.default || m.exports; // 判断指定项,是否需要热更新替换。 this.needHotFn = (m, ctor) => m instanceof ctor; this.data = data; this.now = now; this.old = old; } /** * 设置:是否需要热更新的比较函数 */ needHot(handler) { this.needHotFn = handler; return this; } /** * 设置:提取需要热更新的类型 */ use(handler) { this.useFn = handler; return this; } /** * 设置:创建新的的实例的函数 */ creator(createInstance) { this.createInstance = createInstance; return this; } /** * 执行更新 */ update() { const { now, old } = this; if (!now || !old || !this.data) return; // 预更新时,判断当前模块是否未拦截器,如果是的话,则删除,重新注册 const ctor = this.useFn(old); const newCtor = this.useFn(now); if (typeof ctor !== 'function') return; // 执行子类更新 this.internalUpdate(ctor, newCtor); // 清除 this.cleanUpdate(ctor); } /** * 清除当前传入的数据中,热更新模块旧的实例数据。 */ remove() { const ctor = this.useFn(this.old); if (typeof ctor !== 'function') { return; } this.cleanUpdate(ctor); } } exports.default = AbstractUpdater;