Skip to content

Commit

Permalink
Merge pull request #266 from lineupjs/sgratzl/module_sideeffect
Browse files Browse the repository at this point in the history
avoid toolbar side effects
  • Loading branch information
thinkh committed Mar 23, 2020
2 parents 0053579 + c42058b commit 6f83c31
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
21 changes: 2 additions & 19 deletions src/model/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,7 @@ export function chooseUIntByDataLength(dataLength?: number | null) {
return ECompareValueType.UINT32;
}


// side effect
const cache = new Map<string, string[]>();


export function getAllToolbarActions(col: Column) {
if (cache.has(col.desc.type)) {
return cache.get(col.desc.type)!;
}
const actions = new OrderedSet<string>();

// walk up the prototype chain
Expand All @@ -237,17 +229,11 @@ export function getAllToolbarActions(col: Column) {
}
obj = Object.getPrototypeOf(obj);
} while (obj);
const arr = Array.from(actions);
cache.set(col.desc.type, arr);
return arr;
return Array.from(actions);
}


export function getAllToolbarDialogAddons(col: Column, key: string) {
const cacheKey = `${col.desc.type}@${key}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey)!;
}
const actions = new OrderedSet<string>();

// walk up the prototype chain
Expand All @@ -262,8 +248,5 @@ export function getAllToolbarDialogAddons(col: Column, key: string) {
}
obj = Object.getPrototypeOf(obj);
} while (obj);
cache.set(cacheKey, Array.from(actions));
const arr = Array.from(actions);
cache.set(cacheKey, arr);
return arr;
return Array.from(actions);
}
6 changes: 5 additions & 1 deletion src/ui/EngineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export default class EngineRenderer extends AEventDispatcher {
};
},
getPossibleRenderer: (col: Column) => getPossibleRenderer(col, this.options.renderers, this.options.canRender),
colWidth: (col: Column) => !col.isVisible() ? 0 : col.getWidth()
colWidth: (col: Column) => !col.isVisible() ? 0 : col.getWidth(),
caches: {
toolbar: new Map(),
toolbarAddons: new Map()
}
};

this.table = new MultiTableRowRenderer(this.node, this.idPrefix);
Expand Down
5 changes: 5 additions & 0 deletions src/ui/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface IRankingHeaderContextContainer {
getPossibleRenderer(col: Column): {item: IRenderInfo[], group: IRenderInfo[], summary: IRenderInfo[]};

summaryRenderer(co: Column, interactive: boolean, imposer?: IImposer): ISummaryRenderer;

readonly caches: {
toolbar: Map<string, IToolbarAction[]>,
toolbarAddons: Map<string, IToolbarDialogAddon[]>
};
}

export interface IRankingBodyContext extends IRankingHeaderContextContainer, IRenderContext {
Expand Down
5 changes: 2 additions & 3 deletions src/ui/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,8 @@ function sortActions(a: IToolbarAction, b: IToolbarAction) {
return (a.options.order || 50) - (b.options.order || 50);
}

const cache = new Map<string, IToolbarAction[]>();
const cacheAddon = new Map<string, IToolbarDialogAddon[]>();

function getFullToolbar(col: Column, ctx: IRankingHeaderContext) {
const cache = ctx.caches.toolbar;
if (cache.has(col.desc.type)) {
return cache.get(col.desc.type)!;
}
Expand Down Expand Up @@ -377,6 +375,7 @@ export function getToolbar(col: Column, ctx: IRankingHeaderContext) {
/** @internal */
export function getToolbarDialogAddons(col: Column, key: string, ctx: IRankingHeaderContext) {
const cacheKey = `${col.desc.type}@${key}`;
const cacheAddon = ctx.caches.toolbarAddons;
if (cacheAddon.has(cacheKey)) {
return cacheAddon.get(cacheKey)!;
}
Expand Down

0 comments on commit 6f83c31

Please sign in to comment.