refactor: reuse previous Prop instance to keep mobx reaction connnection alive

This commit is contained in:
LeoYuan 袁力皓 2022-06-15 14:33:03 +08:00 committed by 林熠
parent 567fd56cfe
commit 3105081466

View File

@ -297,6 +297,7 @@ export class Prop implements IPropParent {
items.forEach((prop) => prop.purge()); items.forEach((prop) => prop.purge());
} }
this._items = null; this._items = null;
this._prevMaps = this._maps;
this._maps = null; this._maps = null;
if (this._type !== 'slot' && this._slotNode) { if (this._type !== 'slot' && this._slotNode) {
this._slotNode.remove(); this._slotNode.remove();
@ -377,6 +378,14 @@ export class Prop implements IPropParent {
@obx.shallow private _maps: Map<string | number, Prop> | null = null; @obx.shallow private _maps: Map<string | number, Prop> | null = null;
/**
* _maps Prop
* Prop#_value { a: 1 } setValue({ a: 2 }) Prop
* mobx reaction observer
* reaction Prop(a) _value Prop(a) _value
*/
private _prevMaps: Map<string | number, Prop> | null = null;
get path(): string[] { get path(): string[] {
return (this.parent.path || []).concat(this.key as string); return (this.parent.path || []).concat(this.key as string);
} }
@ -400,7 +409,13 @@ export class Prop implements IPropParent {
const maps = new Map<string, Prop>(); const maps = new Map<string, Prop>();
const keys = Object.keys(data); const keys = Object.keys(data);
for (const key of keys) { for (const key of keys) {
const prop = new Prop(this, data[key], key); let prop: Prop;
if (this._prevMaps?.has(key)) {
prop = this._prevMaps.get(key)!;
prop.setValue(data[key]);
} else {
prop = new Prop(this, data[key], key);
}
items = items || []; items = items || [];
items.push(prop); items.push(prop);
maps.set(key, prop); maps.set(key, prop);