Reputation: 155
I have an EventEmitter
instance declared and exported in one module (lets say index.ts
) like this
class Logger extends EventEmitter {};
/* Logger instance */
export const logger: Logger = new Logger();
in another module i imported this logger
instance and defined a few handlers. (handlers.ts
)
logger.on(LOG_TRIGGER, (data: ILogTrigger): void => {
const symbol: string = data.symbol;
const change: number = data.change;
const price: number = data.last_price;
logger.logTrigger(symbol, price, change);
});
But when i import the logger instance in a foreign module from the index.ts
file and emit the events. It doesn't get triggered. But, it gets triggered when both the handlers and the declaration are in one file. What's going on here?
Exporting the class into the handler module is a solution, But my question is that: Can we export the instance of a class and define more handlers on it in a separate module?
Upvotes: 0
Views: 714
Reputation: 155
I realized that the module in which I'm defining my event handler's is not being reached by the process at all. Importing the external module where i defined my handlers into the module where the event emitter instance is being initialized [OR] Importing to the main process where the code is initiated helped solved this issue.
import "./external-handler-module.ts"
This makes the code "reachable" by the application process.
Upvotes: 1