fix: 优化 prop.set 逻辑

This commit is contained in:
lihao.ylh 2021-11-29 12:34:15 +08:00
parent de0e669fb9
commit 5d92deb43d
4 changed files with 24 additions and 8 deletions

View File

@ -375,8 +375,14 @@ export class Prop implements IPropParent {
return (this.parent.path || []).concat(this.key as string); return (this.parent.path || []).concat(this.key as string);
} }
/**
* items maps
*/
@computed private get items(): Prop[] | null { @computed private get items(): Prop[] | null {
if (this._items) return this._items; // 当类型为 list 时,只要有 _items直接返回不再重新构造
if (this._type === 'list' && this._items) return this._items;
// 当类型为 map 时_items 和 _maps 理论上都应该存在,数量一致时,可以不再重新构造
if (this._type === 'map' && this._items && this._items.length === this._maps?.size) return this._items;
return runInAction(() => { return runInAction(() => {
let items: Prop[] | null = []; let items: Prop[] | null = [];
if (this._type === 'list') { if (this._type === 'list') {
@ -451,7 +457,7 @@ export class Prop implements IPropParent {
} }
if (createIfNone) { if (createIfNone) {
prop = new Prop(this, nest ? {} : UNSET, entry); prop = new Prop(this, UNSET, entry);
this.set(entry, prop, true); this.set(entry, prop, true);
if (nest) { if (nest) {
return prop.get(nest, true); return prop.get(nest, true);
@ -476,10 +482,10 @@ export class Prop implements IPropParent {
*/ */
@action @action
delete(prop: Prop): void { delete(prop: Prop): void {
if (this.items) { if (this._items) {
const i = this.items.indexOf(prop); const i = this._items.indexOf(prop);
if (i > -1) { if (i > -1) {
this.items.slice(i, 1); this._items.splice(i, 1);
prop.purge(); prop.purge();
} }
if (this._maps && prop.key) { if (this._maps && prop.key) {
@ -558,8 +564,9 @@ export class Prop implements IPropParent {
} else { } else {
items[key] = prop; items[key] = prop;
} }
this._items = items;
} else if (this.type === 'map') { } else if (this.type === 'map') {
const { maps } = this; const maps = this._maps || new Map<string, Prop>();
const orig = maps?.get(key); const orig = maps?.get(key);
if (orig) { if (orig) {
// replace // replace
@ -574,6 +581,7 @@ export class Prop implements IPropParent {
this._items = items; this._items = items;
maps?.set(key, prop); maps?.set(key, prop);
} }
this._maps = maps;
} /* istanbul ignore next */ else { } /* istanbul ignore next */ else {
return null; return null;
} }

View File

@ -202,7 +202,7 @@ export class Props implements IPropParent {
let prop = this.maps.get(entry); let prop = this.maps.get(entry);
if (!prop && createIfNone) { if (!prop && createIfNone) {
prop = new Prop(this, nest ? {} : UNSET, entry); prop = new Prop(this, UNSET, entry);
this.items.push(prop); this.items.push(prop);
} }

View File

@ -394,7 +394,11 @@ describe('Prop 类测试', () => {
prop.unset(); prop.unset();
prop.set(0, true); prop.set(0, true);
expect(prop.set('x', 'invalid')).toBeNull(); expect(prop.set('x', 'invalid')).toBeNull();
expect(prop.get(0).getValue()).toBeUndefined(); expect(prop.get(0).getValue()).toBeTruthy();
// map / list 级联测试
prop.get('loopArgs.0', true).setValue('newItem');;
expect(prop.get('loopArgs.0').getValue()).toBe('newItem');
}); });
it('export', () => { it('export', () => {

View File

@ -77,6 +77,10 @@ describe('Props 类测试', () => {
expect(props.get('l').getValue()).toBe('newlyCreatedProp'); expect(props.get('l').getValue()).toBe('newlyCreatedProp');
expect(props.get('m.m1').getValue()).toBe('newlyCreatedNestedProp'); expect(props.get('m.m1').getValue()).toBe('newlyCreatedNestedProp');
// map / list 级联测试
props.get('loopArgs.0', true).setValue('newItem');
expect(props.get('loopArgs.0').getValue()).toBe('newItem');
}); });
it('export', () => { it('export', () => {