Skip to content

Commit 71a896f

Browse files
committed
wip: subscribe to symbol
1 parent 762b037 commit 71a896f

6 files changed

Lines changed: 49 additions & 47 deletions

File tree

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export abstract class Command {
2-
static COMMAND_NAME: string;
3-
}
1+
export abstract class Command {}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Command } from "./Command";
1+
import { Command } from './Command';
22

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

src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
import { CommandBus } from './../../domain/CommandBus';
21
import { Command } from '../../domain/Command';
2+
import { CommandBus } from './../../domain/CommandBus';
3+
import { CommandHandlersInformation } from './CommandHandlersInformation';
34
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-
}
275

286
export class InMemoryCommandBus implements CommandBus {
29-
constructor(private commandHandlersInformation: CommandHandlersInformation) {
30-
}
7+
constructor(private commandHandlersInformation: CommandHandlersInformation) {}
318

329
dispatch(command: Command): void {
33-
const handler = this.commandHandlersInformation.getHandler(command);
10+
const handler = this.commandHandlersInformation.getCommandHandler(command);
3411

3512
if (!handler) {
3613
throw new NoHandlerForMessageError(command);

src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { Command } from '../../domain/Command';
22

33
export class NoHandlerForMessageError extends Error {
44
constructor(command: Command) {
5-
super(`There is not handler for command of type ${command.commandName}`);
5+
super(`There is not handler for command of type ${command.constructor.name}`);
66
}
7-
}
7+
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { InMemoryCommandBus } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus';
21
import { Command } from '../../../../../src/Contexts/Shared/domain/Command';
2+
import { CommandHandler } from '../../../../../src/Contexts/Shared/domain/CommandHandler';
3+
import { CommandHandlersInformation } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation';
4+
import { InMemoryCommandBus } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus';
35
import { NoHandlerForMessageError } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError';
46

57
class UnhandledCommand extends Command {
@@ -10,36 +12,38 @@ class HandledCommand extends Command {
1012
static COMMAND_NAME = 'handled.command';
1113
}
1214

13-
interface CommandHandler {
14-
handle(command: Command): void;
15-
}
15+
class MyCommandHandler implements CommandHandler<HandledCommand> {
16+
subscribedTo(): HandledCommand {
17+
return HandledCommand;
18+
}
1619

17-
class MyCommandHandler implements CommandHandler {
1820
handle(command: HandledCommand): void {}
1921
}
2022

2123
describe('InMemoryCommandBus', () => {
22-
it('throws an error if dispatches a command without handler', (done) => {
24+
it('throws an error if dispatches a command without handler', done => {
2325
const unhandledCommand = new UnhandledCommand();
24-
const commandBus = new InMemoryCommandBus();
26+
const commandHandlersInformation = new CommandHandlersInformation([]);
27+
const commandBus = new InMemoryCommandBus(commandHandlersInformation);
2528

2629
try {
2730
commandBus.dispatch(unhandledCommand);
2831
} catch (error) {
2932
expect(error).toBeInstanceOf(NoHandlerForMessageError);
30-
expect(error.message).toBe('There is not handler for command of type UnhandledCommandName');
33+
expect(error.message).toBe('There is not handler for command of type UnhandledCommand');
3134
done();
3235
}
3336
});
3437

3538
it('accepts a command with handler', done => {
3639
const handledCommand = new HandledCommand();
3740
const myCommandHandler = new MyCommandHandler();
38-
const commandBus = new InMemoryCommandBus([myCommandHandler]);
41+
const commandHandlersInformation = new CommandHandlersInformation([myCommandHandler]);
42+
const commandBus = new InMemoryCommandBus(commandHandlersInformation);
3943

4044
try {
4145
commandBus.dispatch(handledCommand);
4246
done();
43-
} catch (error) { }
47+
} catch (error) {}
4448
});
4549
});

0 commit comments

Comments
 (0)