Node.js 事件发射器
Node.js 使用事件模块来创建和处理自定义事件。 EventEmitter 类可用于创建和处理自定义事件模块。
导入事件模块的语法如下:
句法:
const EventEmitter = require('events');
所有 EventEmitter 都会在添加新侦听器时发出事件newListener ,在移除现有侦听器时发出removeListener事件。它还提供了另一种选择:
布尔捕获拒绝
Default Value: false
It automatically captures rejections.
监听事件:在发出任何事件之前,它必须注册函数(回调)来监听事件。
句法:
eventEmitter.addListener(event, listener)
eventEmitter.on(event, listener)
eventEmitter.on(event, listener)和eventEmitter.addListener(event, listener)非常相似。它将侦听器添加到指定事件的侦听器数组的末尾。对同一事件和侦听器的多次调用将多次添加侦听器并相应地触发多次。这两个函数都返回发射器,因此可以链接调用。
发射事件:每个事件在 nodejs 中都被命名为事件。我们可以通过 emit(event, [arg1], [arg2], […])函数触发一个事件。我们可以将任意一组参数传递给侦听器函数。
句法:
eventEmitter.emit(event, [arg1], [arg2], [...])
例子:
// Importing events
const EventEmitter = require('events');
// Initializing event emitter instances
var eventEmitter = new EventEmitter();
// Registering to myEvent
eventEmitter.on('myEvent', (msg) => {
console.log(msg);
});
// Triggering myEvent
eventEmitter.emit('myEvent', "First event");
输出:
First event
删除监听器: eventEmitter.removeListener()接受两个参数事件和监听器,并从订阅该事件的监听器数组中删除该监听器。而eventEmitter.removeAllListeners()从数组中删除所有订阅了上述事件的侦听器。
句法:
eventEmitter.removeListener(event, listener)
eventEmitter.removeAllListeners([event])
例子:
// Importing events
const EventEmitter = require('events');
// Initializing event emitter instances
var eventEmitter = new EventEmitter();
var geek1= (msg) => {
console.log("Message from geek1: " + msg);
};
var geek2 = (msg) => {
console.log("Message from geek2: " + msg);
};
// Registering geek1 and geek2
eventEmitter.on('myEvent', geek1);
eventEmitter.on('myEvent', geek1);
eventEmitter.on('myEvent', geek2);
// Removing listener geek1 that was
// registered on the line 13
eventEmitter.removeListener('myEvent', geek1);
// Triggering myEvent
eventEmitter.emit('myEvent', "Event occurred");
// Removing all the listeners to myEvent
eventEmitter.removeAllListeners('myEvent');
// Triggering myEvent
eventEmitter.emit('myEvent', "Event occurred");
输出:
Message from geek1: Event occurred
Message from geek2: Event occurred
我们注册了两次 geek1 和一次 geek2。调用 eventEmitter.removeListener('myEvent', geek1) 将删除 geek1 的一个实例。最后,使用 removeAllListeners() 方法删除所有侦听器,该方法将删除 myEvent 的所有侦听器。
特殊事件:所有 EventEmitter 实例都会在添加新侦听器并删除现有侦听器“removeListener”时发出事件“newListener” 。
- 事件:'newListener' EventEmitter 实例将在将侦听器添加到其内部侦听器数组之前发出自己的“newListener”事件。为“newListener”事件注册的侦听器将被传递给事件名称和对正在添加的侦听器的引用。在将侦听器添加到数组之前触发事件“newListener”。
eventEmitter.once( 'newListener', listener) eventEmitter.on( 'newListener', listener)
- 事件:'removeListener' 'removeListener'事件在监听器被移除后触发。
eventEmitter.once( ‘removeListener’, listener) eventEmitter.on( 'removeListener’, listener)
- Event: 'error'当 EventEmitter 实例中发生错误时,典型的操作是发出一个'error'事件。如果 EventEmitter 没有为 'error' 事件注册至少一个侦听器,并且发出了 'error' 事件,则抛出错误,打印堆栈跟踪,并退出 Node.js 进程。
eventEmitter.on('error', listener)
例子:
// Importing events
const EventEmitter = require('events');
// Initializing event emitter instances
var eventEmitter = new EventEmitter();
// Register to error
eventEmitter.on('error', (err) => {
console.error('Attention! There was an error');
});
// Register to newListener
eventEmitter.on( 'newListener', (event, listener) => {
console.log(`The listener is added to ${event}`);
});
// Register to removeListener
eventEmitter.on( 'removeListener', (event, listener) => {
console.log(`The listener is removed from ${event}`);
});
// Declaring listener geek1 to myEvent1
var geek1 = (msg) => {
console.log("Message from geek1: " + msg);
};
// Declaring listener geek2 to myEvent2
var geek2 = (msg) => {
console.log("Message from geek2: " + msg);
};
// Listening to myEvent with geek1 and geek2
eventEmitter.on('myEvent', geek1);
eventEmitter.on('myEvent', geek2);
// Removing listener
eventEmitter.off('myEvent', geek1);
// Triggering myEvent
eventEmitter.emit('myEvent', 'Event occurred');
// Triggering error
eventEmitter.emit('error', new Error('Attention!'));
输出:
The listener is added to removeListener
The listener is added to myEvent
The listener is added to myEvent
The listener is removed from myEvent
Message from geek2: Event occurred
Attention! There was an error
参考: https://nodejs.org/api/events.html#events_class_eventemitter