centralize handlers, allow multiple handlers per model

This commit is contained in:
marlon-erler
2024-07-23 01:58:34 +02:00
parent 5483c46ca1
commit 68dae8099a
7 changed files with 76 additions and 58 deletions
+23
View File
@@ -5,6 +5,29 @@ export function createTimestamp(): string {
return new Date().toISOString();
}
// handlers
export type Handler<T> = (item: T) => void;
export class HandlerManager<T> {
handlers: Set<Handler<T>> = new Set();
// manage
addHandler = (handler: Handler<T>): void => {
this.handlers.add(handler);
};
deleteHandler = (handler: Handler<T>): void => {
this.handlers.delete(handler);
};
// trigger
trigger = (item: T): void => {
for (const handler of this.handlers) {
handler(item);
}
};
}
// string
export function stringify(data: any): string {
return JSON.stringify(data, null, 4);