dd-timer/src/application/handlers/QueryBus.ts
POL Mickaël 3e485fd09b chore: normalise fins de ligne CRLF → LF dans tout le repo
Applique .gitattributes sur tous les fichiers existants.
Élimine les différences fantômes entre WSL et Windows.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-06 08:55:10 +02:00

25 lines
651 B
TypeScript
Executable File

export interface Query {
readonly type: string;
readonly [key: string]: unknown;
}
type QueryHandler<T extends Query = Query, R = unknown> = (query: T) => R;
export class QueryBus {
private handlers = new Map<string, QueryHandler>();
register<T extends Query, R>(type: string, handler: QueryHandler<T, R>): void {
this.handlers.set(type, handler as QueryHandler);
}
execute<R>(query: Query): R {
const handler = this.handlers.get(query.type);
if (!handler) throw new Error(`No handler for query: ${query.type}`);
return handler(query) as R;
}
has(type: string): boolean {
return this.handlers.has(type);
}
}