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?
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.
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.
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.
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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With