fix: fix that the prop model is not reused and the update is not triggered

This commit is contained in:
liujuping 2023-05-31 18:18:52 +08:00 committed by 林熠
parent d82bcfdf2a
commit 035c213b16
3 changed files with 7 additions and 11 deletions

View File

@ -15,6 +15,7 @@
},
"license": "MIT",
"dependencies": {
"@alilc/build-plugin-lce": "^0.0.4-beta.2",
"@alilc/lowcode-editor-core": "1.1.7",
"@alilc/lowcode-types": "1.1.7",
"@alilc/lowcode-utils": "1.1.7",

View File

@ -148,15 +148,13 @@ export class Prop implements IProp, IPropParent {
@obx.shallow private _items: IProp[] | null = null;
@obx.shallow private _maps: Map<string | number, IProp> | null = null;
/**
* 作为 _maps 的一层缓存机制,主要是复用部分已存在的 Prop,保持响应式关系,比如:
* 作为一层缓存机制,主要是复用部分已存在的 Prop,保持响应式关系,比如:
* 当前 Prop#_value 值为 { a: 1 },当调用 setValue({ a: 2 }) 时,所有原来的子 Prop 均被销毁,
* 导致假如外部有 mobx reaction(常见于 observer),此时响应式链路会被打断,
* 因为 reaction 监听的是原 Prop(a) 的 _value,而不是新 Prop(a) 的 _value。
*/
private _prevMaps: Map<string | number, IProp> | null = null;
@obx.shallow private _maps: Map<string | number, IProp> | null = null;
/**
* 构造 items 属性,同时构造 maps 属性
@ -171,8 +169,8 @@ export class Prop implements IProp, IPropParent {
data.forEach((item: any, idx: number) => {
items = items || [];
let prop;
if (this._prevMaps?.has(idx.toString())) {
prop = this._prevMaps.get(idx.toString())!;
if (this._maps?.has(idx.toString())) {
prop = this._maps.get(idx.toString())!;
prop.setValue(item);
} else {
prop = new Prop(this, item, idx);
@ -187,8 +185,8 @@ export class Prop implements IProp, IPropParent {
const keys = Object.keys(data);
for (const key of keys) {
let prop: IProp;
if (this._prevMaps?.has(key)) {
prop = this._prevMaps.get(key)!;
if (this._maps?.has(key)) {
prop = this._maps.get(key)!;
prop.setValue(data[key]);
} else {
prop = new Prop(this, data[key], key);
@ -419,8 +417,6 @@ export class Prop implements IProp, IPropParent {
items.forEach((prop) => prop.purge());
}
this._items = null;
this._prevMaps = this._maps;
this._maps = null;
if (this._type !== 'slot' && this._slotNode) {
this._slotNode.remove();
this._slotNode = undefined;

View File

@ -379,7 +379,6 @@ describe('Prop 类测试', () => {
prop.dispose();
expect(prop._items).toBeNull();
expect(prop._maps).toBeNull();
});
});