25 lines
592 B
JavaScript
25 lines
592 B
JavaScript
|
|
export default class Repository {
|
||
|
|
#entityClass = undefined;
|
||
|
|
#actions = new Map();
|
||
|
|
constructor(config) {
|
||
|
|
this.#entityClass = config.entityClass;
|
||
|
|
}
|
||
|
|
|
||
|
|
create(entityData) {
|
||
|
|
return new this.#entityClass(entityData);
|
||
|
|
}
|
||
|
|
|
||
|
|
perform(entity, actionName, ...args) {
|
||
|
|
if(!this.#actions.has(actionName)) throw new Error(`Action '${actionName}' is not registered.`);
|
||
|
|
return this.#actions.get(actionName)(entity, ...args);
|
||
|
|
}
|
||
|
|
|
||
|
|
register(actionName, actionFn) {
|
||
|
|
this.#actions.set(actionName, actionFn);
|
||
|
|
}
|
||
|
|
|
||
|
|
getActions() {
|
||
|
|
return Array.from(this.#actions.keys());
|
||
|
|
}
|
||
|
|
}
|