1
- import { DiscoveryService } from '@golevelup/nestjs-discovery' ;
2
1
import { Injectable , type OnApplicationBootstrap } from '@nestjs/common' ;
3
2
import {
4
3
type FnLike ,
@@ -8,13 +7,10 @@ import {
8
7
} from '@seedcompany/common' ;
9
8
import { stripIndent } from 'common-tags' ;
10
9
import { type ID , ServerException } from '~/common' ;
10
+ import { MetadataDiscovery } from '~/core/discovery' ;
11
11
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' ;
18
14
19
15
/**
20
16
* An event bus for internal use.
@@ -30,7 +26,7 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
30
26
private listenerMap : Record < ID , FnLike [ ] > = { } ;
31
27
32
28
constructor (
33
- private readonly discovery : DiscoveryService ,
29
+ private readonly discovery : MetadataDiscovery ,
34
30
@Logger ( 'event-bus' ) private readonly logger : ILogger ,
35
31
) { }
36
32
@@ -59,15 +55,14 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
59
55
}
60
56
61
57
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 > > ( ) ;
66
61
67
62
if ( process . env . NODE_ENV !== 'production' ) {
68
63
const defined = new Set < string > ( ) ;
69
64
for ( const entry of discovered ) {
70
- const name = entry . discoveredClass . name ;
65
+ const name = entry . instance . constructor . name ;
71
66
if ( defined . has ( name ) ) {
72
67
throw new ServerException ( stripIndent `
73
68
Event handler "${ name } " has already been defined.
@@ -78,10 +73,9 @@ export class SyncEventBus implements IEventBus, OnApplicationBootstrap {
78
73
}
79
74
}
80
75
81
- const flat = discovered . flatMap ( ( entry ) => {
82
- const instance = entry . discoveredClass . instance as IEventHandler < any > ;
76
+ const flat = discovered . flatMap ( ( { instance, meta } ) => {
83
77
const handler = instance . handle . bind ( instance ) ;
84
- return [ ...entry . meta ] . map ( ( [ id , priority ] ) => ( {
78
+ return [ ...meta ] . map ( ( [ id , priority ] ) => ( {
85
79
id,
86
80
priority,
87
81
handler,
0 commit comments