feat(core): Add selection dimensions and route removal functionality
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartrouter',
|
||||
version: '1.0.17',
|
||||
description: 'a router for routing on websites'
|
||||
version: '1.1.0',
|
||||
description: 'A JavaScript library providing routing capabilities for web applications.'
|
||||
}
|
||||
|
50
ts/smartrouter.classes.selectiondimension.ts
Normal file
50
ts/smartrouter.classes.selectiondimension.ts
Normal file
@ -0,0 +1,50 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import * as plugins from './smartrouter.plugins.js';
|
||||
|
||||
import { QueryParams } from './smartrouter.classes.queryparams.js';
|
||||
import { type ISelectionOption, SelectionDimension } from './smartrouter.classes.selectiondimension.js';
|
||||
|
||||
const routeLog = (message: string) => {
|
||||
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
|
||||
@ -71,10 +72,15 @@ export class SmartRouter {
|
||||
* @param {function} handlerArg
|
||||
*/
|
||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||
this.routes.push({
|
||||
const routeObject = {
|
||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||
handler: handlerArg,
|
||||
});
|
||||
};
|
||||
this.routes.push(routeObject);
|
||||
const removeFunction = () => {
|
||||
this.routes.splice(this.routes.indexOf(routeObject), 1);
|
||||
};
|
||||
return removeFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,4 +102,19 @@ export class SmartRouter {
|
||||
} as IRouteInfo); // not waiting here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public selectionDimensionsMap = new plugins.lik.ObjectMap<SelectionDimension>();
|
||||
public async createSelectionDimension(optionsArg: {
|
||||
routeArg: string,
|
||||
keyArg: string,
|
||||
options: ISelectionOption[],
|
||||
peers?: SelectionDimension[],
|
||||
}) {
|
||||
const selectionDimension = new SelectionDimension();
|
||||
selectionDimension.dimensionKey = optionsArg.keyArg;
|
||||
selectionDimension.dimensionPeers = optionsArg.peers;
|
||||
selectionDimension.selectionOptions = optionsArg.options;
|
||||
await this.selectionDimensionsMap.findOneAndRemove(async dimensionArg => dimensionArg.dimensionKey === optionsArg.keyArg);
|
||||
this.selectionDimensionsMap.addMappedUnique(optionsArg.keyArg, selectionDimension);
|
||||
}
|
||||
}
|
@ -1,3 +1,13 @@
|
||||
// @push.rocks scope
|
||||
import * as lik from '@push.rocks/lik';
|
||||
import * as smartrx from '@push.rocks/smartrx';
|
||||
|
||||
export {
|
||||
lik,
|
||||
smartrx,
|
||||
}
|
||||
|
||||
// third party scope
|
||||
import * as pathToRegExp from 'path-to-regexp';
|
||||
|
||||
export { pathToRegExp };
|
||||
|
Reference in New Issue
Block a user