Issue with Node.js EventEmitter Listener Getting Stuck Indefinitely
Hi guys I am trying to handle node Events but sometimes when I run this It get's stuck indefinitely, what is causing that?:
/**
* Sets up a listener on a Node.js EventEmitter for a specific event.
*
* @param emitter - The EventEmitter instance to listen on.
* @param eventName - The name of the event to listen for.
* @param f - A function that takes the event data and returns an Effect.
* @returns An Effect that sets up the listener and ensures it is disposed of properly.
*/
export function onEffect<Data, A, E, R>(
emitter: EventEmitter,
eventName: string,
f: (data: Data) => E.Effect<A, E, R>,
): E.Effect<A, never, R> {
return E.runtime<R>().pipe(E.flatMap((runtime) =>
E.async<A>((_resume) => {
const run = Runtime.runFork(runtime);
const handler = (data: Data) =>
run(E.catchAllCause(f(data), (cause) =>
E.log("Unhandled defect in onEffect handler", cause)));
emitter.on(eventName, handler);
return E.sync(() =>
emitter.off(eventName, handler)
);
})
));
}/**
* Sets up a listener on a Node.js EventEmitter for a specific event.
*
* @param emitter - The EventEmitter instance to listen on.
* @param eventName - The name of the event to listen for.
* @param f - A function that takes the event data and returns an Effect.
* @returns An Effect that sets up the listener and ensures it is disposed of properly.
*/
export function onEffect<Data, A, E, R>(
emitter: EventEmitter,
eventName: string,
f: (data: Data) => E.Effect<A, E, R>,
): E.Effect<A, never, R> {
return E.runtime<R>().pipe(E.flatMap((runtime) =>
E.async<A>((_resume) => {
const run = Runtime.runFork(runtime);
const handler = (data: Data) =>
run(E.catchAllCause(f(data), (cause) =>
E.log("Unhandled defect in onEffect handler", cause)));
emitter.on(eventName, handler);
return E.sync(() =>
emitter.off(eventName, handler)
);
})
));
}