ddd-team1 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddd-team1",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "ddd-team1": "./src/infrastructure/tui/index.tsx"
@@ -9,7 +9,7 @@ export class MakeMoveUseCase {
9
9
  constructor(private repo: GameRepository) {}
10
10
 
11
11
  async execute(gameId: Game['id'], standardAlgebraicNotation: string): Promise<void> {
12
- let game = await this.repo.findById(gameId) ?? new Game(gameId);
12
+ const game = await this.repo.findById(gameId) ?? new Game(gameId);
13
13
  const expectedVersion = game.version;
14
14
  const command = {
15
15
  name: 'Make move!' as const,
@@ -1,6 +1,6 @@
1
1
  import type { UUID } from 'crypto';
2
2
  import type { Command } from './Command.ts';
3
- import type { DomainEvent, GameStartedEvent, MoveMadeEvent } from './DomainEvent.ts';
3
+ import type { DomainEvent } from './DomainEvent.ts';
4
4
  import { AggregateRoot } from './shared';
5
5
  import { Board } from './value-objects/Board.ts';
6
6
  import { StandardAlgebraicNotationMove } from './value-objects/StandardAlgebraicNotationMove.ts';
@@ -14,23 +14,22 @@ export class Game extends AggregateRoot<UUID> {
14
14
 
15
15
  static fromEvents(id: UUID, events: DomainEvent[]): Game {
16
16
  const game = new Game(id);
17
- game.replay(events);
18
- game.setVersion(events.filter(e => e.aggregateId === id).length);
17
+ game.rehydrate(events.filter(e => e.aggregateId === id));
19
18
  return game;
20
19
  }
21
20
 
22
- replay(events: DomainEvent[]): void {
23
- events
24
- .filter(event => event.aggregateId === this.id)
25
- .forEach(event => {
26
- if (event.name === 'Move made.') {
27
- this.board = this.board.play(StandardAlgebraicNotationMove.fromString(event.payload));
28
- }
29
- });
21
+ protected apply(event: DomainEvent): void {
22
+ switch (event.name) {
23
+ case 'Game started.':
24
+ break;
25
+ case 'Move made.':
26
+ this.board = this.board.play(StandardAlgebraicNotationMove.fromString(event.payload));
27
+ break;
28
+ }
30
29
  }
31
30
 
32
- move(move: StandardAlgebraicNotationMove): DomainEvent[] {
33
- if (this.board.equals(new Board())) {
31
+ move(move: StandardAlgebraicNotationMove): void {
32
+ if (this.version === 0) {
34
33
  this.recordThat({
35
34
  name: 'Game started.',
36
35
  aggregateId: this.id,
@@ -42,14 +41,13 @@ export class Game extends AggregateRoot<UUID> {
42
41
  aggregateId: this.id,
43
42
  payload: move.toString(),
44
43
  });
45
- this.replay(this.newlyRecordEvents);
46
- return this.newlyRecordEvents;
47
44
  }
48
45
 
49
- execute(command: Command): DomainEvent[] {
46
+ execute(command: Command): void {
50
47
  switch (command.name) {
51
48
  case 'Make move!':
52
- return this.move(command.payload.move);
49
+ this.move(command.payload.move);
50
+ break;
53
51
  default:
54
52
  throw new Error(`Unknown command: ${command.name}`);
55
53
  }
@@ -3,28 +3,35 @@ import type { DomainEvent } from "../DomainEvent.ts";
3
3
 
4
4
  export abstract class AggregateRoot<T> extends Entity<T>
5
5
  {
6
- protected newlyRecordEvents: DomainEvent[] = [];
6
+ private _newlyRecordedEvents: DomainEvent[] = [];
7
7
  private _version = 0;
8
8
 
9
9
  get version(): number {
10
10
  return this._version;
11
11
  }
12
12
 
13
- flushEvents(): DomainEvent[] {
14
- const eventsToFlush = [...this.newlyRecordEvents];
15
- this.newlyRecordEvents = [];
16
- return eventsToFlush;
17
- }
18
-
19
13
  constructor(id: T){
20
14
  super(id);
21
15
  }
22
16
 
23
- protected setVersion(version: number): void {
24
- this._version = version;
17
+ protected abstract apply(event: DomainEvent): void;
18
+
19
+ protected recordThat(event: DomainEvent): void {
20
+ this.apply(event);
21
+ this._version++;
22
+ this._newlyRecordedEvents.push(event);
25
23
  }
26
24
 
27
- recordThat(event: DomainEvent): void {
28
- this.newlyRecordEvents.push(event);
25
+ rehydrate(events: DomainEvent[]): void {
26
+ for (const event of events) {
27
+ this.apply(event);
28
+ this._version++;
29
+ }
30
+ }
31
+
32
+ flushEvents(): DomainEvent[] {
33
+ const eventsToFlush = [...this._newlyRecordedEvents];
34
+ this._newlyRecordedEvents = [];
35
+ return eventsToFlush;
29
36
  }
30
37
  }