mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
feat(core): add module scaffolding and provider registries
This commit is contained in:
21
src/core/action-bus.ts
Normal file
21
src/core/action-bus.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export type ActionWithType = { type: string };
|
||||
|
||||
export type ActionHandler<TAction extends ActionWithType> = (
|
||||
action: TAction,
|
||||
) => void | Promise<void>;
|
||||
|
||||
export class ActionBus<TAction extends ActionWithType> {
|
||||
private handlers = new Map<string, ActionHandler<TAction>>();
|
||||
|
||||
register(type: TAction["type"], handler: ActionHandler<TAction>): void {
|
||||
this.handlers.set(type, handler);
|
||||
}
|
||||
|
||||
async dispatch(action: TAction): Promise<void> {
|
||||
const handler = this.handlers.get(action.type);
|
||||
if (!handler) {
|
||||
throw new Error(`No handler registered for action: ${action.type}`);
|
||||
}
|
||||
await handler(action);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user