Skip to content

Commit

Permalink
chore: fix type after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
tgallacher committed May 18, 2023
1 parent 6fa00e5 commit 5689e7e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ module.exports = {
'@typescript-eslint/no-shadow': 'error',
// Enables TSDoc linting.
'tsdoc/syntax': 'warn',
'@typescript-eslint/unbound-method': [
'error',
{
ignoreStatic: true,
},
],
},
},
{
Expand Down
7 changes: 6 additions & 1 deletion src/aggregate-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export interface IAggregateRoot extends IEntity {
* - External objects can not hold reference to objects within the Aggregate boundary; they can only access them transiently via the Aggregate Root
* - Delete operations should remove _everything_ within the `Aggregate` boundary;
*/
export abstract class AggregateRoot<T>
export abstract class AggregateRoot<
T extends Partial<{ tenantId: string; id: string }> = {
tenantId: string;
id: string;
},
>
extends Entity<T>
implements IAggregateRoot
{
Expand Down
52 changes: 38 additions & 14 deletions src/domain-events-broker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class RegisterSingleEventTestAggregate extends AggregateRoot<void> {
class RegisterSingleEventTestAggregate extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
}

const myEvent = new PrimaryStubEvent(stubEventContext);
const myAggregrate = new RegisterSingleEventTestAggregate();
const myAggregrate = new RegisterSingleEventTestAggregate({
id: '1',
tenantId: '1',
});

myAggregrate.addEvent(myEvent);

Expand All @@ -47,7 +50,7 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class RegisterMultpleEventTestAggregateA extends AggregateRoot<void> {
class RegisterMultpleEventTestAggregateA extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
Expand All @@ -56,8 +59,14 @@ describe('Aggregate DomainEvents', () => {

const myEventA = new PrimaryStubEvent(stubEventContext);
const myEventB = new PrimaryStubEvent(stubEventContext);
const aggregateA = new RegisterMultpleEventTestAggregateA();
const aggregateB = new RegisterMultpleEventTestAggregateB();
const aggregateA = new RegisterMultpleEventTestAggregateA({
id: '1',
tenantId: '1',
});
const aggregateB = new RegisterMultpleEventTestAggregateB({
id: '2',
tenantId: '2',
});

aggregateA.addEvent(myEventA);
aggregateB.addEvent(myEventB);
Expand All @@ -71,15 +80,18 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class RegisterSingleEventTestAggregate extends AggregateRoot<void> {
class RegisterSingleEventTestAggregate extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
}

const myEventA = new PrimaryStubEvent(stubEventContext);
const myEventB = new SecondaryStubEvent(stubEventContext);
const myAggregrate = new RegisterSingleEventTestAggregate();
const myAggregrate = new RegisterSingleEventTestAggregate({
id: '1',
tenantId: '1',
});

myAggregrate.addEvent(myEventA);
myAggregrate.addEvent(myEventB);
Expand All @@ -95,14 +107,17 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class DispatchEventsTestAggregate extends AggregateRoot<void> {
class DispatchEventsTestAggregate extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
}

const myEvent = new PrimaryStubEvent(stubEventContext);
const myAggregrate = new DispatchEventsTestAggregate();
const myAggregrate = new DispatchEventsTestAggregate({
id: '1',
tenantId: '1',
});
const eventHandler = jest.fn();

DomainEventsBroker.registerEventHandler(
Expand All @@ -119,15 +134,18 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class DispatchEventsTestAggregateA extends AggregateRoot<void> {
class DispatchEventsTestAggregateA extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
}

const myEventA = new PrimaryStubEvent(stubEventContext);

const myAggregrateA = new DispatchEventsTestAggregateA();
const myAggregrateA = new DispatchEventsTestAggregateA({
id: '1',
tenantId: '1',
});

const eventHandlerA = jest.fn();
const eventHandlerB = jest.fn();
Expand All @@ -153,7 +171,7 @@ describe('Aggregate DomainEvents', () => {
const { AggregateRoot } = await import('./aggregate-root');
const { DomainEventsBroker } = await import('./domain-events-broker');

class DispatchEventsTestAggregateA extends AggregateRoot<void> {
class DispatchEventsTestAggregateA extends AggregateRoot {
public addEvent(event: IDomainEvent): void {
this.addDomainEvent(event);
}
Expand All @@ -163,8 +181,14 @@ describe('Aggregate DomainEvents', () => {
const myEventA = new PrimaryStubEvent(stubEventContext);
const myEventB = new SecondaryStubEvent(stubEventContext);

const myAggregrateA = new DispatchEventsTestAggregateA();
const myAggregrateB = new DispatchEventsTestAggregateB();
const myAggregrateA = new DispatchEventsTestAggregateA({
id: '1',
tenantId: '1',
});
const myAggregrateB = new DispatchEventsTestAggregateB({
id: '2',
tenantId: '2',
});

const eventHandlerA = jest.fn();
const eventHandlerB = jest.fn();
Expand Down
27 changes: 15 additions & 12 deletions src/domain-events-broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ export class DomainEventsBroker {
private static findRegisteredAggregateById(
id: string,
): IAggregateRoot | null {
return this.aggregatesWithEvents.find((agg) => agg.id === id) ?? null;
return (
DomainEventsBroker.aggregatesWithEvents.find((agg) => agg.id === id) ??
null
);
}

private static unregisterAggregate(aggregate: IAggregateRoot): void {
const aggIdx = this.aggregatesWithEvents.findIndex((agg) =>
const aggIdx = DomainEventsBroker.aggregatesWithEvents.findIndex((agg) =>
agg.equals(aggregate),
);

if (aggIdx !== -1) {
this.aggregatesWithEvents.splice(aggIdx, 1);
DomainEventsBroker.aggregatesWithEvents.splice(aggIdx, 1);
}
}

private static dispatchEvents(aggregate: IAggregateRoot): void {
aggregate.domainEvents.forEach((event) => {
const eventName = event.constructor.name;

this.eventHandlers.get(eventName)?.forEach((handle) => {
DomainEventsBroker.eventHandlers.get(eventName)?.forEach((handle) => {
// eslint-disable-next-line no-void
void handle(event);
});
Expand All @@ -44,9 +47,9 @@ export class DomainEventsBroker {
eventName: string,
handler: DomainEventHandler,
): void {
this.eventHandlers.set(
DomainEventsBroker.eventHandlers.set(
eventName,
(this.eventHandlers.get(eventName) || []).concat(handler),
(DomainEventsBroker.eventHandlers.get(eventName) || []).concat(handler),
);
}

Expand All @@ -58,28 +61,28 @@ export class DomainEventsBroker {
const foundAggregate = this.findRegisteredAggregateById(aggregate.id);

if (!foundAggregate) {
this.aggregatesWithEvents.push(aggregate);
DomainEventsBroker.aggregatesWithEvents.push(aggregate);
}
}

public static dispatchAggregateEvents(aggregate: IAggregateRoot): void {
// ensure the provided aggregate has been registered for dispatch
const found = this.findRegisteredAggregateById(aggregate.id);
const found = DomainEventsBroker.findRegisteredAggregateById(aggregate.id);

if (found) {
this.dispatchEvents(found);
DomainEventsBroker.dispatchEvents(found);

found.clearDomainEvents();

this.unregisterAggregate(found);
DomainEventsBroker.unregisterAggregate(found);
}
}

public static clearEventHandlers(): void {
this.eventHandlers = new Map();
DomainEventsBroker.eventHandlers = new Map();
}

public static clearRegisteredAggregates(): void {
this.aggregatesWithEvents = [];
DomainEventsBroker.aggregatesWithEvents = [];
}
}

0 comments on commit 5689e7e

Please sign in to comment.