Skip to content

Commit

Permalink
feat: pass context to publisher alongside events
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 8, 2023
1 parent 9eb110f commit 8c0b5c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,27 @@ export class EventBus<EventBase extends IEvent = IEvent>
* Publishes an event.
* @param event The event to publish.
*/
publish<T extends EventBase>(event: T) {
return this._publisher.publish(event);
publish<TEvent extends EventBase, TContext = unknown>(
event: TEvent,
context: TContext,
) {
return this._publisher.publish(event, context);
}

/**
* Publishes multiple events.
* @param events The events to publish.
*/
publishAll<T extends EventBase>(events: T[]) {
publishAll<TEvent extends EventBase, TContext = unknown>(
events: TEvent[],
context: TContext,
) {
if (this._publisher.publishAll) {
return this._publisher.publishAll(events);
}
return (events || []).map((event) => this._publisher.publish(event));
return (events || []).map((event) =>
this._publisher.publish(event, context),
);
}

bind(handler: IEventHandler<EventBase>, id: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/event-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class EventPublisher<EventBase extends IEvent = IEvent> {
const eventBus = this.eventBus;
return class extends metatype {
publish(event: EventBase) {
eventBus.publish(event);
eventBus.publish(event, this);
}

publishAll(events: EventBase[]) {
eventBus.publishAll(events);
eventBus.publishAll(events, this);
}
};
}
Expand All @@ -39,11 +39,11 @@ export class EventPublisher<EventBase extends IEvent = IEvent> {
mergeObjectContext<T extends AggregateRoot<EventBase>>(object: T): T {
const eventBus = this.eventBus;
object.publish = (event: EventBase) => {
eventBus.publish(event);
eventBus.publish(event, object);
};

object.publishAll = (events: EventBase[]) => {
eventBus.publishAll(events);
eventBus.publishAll(events, object);
};
return object;
}
Expand Down
12 changes: 10 additions & 2 deletions src/interfaces/events/event-bus.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ export interface IEventBus<EventBase extends IEvent = IEvent> {
/**
* Publishes an event.
* @param event The event to publish.
* @param context The context.
*/
publish<T extends EventBase>(event: T);
publish<TEvent extends EventBase, TContext = unknown>(
event: TEvent,
context?: TContext,
): any;

/**
* Publishes multiple events.
* @param events The events to publish.
* @param context The context.
*/
publishAll(events: EventBase[]);
publishAll<TEvent extends EventBase, TContext = unknown>(
events: TEvent[],
context?: TContext,
): any;
}
21 changes: 19 additions & 2 deletions src/interfaces/events/event-publisher.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { IEvent } from './event.interface';

export interface IEventPublisher<EventBase extends IEvent = IEvent> {
publish<T extends EventBase = EventBase>(event: T): any;
publishAll?<T extends EventBase = EventBase>(events: T[]): any;
/**
* Publishes an event.
* @param event The event to publish.
* @param context The context.
*/
publish<TEvent extends EventBase, TContext = unknown>(
event: TEvent,
context?: TContext,
): any;

/**
* Publishes multiple events.
* @param events The events to publish.
* @param context The context.
*/
publishAll?<TEvent extends EventBase, TContext = unknown>(
events: TEvent[],
context?: TContext,
): any;
}

0 comments on commit 8c0b5c5

Please sign in to comment.