Application summary of node core EventEmitter

EventEmitter is widely used in node, whether it is a framework, scaffolding, or plug-ins, you can see eventEmitter. For example, if we check the source code of vue, react, webpack, egg, koa and other frameworks or plug-ins, we can see that EventEmitter is used internally. So what exactly is EventEmitter?

General introduction:

Entering the official website of node.js, the introduction of eventEmitter has a lot of APIs. Many beginners feel confused. In fact, eventEmitter is not complicated. It adopts the most classic design mode in js, the subscription/publisher mode, and expands on this basis. and encapsulation implements methods and events applicable to node.

To make a metaphor, eventEmitter is like a signal dispatching center, which stores all internal event signals and receives external signals. When receiving external signals, it triggers corresponding events according to the signals. To be simpler, it can be understood as a mobile phone number storage center. First, all the information of the numbers is stored in this center, and when the number is dialed, the corresponding person is notified.

The following is a simple implementation of eventEmitter

The events module provides only one object: events.EventEmitter. The core of EventEmitter is the encapsulation of event triggering and event listener functions.

EventEmitter is a class that needs to be instantiated when used
var EventEmitter = require('events').EventEmitter; 
var event = new EventEmitter(); 
event.on('some_event', function() { 
    console.log('some_event 事件触发'); 
}); 
setTimeout(function() { 
    event.emit('some_event'); 
}, 1000); 

Common APIs:

method

Subscribe to events on(event, listener), addListener(event, listener)  (add phone number)

Single subscription once(event, listener)  (phone number used only once)

Remove subscription event removeListener(event, listener) listener must be passed in (delete a phone number)

remove all removeAllListeners([event])   (remove all phone numbers)

var callback = function(stream) {
  console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

Set the number of event subscriptions: setMaxListeners(n)  (the maximum number of phone numbers)

Return the number of event subscriptions: listeners(event)    (number of phone numbers)

A function that fires event subscriptions in order: emit(event, [arg1], [arg2], [...])  (call)

event event

Fired when a new listener is added, the newListener event fires,

The removeListener event is fired when the listener is removed

The removeListener and newListener here refer to events. Although removeListener has the same name as the method removeListener, one is an event and the other is a method.

Example:

In addition to the above add event and remove event, there is also error event

error event

The error event is triggered when an error occurs during node operation, but when the error is triggered, EventEmitter stipulates that if there is no responding listener, Node.js will treat it as an exception, exit the program and output an error message.

So generally we need to add error event monitoring to the emitter

The above is the use of events module EventEmitter, but in many cases, we will inherit EventEmitter and use the inherited class.

 

 

 

Supongo que te gusta

Origin blog.csdn.net/qdmoment/article/details/105195840
Recomendado
Clasificación