-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.ts
54 lines (44 loc) · 1.45 KB
/
aggregate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { v4 as uuid } from "uuid";
import { DomainEvent } from "./event";
// todo mutex
// JavaScript is single-threaded (web-workers aside for a moment), nothing happens asynchronously (or everything for that matter) - all code: event handlers, timeouts, callbacks, etc. - run in the same thread, one after another.
// Thus you don't need any synchronization in JavaScript. Any given piece of code in JavaScript is guaranteed to be executed by only a single thread. How cool is that?
// todo nocopy
function NewAggregateId(): string {
return uuid();
}
type aggregateChange = {};
abstract class AbstractAggregate {
readonly Id: string;
private domainEvents: DomainEvent[];
constructor() {
this.Id = NewAggregateId();
this.domainEvents = [];
}
Apply(aggregateChange: aggregateChange) {
let eventName = this.Apply.caller.name;
if (eventName == "") {
throw new Error("aggregate apply failed, eventName is empty");
}
const domainEvent = new DomainEvent(
this.Id,
this.constructor.name,
NewAggregateId(),
eventName,
aggregateChange
);
let err = handleAppliedDomainEvent(agg, domainEvent);
if (err != null) {
throw new Error("aggregate apply failed, apply domain event failed");
}
this.domainEvents.push(domainEvent);
return;
}
Applied(): DomainEvent[] {
return this.domainEvents;
}
cleanDomainEvents() {
this.domainEvents = [];
}
}
export { AbstractAggregate };