Skip to content

Commit fb0027f

Browse files
committed
Migrate EventsHandler
1 parent 24e033e commit fb0027f

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

src/core/events/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { type ID } from '~/common';
2-
31
export const EVENT_METADATA = '__event__';
4-
export const EVENTS_HANDLER_METADATA = '__eventsHandler__';
5-
6-
export type EventHandlerMetadata = Map<ID, Priority>;
72

83
/**
94
* Higher goes first.

src/core/events/event-bus.service.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DiscoveryService } from '@golevelup/nestjs-discovery';
21
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
32
import {
43
type FnLike,
@@ -8,13 +7,10 @@ import {
87
} from '@seedcompany/common';
98
import { stripIndent } from 'common-tags';
109
import { type ID, ServerException } from '~/common';
10+
import { MetadataDiscovery } from '~/core/discovery';
1111
import { ILogger, Logger } from '../logger';
12-
import {
13-
EVENT_METADATA,
14-
type EventHandlerMetadata,
15-
EVENTS_HANDLER_METADATA,
16-
} from './constants';
17-
import { type IEventHandler } from './event-handler.decorator';
12+
import { EVENT_METADATA } from './constants';
13+
import { EventsHandler, type IEventHandler } from './event-handler.decorator';
1814

1915
/**
2016
* An event bus for internal use.
@@ -30,7 +26,7 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
3026
private listenerMap: Record<ID, FnLike[]> = {};
3127

3228
constructor(
33-
private readonly discovery: DiscoveryService,
29+
private readonly discovery: MetadataDiscovery,
3430
@Logger('event-bus') private readonly logger: ILogger,
3531
) {}
3632

@@ -59,15 +55,14 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
5955
}
6056

6157
async onApplicationBootstrap() {
62-
const discovered =
63-
await this.discovery.providersWithMetaAtKey<EventHandlerMetadata>(
64-
EVENTS_HANDLER_METADATA,
65-
);
58+
const discovered = this.discovery
59+
.discover(EventsHandler)
60+
.classes<IEventHandler<any>>();
6661

6762
if (process.env.NODE_ENV !== 'production') {
6863
const defined = new Set<string>();
6964
for (const entry of discovered) {
70-
const name = entry.discoveredClass.name;
65+
const name = entry.instance.constructor.name;
7166
if (defined.has(name)) {
7267
throw new ServerException(stripIndent`
7368
Event handler "${name}" has already been defined.
@@ -78,10 +73,9 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
7873
}
7974
}
8075

81-
const flat = discovered.flatMap((entry) => {
82-
const instance = entry.discoveredClass.instance as IEventHandler<any>;
76+
const flat = discovered.flatMap(({ instance, meta }) => {
8377
const handler = instance.handle.bind(instance);
84-
return [...entry.meta].map(([id, priority]) => ({
78+
return [...meta].map(([id, priority]) => ({
8579
id,
8680
priority,
8781
handler,

src/core/events/event-handler.decorator.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { type Type } from '@nestjs/common';
2+
import { createMetadataDecorator } from '@seedcompany/nest';
23
import { nanoid } from 'nanoid';
34
import { type ID } from '~/common';
4-
import {
5-
EVENT_METADATA,
6-
type EventHandlerMetadata,
7-
EVENTS_HANDLER_METADATA,
8-
type Priority,
9-
} from './constants';
5+
import { EVENT_METADATA, type Priority } from './constants';
106

117
/**
128
* Subscribe to these given events.
@@ -25,10 +21,10 @@ import {
2521
* )
2622
* ```
2723
*/
28-
export const EventsHandler =
29-
(...events: Array<Type | [...Type[], Priority]>): ClassDecorator =>
30-
(target) => {
31-
const metadata: EventHandlerMetadata = new Map<ID, Priority>();
24+
export const EventsHandler = createMetadataDecorator({
25+
types: ['class'],
26+
setter: (...events: Array<Type | [...Type[], Priority]>) => {
27+
const metadata = new Map<ID, Priority>();
3228

3329
for (const arg of events) {
3430
let priority: Priority = 0;
@@ -45,8 +41,9 @@ export const EventsHandler =
4541
}
4642
}
4743

48-
Reflect.defineMetadata(EVENTS_HANDLER_METADATA, metadata, target);
49-
};
44+
return metadata;
45+
},
46+
});
5047

5148
// eslint-disable-next-line @typescript-eslint/naming-convention
5249
export interface IEventHandler<T> {

0 commit comments

Comments
 (0)