smartrouter/ts/smartrouter.classes.selectiondimension.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

import * as plugins from './smartrouter.plugins.js';
export interface ISelectionOption<T = any> {
key: string;
detail: T;
selected?: boolean;
action: () => Promise<T>;
}
export class SelectionDimension<T = any> {
/**
* the key of the selection dimension
*/
public dimensionKey: string;
/**
* the level of the selection dimension
* a higher level is execution later than a lower level
* during catchup phase
*/
public dimensionLevel: number;
public dimensionPeers: SelectionDimension<T>[] = [];
public selectionOptions: ISelectionOption<T>[] = [];
public selectionOptionSubject = new plugins.smartrx.rxjs.Subject<ISelectionOption<T>[]>();
public async emitSelectionOptions () {
const selectionOptions = this.selectionOptions.map((selectionOptionArg): ISelectionOption => {
return {
key: selectionOptionArg.key,
detail: selectionOptionArg.detail,
action: async () => {
this.updateSelection(selectionOptionArg);
return await selectionOptionArg.action();
},
}
});
this.selectionOptionSubject.next(selectionOptions);
}
public async updateSelection (selectionOptionArg: ISelectionOption<T>) {
for (const selectionOption of this.selectionOptions) {
if (selectionOption.key === selectionOptionArg.key) {
selectionOption.selected = true;
} else {
selectionOption.selected = false;
}
}
this.emitSelectionOptions();
}
}