Closed mStirner closed 1 month ago
const { EventEmitter } = require("events");
module.exports = class Events extends EventEmitter {
constructor(...args) {
super(...args);
}
static symbol = Symbol("register");
static events = new Set();
emit(event, ...args) {
// is the code below even necessary?
// if we hook into the `.emit` method
// we could just use this here to "broadcast" into child/plugin/worker
// change `static symbol...` to `static broadcast = Symbol("broadcast")`
// and emit events as symbol: `super.emit(Events.broadcast, ...)`
// some where, where the main/worker communication is handeld, listen then for `Events.on(Events.broadcast, ...)`
if (!Events.events.has(event)) {
Events.events.add(event);
process.nextTick(() => {
super.emit(Events.symbol, event, ...args);
});
}
return super.emit(event, ...args);
}
};
To be able to make every emitted event somewhere in a component available in all worker threads (if implemented, see #6), a custom/intercepted
.emit(...)
method is needed.Either use a Proxy or a custom
EventEmitter
class.Proxy approach (quick ChatGPT asked):
EventEmitter class approach (quick written here)