Skip to content

Commit

Permalink
Merge pull request #926 from Sikora00/bugfix/924
Browse files Browse the repository at this point in the history
fix(event-bus): event id is optional
  • Loading branch information
kamilmysliwiec authored Feb 21, 2022
2 parents 7f2d920 + 6d59f82 commit 6d81a84
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type EventHandlerType<EventBase extends IEvent = IEvent> = Type<
export class EventBus<EventBase extends IEvent = IEvent>
extends ObservableBus<EventBase>
implements IEventBus<EventBase>, OnModuleDestroy {
protected getEventId: (event: EventBase) => string;
protected getEventId: (event: EventBase) => string | null;
protected readonly subscriptions: Subscription[];

private _publisher: IEventPublisher<EventBase>;
Expand Down Expand Up @@ -102,9 +102,9 @@ export class EventBus<EventBase extends IEvent = IEvent>
);
}

protected ofEventId(name: string) {
protected ofEventId(id: string) {
return this.subject$.pipe(
filter((event) => this.getEventId(event) === name),
filter((event) => this.getEventId(event) === id),
);
}

Expand Down
7 changes: 6 additions & 1 deletion src/helpers/default-get-event-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <EventBase extends IEvent = IEvent>(
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 = <
Expand Down

0 comments on commit 6d81a84

Please sign in to comment.