Skip to content

Commit ff89ed2

Browse files
committed
feat: add module options
1 parent 9a37953 commit ff89ed2

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/bus.spec.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable, Scope } from '@nestjs/common';
22
import { Test } from '@nestjs/testing';
3-
import { DefaultEventEmitter, Emitter, EventBus, EventBusModule, From, On } from './index';
3+
import { DefaultEventEmitter, Emitter, EventBus, EventBusModule, EventBusModuleOptions, From, On } from './index';
44

55
@Emitter('mocked')
66
class MockEmitter extends DefaultEventEmitter {}
@@ -59,12 +59,17 @@ class MockOn {
5959
onTestFromMocked(payload: any) {
6060
this.calls('mocked', payload);
6161
}
62+
63+
@On('test.*')
64+
onTestWildcard(payload: any) {
65+
this.calls('test.*', payload);
66+
}
6267
}
6368

6469
describe('Module Tests', () => {
65-
async function setup() {
70+
async function setup(config?: EventBusModuleOptions) {
6671
const module = Test.createTestingModule({
67-
imports: [EventBusModule.forRoot()],
72+
imports: [EventBusModule.forRoot(config)],
6873
providers: [MockEmitter, MockOn, MockOnRequest]
6974
});
7075
return module.compile().then(module => module.init());
@@ -126,4 +131,19 @@ describe('Module Tests', () => {
126131
const actual = module.get(EventBus).emitAsync('rethrow', { _: 'error' });
127132
await expect(actual).rejects.toThrow('rethrow');
128133
});
134+
135+
it('should work with evementemitter2 options (wildcard)', async () => {
136+
const module = await setup({
137+
wildcard: true,
138+
delimiter: '.'
139+
});
140+
141+
await module.get(EventBus).emitAsync('test.one', { _: '1' });
142+
await module.get(EventBus).emitAsync('test.two', { _: '2' });
143+
144+
const actual = module.get(MockOn).calls;
145+
expect(actual).toBeCalledTimes(2);
146+
expect(actual).toBeCalledWith('test.*', { _: '1' });
147+
expect(actual).toBeCalledWith('test.*', { _: '2' });
148+
});
129149
});

src/definitions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { EventEmitter2 } from 'eventemitter2';
1+
import { Inject, Optional } from '@nestjs/common';
2+
import { ConstructorOptions, EventEmitter2 } from 'eventemitter2';
23

34
export type IEventEmitter = Pick<EventEmitter2, 'emitAsync' | 'on' | 'off'>;
4-
export { EventEmitter2 as DefaultEventEmitter };
5+
6+
7+
export class DefaultEventEmitter extends EventEmitter2 implements IEventEmitter {
8+
constructor(@Optional() @Inject('Options') options?: ConstructorOptions) {
9+
super(options);
10+
}
11+
}

src/module.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import { DynamicModule, Module } from '@nestjs/common';
22
import { DiscoveryModule } from '@nestjs/core';
3+
import { ConstructorOptions } from 'eventemitter2';
34
import { DefaultEventEmitter } from './definitions';
45
import { EventBus } from './eventbus';
56
import { EventBusInitializer } from './initializer';
67

8+
export interface EventBusModuleOptions extends ConstructorOptions {
9+
/**
10+
* If "true", registers `EventEmitterModule` as a global module.
11+
* See: https://docs.nestjs.com/modules#global-modules
12+
*
13+
* @default true
14+
*/
15+
global?: boolean;
16+
}
17+
718
@Module({})
819
export class EventBusModule {
9-
static forRoot(options?: any): DynamicModule {
20+
static forRoot(options?: EventBusModuleOptions): DynamicModule {
1021
return {
1122
global: options?.global ?? true,
1223
module: EventBusModule,
1324
imports: [DiscoveryModule],
14-
providers: [EventBusInitializer, EventBus, DefaultEventEmitter],
25+
providers: [
26+
{ provide: 'Options', useValue: options },
27+
EventBusInitializer,
28+
EventBus,
29+
DefaultEventEmitter
30+
],
1531
exports: [EventBus, DefaultEventEmitter]
1632
};
1733
}

0 commit comments

Comments
 (0)