Skip to content

Commit 359c845

Browse files
docs: add docs on event emitter readiness watcher
1 parent a38ec52 commit 359c845

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

content/techniques/events.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ EventEmitterModule.forRoot({
5050
});
5151
```
5252

53-
#### Dispatching Events
53+
#### Dispatching events
5454

5555
To dispatch (i.e., fire) an event, first inject `EventEmitter2` using standard constructor injection:
5656

@@ -72,7 +72,7 @@ this.eventEmitter.emit(
7272
);
7373
```
7474

75-
#### Listening to Events
75+
#### Listening to events
7676

7777
To declare an event listener, decorate a method with the `@OnEvent()` decorator preceding the method definition containing the code to be executed, as follows:
7878

@@ -85,11 +85,10 @@ handleOrderCreatedEvent(payload: OrderCreatedEvent) {
8585

8686
> warning **Warning** Event subscribers cannot be request-scoped.
8787
88-
The first argument can be a `string` or `symbol` for a simple event emitter and a `string | symbol | Array<string | symbol>` in a case of a wildcard emitter.
88+
The first argument can be a `string` or `symbol` for a simple event emitter and a `string | symbol | Array<string | symbol>` in a case of a wildcard emitter.
8989

9090
The second argument (optional) is a listener options object as follows:
9191

92-
9392
```typescript
9493
export type OnEventOptions = OnOptions & {
9594
/**
@@ -103,7 +102,7 @@ export type OnEventOptions = OnOptions & {
103102

104103
/**
105104
* If "true", the onEvent callback will not throw an error while handling the event. Otherwise, if "false" it will throw an error.
106-
*
105+
*
107106
* @default true
108107
*/
109108
suppressErrors?: boolean;
@@ -142,6 +141,22 @@ handleEverything(payload: any) {
142141

143142
> info **Hint** `EventEmitter2` class provides several useful methods for interacting with events, like `waitFor` and `onAny`. You can read more about them [here](https://github.com/EventEmitter2/EventEmitter2).
144143
144+
#### Preventing event loss
145+
146+
Events triggered before or during the `onApplicationBootstrap` lifecycle hook—such as those from module constructors or the `onModuleInit` method—may be missed because the `EventSubscribersLoader` might not have finished setting up the listeners.
147+
148+
To avoid this issue, you can use the `waitUntilReady` method of the `EventEmitterReadinessWatcher`, which returns a promise that resolves once all listeners have been registered. This method can be called in the `onApplicationBootstrap` lifecycle hook of a module to ensure that all events are properly captured.
149+
150+
```typescript
151+
await this.eventEmitterReadinessWatcher.waitUntilReady();
152+
await this.eventEmitter.emit(
153+
'order.created',
154+
new OrderCreatedEvent({ orderId: 1, payload: {} }),
155+
);
156+
```
157+
158+
> info **Note** This is only necessary for events emitted before the `onApplicationBootstrap` lifecycle hook is complete.
159+
145160
#### Example
146161

147162
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/30-event-emitter).

0 commit comments

Comments
 (0)