Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/catalint/edh
https://github.com/catalint/edh
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/catalint/edh
- Owner: catalint
- Created: 2023-09-30T22:14:46.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-01T08:10:24.000Z (about 1 year ago)
- Last Synced: 2024-10-18T16:27:36.386Z (2 months ago)
- Language: TypeScript
- Size: 25.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Event Driven Helper
This is a POC , don't use this
## Why do it?
I wanted to create a simple event emitter POC that would allow me keep a stack trace of the event emitter calls. This is useful for debugging and auditing purposes.
## How?
Using async context
## Please investigate other packages before using this one
https://www.npmjs.com/package/typed-emitter
https://www.npmjs.com/package/eventemitter3
## Example
```typescript
// Example usageimport { EventEmitterH } from "../src";
interface MyEvents {
userLoggedIn: { username: string };
messageReceived: { sender: string; message: string };
messageReceived2: { sender: string; message: string };
// Add more event types as needed
}const eventEmitter = new EventEmitterH();
eventEmitter.setDebugMode(true);
eventEmitter.on("userLoggedIn", {
name: "userLoginHandler",
await: true,
func: async () => {
await eventEmitter.emit("messageReceived", {
sender: "john_doe",
message: "Hello World!",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("User login handling completed.");
},
});eventEmitter.on("messageReceived", {
name: "messageReceivedHandler",
await: true,
func: async () => {
await eventEmitter.emit("messageReceived2", {
sender: "john_doe",
message: "Hello World!",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
},
});eventEmitter.on("messageReceived2", {
name: "messageReceivedHandler2",
await: true,
func: async () => {
throw new Error("Error in messageReceivedHandler2");
await new Promise((resolve) => setTimeout(resolve, 1000));
},
});// Emit event and handle the promise
eventEmitter
.emit("userLoggedIn", { username: "john_doe" })
.then(() => {
console.log("All handlers completed.");
})
.catch((error) => {
console.error("Error:", error);
});// Output
// [EventEmitter] [START] userLoginHandler(userLoggedIn:81os0jt)
// [EventEmitter] [START] userLoginHandler(userLoggedIn:81os0jt) -> messageReceivedHandler(messageReceived:yw2cka7)
// [EventEmitter] [START] userLoginHandler(userLoggedIn:81os0jt) -> messageReceivedHandler(messageReceived:yw2cka7) -> messageReceivedHandler2(messageReceived2:ajl0cv7)
// Error: [EventEmitter] [ERROR] Error in messageReceivedHandler2 userLoginHandler(userLoggedIn:81os0jt) -> messageReceivedHandler(messageReceived:yw2cka7) -> messageReceivedHandler2(messageReceived2:ajl0cv7)
// [EventEmitter] [ALL_DONE] Handling all event listeners of messageReceived2
// [EventEmitter] [DONE] userLoginHandler(userLoggedIn:81os0jt) -> messageReceivedHandler(messageReceived:yw2cka7)
// [EventEmitter] [ALL_DONE] Handling all event listeners of messageReceived
// User login handling completed.
// [EventEmitter] [DONE] userLoginHandler(userLoggedIn:81os0jt)
// [EventEmitter] [ALL_DONE] Handling all event listeners of userLoggedIn```