Node.js EventEmitter
Node.js All asynchronous I/O operations will send an event to the event queue when completed.
Many objects in Node.js will emit events: a net.Server object will emit an event every time there is a new connection, and a fs.readStream object will emit an event when the file is opened. All of these event-generating objects are instances of events.EventEmitter.
EventEmitter class
The events module only provides one object: events.EventEmitter. The core of EventEmitter is the encapsulation of event triggering and event listener functions.
You can access this module through require("events");.
// 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var eventEmitter = new events.EventEmitter();
EventEmitter object will trigger the 'error' event if an error occurs during instantiation. The 'newListener' event is fired when a new listener is added, and the 'removeListener' event is fired when a listener is removed.
Below we use a simple example to illustrate the usage of EventEmitter:
//event.js 文件 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);
The execution results are as follows:
Run this code, and the console outputs after 1 second 'some_event event triggers'. The principle is that the event object registers a listener for the event some_event, and then we use setTimeout to send the event some_event to the event object after 1000 milliseconds. At this time, the listener for some_event will be called.
$ node event.js some_event 事件触发
Each event of EventEmitter consists of an event name and several parameters. The event name is a string, which usually expresses certain semantics. For each event, EventEmitter supports several event listeners.
When an event is triggered, the event listeners registered to this event are called in sequence, and the event parameters are passed as callback function parameters.
Let us explain this process with the following example:
//event.js 文件 var events = require('events'); var emitter = new events.EventEmitter(); emitter.on('someEvent', function(arg1, arg2) { console.log('listener1', arg1, arg2); }); emitter.on('someEvent', function(arg1, arg2) { console.log('listener2', arg1, arg2); }); emitter.emit('someEvent', 'arg1 参数', 'arg2 参数');
Execute the above code, the running results are as follows:
$ node event.js listener1 arg1 参数 arg2 参数 listener2 arg1 参数 arg2 参数
In the above example, the emitter registered two events for someEvent event listener, and then the someEvent event is triggered.
You can see in the running results that two event listener callback functions are called successively. This is the simplest usage of EventEmitter.
EventEmitter provides multiple properties, such as on and emit. The on function is used to bind the event function, and the emit attribute is used to trigger an event. Next, let’s take a closer look at the properties of EventEmitter.
Method
Serial Number | Method & Description |
---|---|
1 | addListener(event, listener) Add a listener to the end of the listener array for the specified event. |
2 | on(event, listener) Register a listener for the specified event, accepting a string event and a callback function. server.on('connection', function (stream) { console.log('someone connected!'); }); |
3 | once(event, listener) Register a one-time listener for the specified event, that is, the listener can only It will be triggered once, and the listener will be released immediately after being triggered. server.once('connection', function (stream) { console.log('Ah, we have our first user!'); }); |
removeListener(event, listener)Remove a listener for the specified event. The listener must be the A listener for the event that has been registered. var callback = function(stream) { console.log('someone connected!'); }; server.on('connection', callback); // ... server.removeListener('connection', callback); | |
removeAllListeners([event]) | Remove all listeners for all events. If the event is specified, move All listeners except the specified event. |
setMaxListeners(n) | By default, EventEmitters will output a warning if you add more than 10 listeners information.
The setMaxListeners function is used to increase the default limit of the number of listeners. |
listeners(event) | Returns the listener array for the specified event. |
emit(event, [arg1], [arg2], [...]) | In the order of parameters Execute each listener and return true if the event has a registered listener, otherwise return false. |
Method & Description | |
---|---|
listenerCount(emitter, event) | Returns the number of listeners for the specified event. |
Event & Description | |
---|---|
newListener |
|
removeListener |
|