Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+
BSD 2-Clause "Simplified" License
758 stars 89 forks source link

turning listener off on bound EventEmitter does not work from inside the listener #79

Open ilmarspenneo opened 1 year ago

ilmarspenneo commented 1 year ago

With a vanilla EventEmitter you can do turn off a listener from the listener it self and it works as expected:

const emitter = new EventEmitter();

const listener = () => {
  console.log('DONG');
  emitter.off('DING', listener);
}

emitter.on('DING', listener);

emitter.emit('DING');
emitter.emit('DING');

Console output:

DONG

But with an EventEmitter wrapped with cls-hooked, it seems that you cannot turn off the event listener from within the listener itself:

const namespace = createNamespace('space');
const emitter = new EventEmitter();
namespace.bindEmitter(emitter);

const listener = function () {
  console.log('PONG');
  emitter.off('PING', listener);
};

emitter.on('PING', listener);

emitter.emit('PING');  
emitter.emit('PING');

Console output:

PONG
PONG