Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eventEmitter listeners and emitters with different parameters

Can we have multiple listeners of an emitter, each working on different number of arguments?

e.g. let event emitter be like this:

evetE.emit('pre', global, file, self);
corresponding event listeners:
//Listener 1

m.eventE.on('pre', function() {
//TODO
})

//Listener 2
eventE.on('pre', function(context, file, m){
  console.log(context.ans);
});

//Listener 3
eventE.on('pre', function(context){
  console.log(context.ans);
});

//Listener 4
this.eventE.on('pre',function (context) {})

If the above is true, then which parameter goes to which listener?

like image 954
nirvanastack Avatar asked Jun 25 '14 17:06

nirvanastack


People also ask

Which method will process the request parameters using on () method of EventEmitter class?

The on() method requires name of the event to handle and callback function which is called when an event is raised. The emit() function raises the specified event. First parameter is name of the event as a string and then arguments. An event can be emitted with zero or more arguments.

How do I add a listener to EventEmitter?

on(event, listener) and eventEmitter. addListener(event, listener) are pretty much similar. It adds the listener at the end of the listener's array for the specified event. Multiple calls to the same event and listener will add the listener multiple times and correspondingly fire multiple times.

How are functions called when an EventEmitter object emits an event?

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and discarded. The following example shows a simple EventEmitter instance with a single listener.

What is event and event emitter?

An event emitter is a pattern that listens to a named event, fires a callback, then emits that event with a value. Sometimes this is referred to as a “pub/sub” model, or listener.


2 Answers

Event listeners just regular JS functions. So you can pass as many arguments as you want, but function only can access those arguments you have declared in function definition i.e.

var EE = require('events').EventEmitter;
var ee = new EE();

ee.on('test', function (first, second, third) {
  console.log(first, second, third); //Will output full phrase
});

ee.on('test', function (first) {
  console.log(first); //Will output just first word
});

ee.on('test', function () {
  console.log.apply(console, arguments); //Will output full phrase again
});

ee.emit('test', 'Hello', 'my', 'world!');

Actually you can see that all provided arguments always passed to every function. But if you don't define argument names in function declaration you won't be able to directly access this arguments. But you can use magic "arguments" object inside every function to access all provided arguments. Ofcourse, argument provided to the function in the order they passed to EE.

like image 168
mynameisdaniil Avatar answered Oct 23 '22 04:10

mynameisdaniil


The EventEmitter appears to call all listeners using the apply method. Therefore, every listener can expect to receive arguments in the same order passed to the emit function. The following code demonstrates that the parameterless listener still receives all arguments to the function.

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

var ee = new EventEmitter();

ee.on('myEvent', function() {
    console.log('no arguments');
    console.log(arguments); // Outputs: { '0': 'arg 1', '1': 'arg 2' }
});

ee.on('myEvent', function(arg1, arg2) {
    console.log('with arguments');
    console.log(arg1);
    console.log(arg2);
});

ee.emit('myEvent', 'arg 1', 'arg 2');
like image 24
Hayes Avatar answered Oct 23 '22 06:10

Hayes