You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! Node.js v0.1.26 implemented a special event newListener. It is emitted once any new listener is added to an EventEmitter. It seems to me that dest.on(...) should trigger source to emit newListener event. In the example below the code prints nothing:
constee1=newEventEmitter()constee2=newEventEmitter()ee1.once('newListener',(event,listener)=>console.log('ee1 new listener'))ee2.once('newListener',(event,listener)=>console.log('ee2 new listener'))propagate(ee1,ee2)// should print:// ee1 new listener// ee2 new listeneree2.on('event',(a,b)=>console.log('got propagated event',a,b))
I faced the issue while propagating MongoDB change streams into Node's EventEmitter. As you can see in the code, ChangeStream extends EventEmitter and calls emit method only if newListener was fired. In other words, there must be at least one event listener registered. listenerCount also has to be changed.
@xamgore As an alternative, you could use EventEmitter2 emitter as a relay. Every instance of the EventEmitter2 class has the listenTo method to do the similar things, but instead of replacing the emit method on the source it just subscribe to the events you need and emits them through itself. All the listeners will be append to the source emitter on demand, so this should work.
constEventEmitter=require('events').EventEmitter;constEventEmitter2=require('eventemitter2');constsource=newEventEmitter();constdest=newEventEmitter2({newListener: true,removeListener: true});source.once('newListener',(event,listener)=>console.log('ee1 new listener'))dest.once('newListener',(event,listener)=>console.log('ee2 new listener'))dest.listenTo(source,'event');console.log('listening...');dest.on('event',(...data)=>console.log('emitted',data));//append listener to the source on demandsource.emit('event',1,2,3);
Console output:
ee2 new listener
listening...
ee1 new listener
emitted [ 1, 2, 3 ]
Process finished with exit code 0
Hi! Node.js v0.1.26 implemented a special event
newListener
. It is emitted once any new listener is added to anEventEmitter
. It seems to me thatdest.on(...)
should triggersource
to emitnewListener
event. In the example below the code prints nothing:I faced the issue while propagating MongoDB change streams into Node's
EventEmitter
. As you can see in the code,ChangeStream
extendsEventEmitter
and callsemit
method only ifnewListener
was fired. In other words, there must be at least oneevent
listener registered.listenerCount
also has to be changed.What do you think about it?
The text was updated successfully, but these errors were encountered: