events: don't call once twice
authorTim Wood <washwithcare@gmail.com>
Tue, 12 Nov 2013 20:19:13 +0000 (12:19 -0800)
committerFedor Indutny <fedor.indutny@gmail.com>
Tue, 12 Nov 2013 23:21:04 +0000 (03:21 +0400)
commitc9d93f34311ce0a9b59ed9f4511a2e3ba69e0f25
treef71eb3fbecbff07691e1d4a1c538bebd683483e0
parentac9cf002524d6817f4fdd42e08d4757fb0c29b5f
events: don't call once twice

Emitting an event within a `EventEmitter#once` callback of the same
event name will cause subsequent `EventEmitter#once` listeners of the
same name to be called multiple times.

    var emitter = new EventEmitter();

    emitter.once('e', function() {
      emitter.emit('e');
      console.log(1);
    });

    emitter.once('e', function() {
      console.log(2);
    });

    emitter.emit('e');

    // Output
    // 2
    // 1
    // 2

Fix the issue, by calling the listener method only if it was not
already called.
lib/events.js
test/simple/test-event-emitter-once.js