import { injectable } from 'inversify'; import { IDisposable } from 'ts-toolset'; import { mergeListToMap } from 'ts-toolset/dist/common/map'; import { IFunctionalEntity, IVariableEntity } from '../../../models/html'; export const mainFunctional = 'mainFunctional'; const variableKey = 'key'; export interface IFunctional extends IDisposable { getVariable(key: string, storeKey: string): IVariableEntity | undefined; appendVariable(variable: IVariableEntity | IVariableEntity[], key: string): IFunctional; updateVariable(): IFunctional; removeVariable(): IFunctional; appendFunction(func: any, key: string): IFunctional; } @injectable() export class Functional implements IFunctional { private _variableStores!: Map>; private _functionStores!: Map>; private _mainVariableStore!: Map; private _mainFunctionStore!: Map; constructor() { this._initStore(); } private _initStore() { this._mainVariableStore = new Map(); this._mainFunctionStore = new Map(); this._variableStores = new Map().set(mainFunctional, this._mainVariableStore); this._functionStores = new Map().set(mainFunctional, this._mainFunctionStore); } getVariable(key: string, storeKey: any = mainFunctional) { let variable: IVariableEntity | undefined = undefined; const store = this._variableStores.get(storeKey); store && (variable = store.get(key)); return variable; } /** 添加变量到指定集合,默认添加到 主变量集合(mainFunctional),当指定集合不存在时会根据指定storeKey创建一个新当集合 */ appendVariable(variable: IVariableEntity | IVariableEntity[], storeKey: any = mainFunctional): Functional { const variables = Array.isArray(variable) ? variable : [variable]; this._variableStores.has(storeKey) || this._variableStores.set(storeKey, new Map()); const variableStore = this._variableStores.get(storeKey)!; mergeListToMap(variableStore, variables, variableKey); return this; } updateVariable() { return this; } removeVariable() { return this; } appendFunction(func: any, storeKey: any = mainFunctional) { return this; } dispose(): Functional { this._variableStores.forEach(store => store.clear()); this._variableStores.clear(); this._functionStores.forEach(store => store.clear()); this._functionStores.clear(); this._initStore(); return this; } }