diff --git a/packages/vdataset/src/data-set.ts b/packages/vdataset/src/data-set.ts index a9d7e23..edb5948 100644 --- a/packages/vdataset/src/data-set.ts +++ b/packages/vdataset/src/data-set.ts @@ -40,7 +40,7 @@ export class DataSet { * 多 DataView消息监听工具 */ // eslint-disable-next-line @typescript-eslint/ban-types - _callMap: Map void> = new Map(); + _callMap: Map void>; constructor(public options?: IDataSetOptions) { let name; @@ -144,6 +144,10 @@ export class DataSet { // eslint-disable-next-line @typescript-eslint/ban-types multipleDataViewAddListener(list: DataView[], event: string, call: Function) { + if (!this._callMap) { + this._callMap = new Map(); + } + let callAd = this._callMap.get(call); if (!callAd) { callAd = () => { @@ -159,19 +163,21 @@ export class DataSet { this._callMap.set(call, callAd); } - allDataViewAddListener(event: string, call: Function) { + allDataViewAddListener(event: string, call: () => void) { this.multipleDataViewAddListener(Object.values(this.dataViewMap), event, call); } // eslint-disable-next-line @typescript-eslint/ban-types multipleDataViewRemoveListener(list: DataView[], event: string, call: Function) { - const callAd = this._callMap.get(call); - if (callAd) { - list.forEach(l => { - l.target.removeListener(event, callAd); - }); + if (this._callMap) { + const callAd = this._callMap.get(call); + if (callAd) { + list.forEach(l => { + l.target.removeListener(event, callAd); + }); + } + this._callMap.delete(call); } - this._callMap.delete(call); } multipleDataViewUpdateInParse(newData: { name: string; data: any; options?: IParserOptions }[]) { diff --git a/packages/vdataset/src/data-view.ts b/packages/vdataset/src/data-view.ts index afe38f2..caa8907 100644 --- a/packages/vdataset/src/data-view.ts +++ b/packages/vdataset/src/data-view.ts @@ -63,7 +63,7 @@ export class DataView { /** * 中间态数据,默认 history false 不存储 */ - historyData: any[] = []; + historyData: any[]; /** * parser后的数据 @@ -81,10 +81,10 @@ export class DataView { protected _fields: IFields = null; // diff用数据id - private _diffData: boolean = false; - private _diffKeys: string[] = null; - _diffMap: Map = new Map(); - _diffRank: number = 0; + private _diffData: boolean; + private _diffKeys: string[]; + _diffMap: Map; + _diffRank: number; // tag @@ -105,6 +105,7 @@ export class DataView { if (options?.history) { this.history = options.history; + this.historyData = []; } this.dataSet.setDataView(name, this); @@ -127,7 +128,6 @@ export class DataView { options && (this.parseOption = options); const cloneData = this.cloneParseData(data, options); if (options?.type) { - options = cloneDeep(options); // 默认bytejson const parserFn = this.dataSet.getParser(options.type) ?? this.dataSet.getParser('bytejson'); @@ -178,7 +178,7 @@ export class DataView { pushOption && this.transformsArr.push(options); if (execute) { const lastTag = this.isLastTransform(options); - options = cloneDeep(options); + this.executeTransform(options); if (lastTag) { this.diffLastData(); @@ -195,7 +195,9 @@ export class DataView { } sortTransform() { - this.transformsArr.sort((a, b) => (a.level ?? 0) - (b.level ?? 0)); + if (this.transformsArr.length >= 2) { + this.transformsArr.sort((a, b) => (a.level ?? 0) - (b.level ?? 0)); + } } private executeTransform( @@ -234,24 +236,29 @@ export class DataView { this.isRunning = true; this.resetTransformData(); this.transformsArr.forEach(t => { - this.executeTransform(t, { ...opt, emitMessage: false }); + this.executeTransform(t, { pushHistory: opt.pushHistory, emitMessage: false }); if (this.isLastTransform(t)) { this.diffLastData(); } }); this.isRunning = false; - opt?.emitMessage !== false && this.target.emit('change', []); + + opt.emitMessage !== false && this.target.emit('change', []); return this; }; enableDiff(keys: string[]) { this._diffData = true; this._diffKeys = keys; + + this._diffMap = new Map(); + this._diffRank = 0; } disableDiff() { this._diffData = false; - this.resetDiff(); + this._diffMap = null; + this._diffRank = null; } resetDiff() { @@ -390,7 +397,8 @@ export class DataView { destroy() { this.dataSet.removeDataView(this.name); - this.resetDiff(); + this._diffMap = null; + this._diffRank = null; this.latestData = null; this.rawData = null; this.parserData = null;