节点的模块度
The events
module provides us the EventEmitter class, which is key to working with events in Node.
events
模块为我们提供了EventEmitter类,这是在Node中处理事件的关键。
I published a full article on that, so here I will just describe the API without further examples on how to use it.
我对此发表了一篇完整的文章 ,因此在这里我将仅描述该API,而无需进一步说明如何使用它。
const EventEmitter = require('events')
const door = new EventEmitter()
The event listener eats its own dog food and uses these events:
事件侦听器吃自己的狗食并使用以下事件:
newListener
when a listener is added添加侦听
newListener
时的newListener
removeListener
when a listener is removed移除监听器后的
removeListener
Here’s a detailed description of the most useful methods:
这是最有用的方法的详细说明:
emitter.addListener()
(emitter.addListener()
)
Alias for emitter.on()
.
emitter.on()
别名。
emitter.emit()
(emitter.emit()
)
Emits an event. It synchronously calls every event listener in the order they were registered.
发出事件。 它按照注册事件的顺序同步调用每个事件侦听器。
emitter.eventNames()
(emitter.eventNames()
)
Return an array of strings that represent the events registered on the current EventListener:
返回一个字符串数组,这些字符串表示在当前EventListener上注册的事件:
door.eventNames()
emitter.getMaxListeners()
(emitter.getMaxListeners()
)
Get the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered by using setMaxListeners()
获取可以添加到EventListener对象的最大侦听器数量,该侦听器默认为10,但可以使用setMaxListeners()
进行增加或降低
door.getMaxListeners()
emitter.listenerCount()
(emitter.listenerCount()
)
Get the count of listeners of the event passed as parameter:
获取作为参数传递的事件的侦听器计数:
door.listenerCount('open')
emitter.listeners()
(emitter.listeners()
)
Gets an array of listeners of the event passed as parameter:
获取作为参数传递的事件的侦听器数组:
door.listeners('open')
emitter.off()
(emitter.off()
)
Alias for emitter.removeListener()
added in Node 10
在节点10中添加了emitter.removeListener()
别名
emitter.on()
(emitter.on()
)
Adds a callback function that’s called when an event is emitted.
添加发出事件时调用的回调函数。
Usage:
用法:
door.on('open', () => {
console.log('Door was opened')
})
emitter.once()
(emitter.once()
)
Adds a callback function that’s called when an event is emitted for the first time after registering this. This callback is only going to be called once, never again.
添加一个回调函数,该函数在注册此事件后首次发出事件时被调用。 该回调只会被调用一次,不会再被调用。
const EventEmitter = require('events')
const ee = new EventEmitter()
ee.once('my-event', () => {
//call callback function once
})
emitter.prependListener()
(emitter.prependListener()
)
When you add a listener using on
or addListener
, it’s added last in the queue of listeners, and called last. Using prependListener
it’s added, and called, before other listeners.
使用on
或addListener
添加侦听器时on
该侦听器被添加到侦听器队列的最后,并被称为last。 使用prependListener
可以在其他侦听器之前添加并调用它。
emitter.prependOnceListener()
(emitter.prependOnceListener()
)
When you add a listener using once
, it’s added last in the queue of listeners, and called last. Using prependOnceListener
it’s added, and called, before other listeners.
当您使用once
添加侦听器once
,它会被添加到侦听器队列中的最后一个,并被称为最后一个。 使用prependOnceListener
可以在其他侦听器之前添加并调用它。
emitter.removeAllListeners()
(emitter.removeAllListeners()
)
Removes all listeners of an event emitter object listening to a specific event:
删除事件发射器对象的所有侦听特定事件的侦听器:
door.removeAllListeners('open')
emitter.removeListener()
(emitter.removeListener()
)
Remove a specific listener. You can do this by saving the callback function to a variable, when added, so you can reference it later:
删除特定的侦听器。 您可以通过将回调函数保存到变量中(添加后)来完成此操作,以便以后可以引用它:
const doSomething = () => {}
door.on('open', doSomething)
door.removeListener('open', doSomething)
emitter.setMaxListeners()
(emitter.setMaxListeners()
)
Sets the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered.
设置一个可以添加到EventListener对象的侦听器的最大数量,该侦听器默认为10,但可以增加或降低。
door.setMaxListeners(50)
节点的模块度