Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ilmarspenneo opened this issue · comments

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