mattinsler/longjohn

longjohn is not compatible to other modules using a similar wrapping of EventEmitter

Opened this issue · 1 comments

The way how longjohn wraps EventEmitter is incompatible with other modules doing a similar/same wrapping.
I tested by creating a clone of longjohn (incl. modifiny the 'longjohn' property of on()) and used it together with unmodified longjohn in following script:

require('longjohn');

var assert = require('assert');
var EventEmitter = require('events').EventEmitter;

function onListener() {}
function onceListener() {}

function test() {
    var emitter = new EventEmitter;
    emitter.on('xxx', onListener);
    emitter.once('xxx', onceListener);
    assert.equal(emitter.listenerCount('xxx'), 2);

    emitter.removeListener('xxx', onListener);
    // fails on second call as onListener was not removed
    assert.equal(emitter.listenerCount('xxx'), 1);

    emitter.removeListener('xxx', onceListener);
    assert.equal(emitter.listenerCount('xxx'), 0);
}

test();

require('longjohn-clone');

test();

Above sample passes now since #67 but following sample fails:

require('longjohn');

var assert = require('assert');
var EventEmitter = require('events').EventEmitter;

function onListener() {}
function onceListener() {}

function test() {
    var emitter = new EventEmitter;
    emitter.on('xxx', onListener);
    assert.equal(emitter.listenerCount('xxx'), 1);
    emitter.emit("xxx");
    // fails on second call as onListener was removed
    assert.equal(emitter.listenerCount('xxx'), 1);
}

test();

require('longjohn-clone');

test();

Reason is that second on() call sees the listener property and installs the function as once handler.