Skip to content

Commit

Permalink
Merge pull request #116 from VisActor/perf/dataset
Browse files Browse the repository at this point in the history
perf: try to optimize performance of dataset and dataview
  • Loading branch information
xile611 authored Oct 19, 2023
2 parents fe855f1 + 084a461 commit a72faff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
22 changes: 14 additions & 8 deletions packages/vdataset/src/data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class DataSet {
* 多 DataView消息监听工具
*/
// eslint-disable-next-line @typescript-eslint/ban-types
_callMap: Map<Function, (...args: any[]) => void> = new Map();
_callMap: Map<Function, (...args: any[]) => void>;

constructor(public options?: IDataSetOptions) {
let name;
Expand Down Expand Up @@ -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 = () => {
Expand All @@ -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 }[]) {
Expand Down
32 changes: 20 additions & 12 deletions packages/vdataset/src/data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DataView {
/**
* 中间态数据,默认 history false 不存储
*/
historyData: any[] = [];
historyData: any[];

/**
* parser后的数据
Expand All @@ -81,10 +81,10 @@ export class DataView {
protected _fields: IFields = null;

// diff用数据id
private _diffData: boolean = false;
private _diffKeys: string[] = null;
_diffMap: Map<string, any> = new Map();
_diffRank: number = 0;
private _diffData: boolean;
private _diffKeys: string[];
_diffMap: Map<string, any>;
_diffRank: number;

// tag

Expand All @@ -105,6 +105,7 @@ export class DataView {

if (options?.history) {
this.history = options.history;
this.historyData = [];
}

this.dataSet.setDataView(name, this);
Expand All @@ -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');

Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a72faff

Please sign in to comment.