Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DomainEvents memory leak risk #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,735 changes: 1,390 additions & 1,345 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"cls-hooked": "^4.2.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"helmet": "^3.21.1",
"morgan": "^1.9.1",
"mysql2": "^1.6.5",
Expand All @@ -25,6 +27,7 @@
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/bcrypt-nodejs": "0.0.30",
"@types/cls-hooked": "^4.3.0",
"@types/jest": "^24.0.15",
"cross-env": "^5.2.0",
"dotenv-cli": "^2.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

const isProduction = process.env.NODE_ENV === 'production';

const clsName = "com.whitelabel";

export {
isProduction
isProduction,
clsName
}
3 changes: 2 additions & 1 deletion src/core/domain/AggregateRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export abstract class AggregateRoot<T> extends Entity<T> {
this._domainEvents.push(domainEvent);
// Add this aggregate instance to the domain event's list of aggregates who's
// events it eventually needs to dispatch.
DomainEvents.markAggregateForDispatch(this);
const domainEventsPublisher = DomainEvents.create();
domainEventsPublisher.markAggregateForDispatch(this);
// Log the domain event
this.logDomainEventAdded(domainEvent);
}
Expand Down
37 changes: 26 additions & 11 deletions src/core/domain/events/DomainEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import { IDomainEvent } from "./IDomainEvent";
import { AggregateRoot } from "../AggregateRoot";
import { UniqueEntityID } from "../UniqueEntityID";
import { getNamespace } from "cls-hooked";
import { clsName } from "../../../config";

export class DomainEvents {
private static handlersMap = {};
private static markedAggregates: AggregateRoot<any>[] = [];
private handlersMap = {};
private markedAggregates: AggregateRoot<any>[] = [];

/**
* @method markAggregateForDispatch
Expand All @@ -15,24 +17,24 @@ export class DomainEvents {
* the unit of work.
*/

public static markAggregateForDispatch (aggregate: AggregateRoot<any>): void {
public markAggregateForDispatch (aggregate: AggregateRoot<any>): void {
const aggregateFound = !!this.findMarkedAggregateByID(aggregate.id);

if (!aggregateFound) {
this.markedAggregates.push(aggregate);
}
}

private static dispatchAggregateEvents (aggregate: AggregateRoot<any>): void {
private dispatchAggregateEvents (aggregate: AggregateRoot<any>): void {
aggregate.domainEvents.forEach((event: IDomainEvent) => this.dispatch(event));
}

private static removeAggregateFromMarkedDispatchList (aggregate: AggregateRoot<any>): void {
private removeAggregateFromMarkedDispatchList (aggregate: AggregateRoot<any>): void {
const index = this.markedAggregates.findIndex((a) => a.equals(aggregate));
this.markedAggregates.splice(index, 1);
}

private static findMarkedAggregateByID (id: UniqueEntityID): AggregateRoot<any> {
private findMarkedAggregateByID (id: UniqueEntityID): AggregateRoot<any> {
let found: AggregateRoot<any> = null;
for (let aggregate of this.markedAggregates) {
if (aggregate.id.equals(id)) {
Expand All @@ -43,7 +45,7 @@ export class DomainEvents {
return found;
}

public static dispatchEventsForAggregate (id: UniqueEntityID): void {
public dispatchEventsForAggregate (id: UniqueEntityID): void {
const aggregate = this.findMarkedAggregateByID(id);

if (aggregate) {
Expand All @@ -53,22 +55,22 @@ export class DomainEvents {
}
}

public static register(callback: (event: IDomainEvent) => void, eventClassName: string): void {
public register(callback: (event: IDomainEvent) => void, eventClassName: string): void {
if (!this.handlersMap.hasOwnProperty(eventClassName)) {
this.handlersMap[eventClassName] = [];
}
this.handlersMap[eventClassName].push(callback);
}

public static clearHandlers(): void {
public clearHandlers(): void {
this.handlersMap = {};
}

public static clearMarkedAggregates(): void {
public clearMarkedAggregates(): void {
this.markedAggregates = [];
}

private static dispatch (event: IDomainEvent): void {
private dispatch (event: IDomainEvent): void {
const eventClassName: string = event.constructor.name;

if (this.handlersMap.hasOwnProperty(eventClassName)) {
Expand All @@ -78,4 +80,17 @@ export class DomainEvents {
}
}
}

public static create(): DomainEvents {
const ns = getNamespace(clsName);
const key = "DomainEvents";

let model = ns.get(key);
if (model === undefined) {
model = new DomainEvents();
ns.set(key, model);
}

return model;
}
}
Loading