diff --git a/src/event-bus.ts b/src/event-bus.ts index 6c951b62..7c62d349 100644 --- a/src/event-bus.ts +++ b/src/event-bus.ts @@ -28,7 +28,7 @@ export type EventHandlerType = Type< export class EventBus extends ObservableBus implements IEventBus, OnModuleDestroy { - protected getEventId: (event: EventBase) => string; + protected getEventId: (event: EventBase) => string | null; protected readonly subscriptions: Subscription[]; private _publisher: IEventPublisher; @@ -102,9 +102,9 @@ export class EventBus ); } - protected ofEventId(name: string) { + protected ofEventId(id: string) { return this.subject$.pipe( - filter((event) => this.getEventId(event) === name), + filter((event) => this.getEventId(event) === id), ); } diff --git a/src/helpers/default-get-event-id.ts b/src/helpers/default-get-event-id.ts index cd80c032..468f7ae3 100644 --- a/src/helpers/default-get-event-id.ts +++ b/src/helpers/default-get-event-id.ts @@ -2,11 +2,16 @@ import { IEvent } from '../interfaces'; import { EVENT_METADATA } from '../decorators/constants'; import { Type } from '@nestjs/common'; +/** + * Null if the published class is not connected to any handler + * @param event + * @returns + */ export const defaultGetEventId = ( event: EventBase, ): string => { const { constructor } = Object.getPrototypeOf(event); - return Reflect.getMetadata(EVENT_METADATA, constructor).id; + return Reflect.getMetadata(EVENT_METADATA, constructor)?.id ?? null; }; export const defaultReflectEventId = <