@morjs/runtime-web
Version:
mor runtime for web
61 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.forceCheckFunctionChanged = exports.MixinComponentForHot = void 0;
// eslint-disable-next-line node/no-missing-import
const component_1 = require("./component");
let $HotId = 1;
// 支持热更新
function MixinComponentForHot(componentInit, render) {
componentInit.__HotId__ = ++$HotId;
return class extends component_1.KBComponent {
constructor(props) {
const compConfig = componentInit();
compConfig.__HotId__ = componentInit.__HotId__;
super(props, compConfig, {});
}
onInit() {
if (this.componentConfig.__HotId__ === componentInit.__HotId__) {
super.onInit();
}
}
render() {
if (render) {
if (this.componentConfig.__HotId__ !== componentInit.__HotId__) {
const compConfig = componentInit();
compConfig.__HotId__ = componentInit.__HotId__;
const oldConfig = this.componentConfig;
this.forceResetConfig(compConfig);
// 数据强制合并。使得通过setData设置的数据得以保留
Object.assign(compConfig.data, oldConfig.data);
// 强制合并props
this.mergeProps(this.props);
forceCheckConfig(oldConfig, this.componentConfig);
}
return render.call(this.componentConfig, this.getRenderData());
}
return false;
}
};
}
exports.MixinComponentForHot = MixinComponentForHot;
function forceCheckFunctionChanged(oldConfig, newConfig, name) {
const oldF = oldConfig[name];
const newF = newConfig[name];
if ((oldF ? oldF.toString() : '') !== (newF ? newF.toString() : '')) {
if (newF) {
try {
newConfig[name]();
}
catch (e) {
console.error(e);
}
}
}
}
exports.forceCheckFunctionChanged = forceCheckFunctionChanged;
function forceCheckConfig(oldConfig, newConfig) {
// 强制检查一次性调用的生命周期函数是否有更改,如有更改,那么强制调用
forceCheckFunctionChanged(oldConfig, newConfig, 'onInit');
forceCheckFunctionChanged(oldConfig, newConfig, 'didMount');
}
//# sourceMappingURL=component-hot.js.map