Skip to content

Instantly share code, notes, and snippets.

@ljharb

ljharb/Listeners.js

Forked from ralphholzmann/Listeners.js
Last active Dec 11, 2015
Embed
What would you like to do?
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