Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #544 from thiagomini/feature/request-scoped-event-…
…emitters Feature/request scoped event emitters
- Loading branch information
Showing
10 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
import { REQUEST } from '@nestjs/core'; | ||
|
||
export const EVENT_LISTENER_METADATA = 'EVENT_LISTENER_METADATA'; | ||
export const EVENT_PAYLOAD = REQUEST; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { EventEmitter2 } from 'eventemitter2'; | ||
export * from './decorators'; | ||
export * from './event-emitter.module'; | ||
export { EVENT_PAYLOAD } from './constants'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const TEST_EVENT_PAYLOAD = { | ||
test: 'event', | ||
}; | ||
|
||
export const TEST_EVENT_MULTIPLE_PAYLOAD = [ | ||
TEST_EVENT_PAYLOAD, | ||
{ | ||
test2: 'event2', | ||
}, | ||
]; | ||
|
||
export const TEST_EVENT_STRING_PAYLOAD = 'some-string'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { OnEvent } from '../../lib'; | ||
import { EVENT_PAYLOAD } from '../../lib'; | ||
import { RequestScopedEventPayload } from './request-scoped-event-payload'; | ||
|
||
@Injectable() | ||
export class EventsProviderRequestScopedConsumer { | ||
constructor(@Inject(EVENT_PAYLOAD) public eventRef: any) { | ||
EventsProviderRequestScopedConsumer.injectedEventPayload.setPayload( | ||
this.eventRef, | ||
); | ||
} | ||
|
||
public static injectedEventPayload = new RequestScopedEventPayload(); | ||
|
||
@OnEvent('test.*') | ||
onTestEvent() {} | ||
|
||
@OnEvent('multiple.*') | ||
onMultiplePayloadEvent() {} | ||
|
||
@OnEvent('string.*') | ||
onStringPayloadEvent() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { EventEmitter2 } from 'eventemitter2'; | ||
import { | ||
TEST_EVENT_MULTIPLE_PAYLOAD, | ||
TEST_EVENT_PAYLOAD, | ||
TEST_EVENT_STRING_PAYLOAD, | ||
} from './constants'; | ||
|
||
@Injectable() | ||
export class EventsProducer implements OnApplicationBootstrap { | ||
constructor(private readonly eventEmitter: EventEmitter2) {} | ||
|
||
onApplicationBootstrap() { | ||
this.eventEmitter.emit('test.event', { test: 'event' }); | ||
this.eventEmitter.emit('test.event', TEST_EVENT_PAYLOAD); | ||
this.eventEmitter.emit('multiple.event', TEST_EVENT_MULTIPLE_PAYLOAD); | ||
this.eventEmitter.emit('string.event', TEST_EVENT_STRING_PAYLOAD); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Class used to test injected payloads on the RequestScoped listener. | ||
* Each value stored in the instance represents a different type of payload. | ||
*/ | ||
export class RequestScopedEventPayload { | ||
public objectValue: Record<string, any>; | ||
public arrayValue: any[]; | ||
public stringValue: string; | ||
|
||
constructor() { | ||
this.objectValue = {}; | ||
this.arrayValue = []; | ||
this.stringValue = ''; | ||
} | ||
|
||
public setPayload(value: any) { | ||
if (Array.isArray(value)) { | ||
this.arrayValue = value; | ||
} else if (typeof value === 'string') { | ||
this.stringValue = value; | ||
} else { | ||
this.objectValue = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters