refactor: prop 的子实例不再延迟初始化, 也不再 node 未初始化完成之前就调用 dispose 方法

This commit is contained in:
lihao.ylh 2021-09-01 21:10:02 +08:00
parent a10641daff
commit ec31bf9ea5
5 changed files with 20 additions and 15 deletions

View File

@ -157,7 +157,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
return this.componentMeta.icon;
}
private isInited = false;
isInited = false;
constructor(readonly document: DocumentModel, nodeSchema: Schema, options: any = {}) {
const { componentName, id, children, props, ...extras } = nodeSchema;
@ -168,9 +168,6 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
children: isDOMText(children) || isJSExpression(children) ? children : '',
});
} else {
// 这里 props 被初始化两次,一次 new一次 importnew 的实例需要给 propsReducer 的钩子去使用,
// import 是为了使用钩子返回的值,并非完全幂等的操作,部分行为执行两次会有 bug
// 所以在 props 里会对 new / import 做一些区别化的解析
this.props = new Props(this, props, extras);
this._children = new NodeChildren(this as ParentalNode, this.initialChildren(children));
this._children.internalInitParent();

View File

@ -55,6 +55,12 @@ export class Prop implements IPropParent {
if (value !== UNSET) {
this.setValue(value);
}
this.setupItems();
}
// TODO: 先用调用方式触发子 prop 的初始化,后续须重构
private setupItems() {
return this.items;
}
/**
@ -247,7 +253,9 @@ export class Prop implements IPropParent {
};
}
this.dispose();
if (this.owner.isInited) {
this.dispose();
}
if (oldValue !== this._value) {
editor?.emit('node.innerProp.change', {
@ -302,7 +310,6 @@ export class Prop implements IPropParent {
owner.addSlot(this._slotNode);
this._slotNode.internalSetSlotFor(this);
}
this.dispose();
}
/**

View File

@ -166,7 +166,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -240,7 +240,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -310,7 +310,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -392,7 +392,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -478,7 +478,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -620,7 +620,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -694,7 +694,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",
@ -776,7 +776,7 @@ Object {
"labelTextAlign": "right",
"labelTipsIcon": "",
"labelTipsText": Object {
"en_US": null,
"en_US": "",
"type": "i18n",
"use": "zh_CN",
"zh_CN": "",

View File

@ -99,7 +99,7 @@ describe('document-model 测试', () => {
expect(doc.suspensed).toBeFalsy();
doc.suspense();
doc.active();
doc.activate();
doc.close();
doc.remove();

View File

@ -27,6 +27,7 @@ const mockedOwner = {
},
designer: {},
},
isInited: true,
};
const mockedPropsInst = {