Node.js - Listen for when listeners are registered

239
April 11, 2017, at 2:46 PM

In Node.js, we have the standard event emitter:

const EE = require('events');
const ee = new EE();
ee.on('x', function(){
});

what I would like to do, is listen for when a client registers a listener. The purpose is so that if the emitter is in a particular state, it will take actions upon registration.

To be clear, when ee.on() is called, for a particular event 'x', I want to take some actions.

How can I do this without monkey-patching all event emitters in Node.js?

If I do the following, am I patching the prototype method or the instance method?

let on = ee.on;
ee.on = function(){
    if(ee.y === true){
      process.next(function(){
        ee.emit('done');
      });
    }
    return on.apply(ee, arguments);
};

this is of course the same as:

   let on = ee.on;
    ee.on = function(){
        if(this.y === true){
          process.next(() => {
            this.emit('done');
          });
        }
       return on.apply(this, arguments);
    };

I think this will work, but I am not sure. I think this will only effect this one particular event emitter, not all event emitters, because it will not change the prototype...

Answer 1

To have the same behavior replicated you should create a new class/prototype from where all other instances will inherit.

EDIT:

<file_my_class.js>:
var   Util = require('util');
const EventEmmiter = require('events');
// private data goes here
...
// public data follows
module.exports = MyClass;
function MyClass() {
...
}
Util.inherits (MyClass, EventEmmiter);
MyClass.prototype.mymethod = function () {
    someobj.on('somevent', () => {
        ...
        if (test_condition_ok) {
           this.emit ('myeventname');
        }
    });
}
<main.js>:
var MyClass = require('./file_my_class.js');
var myclass_instance = new MyClass();
myclass_instance.mymethod();
myclass_instance.on('myeventname', () => {
      // Do Something
   });
Answer 2

You can listen for a newListener event:

The EventEmitter instance will emit its own 'newListener' event before a listener is added to its internal array of listeners.

See EventEmitter.newListener.

READ ALSO
Webpack add owl.carousel

Webpack add owl.carousel

webpackconfig

611
aws-sdk-mock not mocking putRecord on firehose

aws-sdk-mock not mocking putRecord on firehose

I am trying to mock putRecord method on AWS Firehose object but mocking is not succeedingThe code ends up calling the aws-sdk api on firehose object which talks with live aws service

269
read ECONNRESET error in nodejs

read ECONNRESET error in nodejs

I extracted this piece of code from nodejs documentation Link, but when i run using my nodejs server It gave me this errorSome people in stack overflow had the same error, but I didn't find solution

443
Nodejs Google cloud storage returns 403 when accessing public url

Nodejs Google cloud storage returns 403 when accessing public url

I'm uploading a profile image to google cloud store (which works fine) and trying to make all of the images publicly available, however I keep getting a 403 error from the returned url when used inside of img src or just pasting it in the browser, the returned...

351