Node.js(10) - NodeJs event

Event module of NodeJs

There is an on('data') event in the crawler code. Why does the response result have an on method? What does this on do? To answer these questions, we need to start with the event module. There are no bubbles in the browser or capturing these behaviors in NodeJs, so the events module implemented in NodeJs has a large size inside. Most modules integrate this module.

So events is the most important module. It only exposes one object, eventImit. Its function is to emit events and monitor events. Two different execution links are unrelated, but with the support of event module, these two links can be established. It supports multiple event listeners with a maximum of 10.

It's adding 10 event listeners to an event.

 

var EventEmitter = require('events').EventEmitter

var life = new EventEmitter()

//addEventListener
life.on('Ask for comfort',function(who){
	console.log('to'+who+'To discharge water')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'Knead shoulder')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'cook')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'Wash clothes')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 5')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 6')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 7')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 8')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 9')
})

life.on('Ask for comfort',function(who){
	console.log('to'+who+'. . . . . . . . 10')
})
//trigger
life.emit('Ask for comfort','Man')

The results are as follows:

What happens if there are more than 10 events bound?

life.on('Ask for comfort',function(who){
	console.log('to'+who+'You want to kill me.')
})

The results are as follows:

The 11th listener will still be executed, but a warning will be thrown. This is the official recommendation. For an event, no more than 10 listeners should be set. Too many words will lead to memory leaks. Of course, this value can be modified.

Let's revise the limits of events:

life.setMaxListeners(11)

//After adding the new object, before on

The results of implementation are as follows:

If I register another event for life, will it take up the maximum?

The code is as follows:

life.on('Seeking love',function(who){
	console.log('to'+who+'Buy clothes')
})
life.on('Seeking love',function(who){
	console.log('to'+who+'Payment of wages')
})

The results are as follows:

The results show that if there is no emit caring event, the monitoring function will not be triggered.

It's not a uniform allocation of all events, it's just an allocation of 11 quotas for comfort.


See if an event has been monitored?

The return value of the listener event is a boolean type

var flag1 = life.emit('Ask for comfort','Man')
var flag2 = life.emit('Seeking love','Younger sister')
var flag3 = life.emit('What to Seek','Men and Men')

console.log(flag1);
console.log(flag2);
console.log(flag3);

The results are as follows:


How to remove an event?

Wrong Writing: Write before the event triggers emit

life.removeListener('Ask for comfort',function(){
	console.log('to'+who+'To discharge water')
})

The results are as follows:

The explanation didn't work.

So how should we write it? This form of anonymous function is not feasible, and it needs to write named functions.

The transformation is as follows:

function water(who){
	console.log('to'+who+'To discharge water')
}

//addEventListener
life.on('Ask for comfort',water)

//remove
life.removeListener('Ask for comfort',water)

The results are as follows:

The removal event was successful.

Query the remaining number of event listeners?

console.log(life.listeners('Ask for comfort').length)
console.log(EventEmitter.listenerCount(life,'Ask for comfort'))

The results are as follows:

Remove all surveillance: including caring and consolation

life.removeAllListeners()

The results are as follows:

If you only want to remove the consolation, then you need to pass the name of the specific event.

For example:

life.removeAllListeners('Ask for comfort')

The results are as follows:


Posted by TheMagician on Mon, 15 Jul 2019 17:10:50 -0700