Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ilmarspenneo opened this issue Jun 23, 2023 · 0 comments

Comments

@ilmarspenneo
Copy link

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
@ilmarspenneo ilmarspenneo changed the title turning listener off on bound EventEmitter does not work turning listener off on bound EventEmitter does not work from inside the listener Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant