Mediator
About
interface Handler {
// The Event name
event: string;
// What must be executed when this Event happens
callback: Function
}
class Mediator {
// The handlers are the interested parties
handlers: Handler[];
constructor() {
this.handlers = [];
}
// Here we register events to the handlers and set what should execute when
// the event happens.
register(event: string, callback: Function) {
this.handlers.push({ event, callback });
}
// When a notify of a specific event is called, all handlers that subscribed
// to this event get notified.
async notify(event: string, data: any) {
for (const handler of this.handlers) {
if (handler.event === event) await handler.callback(data);
}
}
}Chaining Usecases Example
Notify from Application Layer
Notify from Domain Layer
Notify from Domain Layer - With Domain Event
Domain EventLast updated