JeffH
JeffH

Reputation: 255

Nodejs removing event listeners

Looking to get some help. I'm new to Nodejs and wondering if it is possible, to remove this custom event emitter. Most of this code comes from the Hand on nodejs by Pedro Teixeira. My function at the bottom is attempting to remove the custom event emitter you setup in the book.

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

// Pseudo-class named ticker that will self emit every 1 second.
var Ticker = function()
{
    var self = this;
    setInterval(function()
    {
        self.emit('tick');
    }, 1000);   
};

// Bind the new EventEmitter to the sudo class.
util.inherits(Ticker, EventEmitter);

// call and instance of the ticker class to get the first
// event started. Then let the event emitter run the infinante loop.
var ticker = new Ticker();
ticker.on('tick', function()
{
    console.log('Tick');
});

(function tock()
{
    setInterval(function()
    {
        console.log('Tock');
        EventEmitter.removeListener('Ticker',function()
            {
                console.log("Clocks Dead!");
            });
    }, 5000);
})();

Upvotes: 10

Views: 19707

Answers (2)

nikoss
nikoss

Reputation: 3698

https://nodejs.org/api/events.html#events_emitter_removelistener_eventname_listener if you check the link above the usage is EventEmitter.removeListener("eventName",listenerFunc) so lets assume i have following scenario

const {EventEmitter} =require("events")
//lets construct our emitter object
const myEmitter=new EventEmitter()
//and our listener which has to be a named func if we want to identify it later for removing
function myListenerFunc(d){
    console.log(d)
    }
//adding the emitter
myEmitter.on("data",myListenerFunc)
//lets remove it after a while
//... some time passed something was done 
//... some more time passes something  else was done
//we dont need the listener anymore lets remove it
myEmitter.removeListener("data",myListenerFunc)
//alternatively if you want to remove all listeners
myEmitter.removeAllListeners(["data"/* if you need list all the event names in this array*/])

Upvotes: 4

Vadim Baryshev
Vadim Baryshev

Reputation: 26219

You need to use removeListener method of ticker object, not EventEmitter. The first argument is event name, the second - link to event listener to be deleted.

This code should works:

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

// Pseudo-class named ticker that will self emit every 1 second.
var Ticker = function()
{
    var self = this;
    setInterval(function()
    {
        self.emit('tick');
    }, 1000);   
};

// Bind the new EventEmitter to the sudo class.
util.inherits(Ticker, EventEmitter);

// call and instance of the ticker class to get the first
// event started. Then let the event emitter run the infinante loop.
var ticker = new Ticker();
var tickListener = function() {
    console.log('Tick');
};
ticker.on('tick', tickListener);

(function tock()
{
    setTimeout(function()
    {
        console.log('Tock');
        ticker.removeListener('tick', tickListener);
        console.log("Clocks Dead!");
    }, 5000);
})();

Upvotes: 10

Related Questions