Skip to content

Commit 762b037

Browse files
committed
WIP
1 parent a2874ee commit 762b037

4 files changed

Lines changed: 66 additions & 10 deletions

File tree

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
export abstract class Command {
2-
readonly commandName: string;
3-
4-
constructor(commandName: string) {
5-
this.commandName = commandName;
6-
}
2+
static COMMAND_NAME: string;
73
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Command } from "./Command";
2+
3+
export interface CommandHandler<T extends Command> {
4+
subscribedTo(): T;
5+
6+
handle(command: T): void;
7+
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import { CommandBus } from './../../domain/CommandBus';
22
import { Command } from '../../domain/Command';
33
import { NoHandlerForMessageError } from './NoHandlerForMessageError';
4+
import { CommandHandler } from '../../domain/CommandHandler';
5+
6+
export class CommandHandlersInformation {
7+
private commandHandlersMap: Map<string, CommandHandler<Command>>;
8+
9+
constructor(commandHandlers: Array<CommandHandler<Command>>) {
10+
this.commandHandlersMap = this.formatHandlers(commandHandlers);
11+
}
12+
13+
private formatHandlers(commandHandlers: Array<CommandHandler<Command>>) {
14+
const handlersMap = new Map();
15+
16+
commandHandlers.forEach(commandHandler => {
17+
handlersMap.set(commandHandler.subscribedTo(), commandHandler);
18+
});
19+
20+
return handlersMap;
21+
}
22+
23+
public getHandler(command: Command) {
24+
return this.commandHandlersMap.get(command.constructor.name);
25+
}
26+
}
427

528
export class InMemoryCommandBus implements CommandBus {
29+
constructor(private commandHandlersInformation: CommandHandlersInformation) {
30+
}
31+
632
dispatch(command: Command): void {
7-
throw new NoHandlerForMessageError(command);
33+
const handler = this.commandHandlersInformation.getHandler(command);
34+
35+
if (!handler) {
36+
throw new NoHandlerForMessageError(command);
37+
}
38+
39+
handler.handle(command);
840
}
9-
}
41+
}

tests/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ import { Command } from '../../../../../src/Contexts/Shared/domain/Command';
33
import { NoHandlerForMessageError } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError';
44

55
class UnhandledCommand extends Command {
6-
constructor() {
7-
super('UnhandledCommandName');
8-
}
6+
static COMMAND_NAME = 'unhandled.command';
7+
}
8+
9+
class HandledCommand extends Command {
10+
static COMMAND_NAME = 'handled.command';
11+
}
12+
13+
interface CommandHandler {
14+
handle(command: Command): void;
15+
}
16+
17+
class MyCommandHandler implements CommandHandler {
18+
handle(command: HandledCommand): void {}
919
}
1020

1121
describe('InMemoryCommandBus', () => {
@@ -21,4 +31,15 @@ describe('InMemoryCommandBus', () => {
2131
done();
2232
}
2333
});
34+
35+
it('accepts a command with handler', done => {
36+
const handledCommand = new HandledCommand();
37+
const myCommandHandler = new MyCommandHandler();
38+
const commandBus = new InMemoryCommandBus([myCommandHandler]);
39+
40+
try {
41+
commandBus.dispatch(handledCommand);
42+
done();
43+
} catch (error) { }
44+
});
2445
});

0 commit comments

Comments
 (0)