Skip to content

Instantly share code, notes, and snippets.

@ljharb
Forked from ralphholzmann/Listeners.js
Last active December 11, 2015 06:28
Show Gist options
  • Save ljharb/4559477 to your computer and use it in GitHub Desktop.
Save ljharb/4559477 to your computer and use it in GitHub Desktop.
var EventEmitter = require("events").EventEmitter;
var emitter = new EventEmitter();
var foo = function () {
console.log("foo");
};
emitter.once("foo", foo);
var bar = function () {
console.log("bar");
};
emitter.on("foo", bar);
var listeners = emitter.listeners("foo"); // => [function(){} bound with "once", function(){} bound with "on"]
// Manually calling the handlers themselves doesn't change their event binding
foo();
bar();
listeners.length; // => 2
listeners[0](); // Manually calling the listener that was bound with "once" removes it from the array
listeners.length; // => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment