turning listener off on bound EventEmitter does not work from inside the listener
ilmarspenneo opened this issue · comments
Ilmārs commented
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