-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send the queueUrl alongside emitted events (#560)
- Loading branch information
1 parent
7bc55fc
commit 3ca59bf
Showing
4 changed files
with
95 additions
and
11 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
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,37 +1,55 @@ | ||
import { EventEmitter } from "node:events"; | ||
|
||
import { logger } from "./logger.js"; | ||
import { Events } from "./types.js"; | ||
import { Events, QueueMetadata } from "./types.js"; | ||
|
||
export class TypedEventEmitter extends EventEmitter { | ||
protected queueUrl?: string; | ||
|
||
/** | ||
* @param queueUrl - The URL of the SQS queue this emitter is associated with | ||
*/ | ||
constructor(queueUrl?: string) { | ||
super(); | ||
this.queueUrl = queueUrl; | ||
} | ||
|
||
/** | ||
* Trigger a listener on all emitted events | ||
* @param event The name of the event to listen to | ||
* @param listener A function to trigger when the event is emitted | ||
*/ | ||
on<E extends keyof Events>( | ||
event: E, | ||
listener: (...args: Events[E]) => void, | ||
listener: (...args: [...Events[E], QueueMetadata]) => void, | ||
): this { | ||
return super.on(event, listener); | ||
} | ||
|
||
/** | ||
* Trigger a listener only once for an emitted event | ||
* @param event The name of the event to listen to | ||
* @param listener A function to trigger when the event is emitted | ||
*/ | ||
once<E extends keyof Events>( | ||
event: E, | ||
listener: (...args: Events[E]) => void, | ||
listener: (...args: [...Events[E], QueueMetadata]) => void, | ||
): this { | ||
return super.once(event, listener); | ||
} | ||
|
||
/** | ||
* Emits an event with the provided arguments | ||
* Emits an event with the provided arguments and adds queue metadata | ||
* @param event The name of the event to emit | ||
* @param args The arguments to pass to the event listeners | ||
* @returns {boolean} Returns true if the event had listeners, false otherwise | ||
* @example | ||
* // Inside a method: | ||
* this.emit('message_received', message); | ||
*/ | ||
emit<E extends keyof Events>(event: E, ...args: Events[E]): boolean { | ||
logger.debug(event, ...args); | ||
return super.emit(event, ...args); | ||
const metadata: QueueMetadata = { queueUrl: this.queueUrl }; | ||
logger.debug(event, ...args, metadata); | ||
return super.emit(event, ...args, metadata); | ||
} | ||
} |
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