Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: try to optimize performance of dataset and dataview #116

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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