primus / eventemitter3

EventEmitter3 - Because there's also a number 2. And we're faster.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

eventEmitter does not skip removed listener when remove happens during emitting event

stepancar opened this issue · comments

Hello, I faced with unexpected behaviour.

import { EventEmitter } from "eventemitter3";

const ee = new EventEmitter();

function listener1() {
  console.log("I am listener 1");
  ee.off("event", listener2);
}

function listener2() {
  console.log("I am listener 2");
}

ee.on("event", listener1);
ee.on("event", listener2);

ee.emit("event");

output:

I am listener 1
I am listener 2

expected output,

I am listener 1

Looking at code, I know where issue comes from. To support such behaviour we need to change design (data structures) completely. But maybe current behaviour is expected?

If it's not, I can create PR

I see, this behaviour comes from nodejs
https://nodejs.org/api/events.html#emitterremovelistenereventname-listener

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

But how to implement what I need with existing api?

Yes, the behavior is expected. It was designed to work like this.

But how to implement what I need with existing api?

You can set a flag in listener1 and make listener2 a noop if the flag is set.

I'm closing this as answered.