feat(core): Add selection dimensions and route removal functionality

This commit is contained in:
2024-10-06 06:57:15 +02:00
parent b2a11a5de2
commit bb30a6e8a8
6 changed files with 135 additions and 32 deletions

View File

@@ -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);
}
}