primus / eventemitter3

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dead Object

aigenseer opened this issue · comments

Hello,

I use this package for a browser plugin. When you start popup.html, a new eventlistener is created, but I have no way to check if the popup.html is closed. Therefore, I need a check to see if the executed function (index.js line 201-204) is a valid function. In firefox I always get a reference error (dead object).
I have already tested this with the following code.

[...]
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
if (!(typeof listeners[i].fn === 'function' && listeners[i].fn instanceof Function)){
     this.removeListener(event, listeners[i].fn, undefined, true);
     continue;
}

switch (len) {
[...]

(These lines should be over 201)

Would that be possible to implement these lines?

Many Thanks!

I don't understand. We check that every listener is a function before adding it

eventemitter3/index.js

Lines 59 to 61 in d021b4d

if (typeof fn !== 'function') {
throw new TypeError('The listener must be a function');
}

How can a listener NOT be a function?

I'm guessing they are doing something like cross window event listener assignment so when one window (popup closes) all the function handles are getting destroyed leaving empty function references in the event emitter.

If that is the case, I'm proposing a "wont fix" as that would be a massive edge case that is not worth th performance penality of additional checks. What you could do in your code base, is perform this check your self by using emitter.listeners(eventName) and validate that assigned listeners are still valid function and if that is not the case, you should remove them before you emit the event.