In vue projects, sometimes we use the global event bus to manage communication between components. In the vue2 project, we can use$emit
,$on
and$off
to encapsulate aneventHub
; in vue3,$on
and$off
are removed, we can use mitt library or tiny-emitter library. In node, there is no need to be so troublesome. It has a built-in events module that can help us monitor and emit events.
First use CommonJS syntax to import theEventEmitter
class, and then generate an instanceemitter
(EventEmitter
is very important. For example, thestream
that will be introduced in subsequent articles is an instance ofEventEmitter
):
const EventEmitter = require('events') const emitter = new EventEmitter()
Then you can useemitter .on()
Monitor the event. The first parameter passed in is the event name. The second parameter is the callback to be executed after listening to the event being emitted. If there are incoming parameters when emitting the event, it will be passed For the callback function, you can obtain it one by one, or you can use the remaining parameters of thefunction as follows: [Recommended related tutorials:nodejs video tutorial,Programming teaching】
// 监听事件 emitter.on('test', (...args) => { console.log(args) // [ 1, 2, 3 ] }) // 发射事件 emitter.emit('test', 1, 2, 3)
emitter.once():
emitter.once('test', () => { console.log('监听到了事件发射') }) emitter.emit('test') emitter.emit('test') // 本次发射不会触发打印
emitter.on('test', () => { console.log('监听到了事件发射,1') }) emitter.on('test', () => { console.log('监听到了事件发射,2') }) emitter.emit('test')
emitter.prependListener()(or
emitter.prependOnceListener(), that is, listen in advance but only once):
emitter.on('test', () => { console.log('监听到了事件发射,1') }) emitter.prependListener('test', () => { console.log('监听到了事件发射,2') }) emitter.emit('test')
emitter.off()(or
emitter.removeListener ()) Remove the monitoring of events, but you need to pass in the corresponding event name and callback function, so our callback when monitoring cannot be directly defined in
emitter.on()## as above # Internal, you need to define it externally and pass in a reference to the callback:function handler(...args) { console.log(args) // [ 1, 2, 3 ] } emitter.on('test', handler) emitter.emit('test', 1, 2, 3) emitter.off('test', handler) emitter.emit('test', '无法被监听到')
Only one listener can be removed, and the listener callback must be passed in. If If you have multiple listeners and want to remove them all, you can useemitter.removeAllListeners()
:emitter.on('test', handler) emitter.on('test', handler) emitter.on('test', handler) emitter.removeAllListeners()
If no parameters are passed in, then Removes all event listeners for all event names. It can also pass in the event name, and all event listeners corresponding to the event name will be removed.
Limit on the number of listeners1 EventEmitter object, a certain The maximum number of listeners for an event name defaults to 10, which can be verified by
emitter.getMaxListeners():
For example, it was written 11 timesconsole.log(emitter.getMaxListeners()) // 10
, an error will be reported, prompting us to useemitter.setMaxListeners()
to increase the maximum limit:
If we If you want to know how many listeners there are for a certain event name on the current EventEmitter object and whether it exceeds the maximum limit, you can use
emitter.listenerCount()to pass in the event name to view:console.log(emitter.listenerCount('test'))
Get event names and listenersUse
emitter.eventNames()to get all event names registered on the current EventEmitter object, and the returned Array composed of event strings:
If you want to get all the listeners corresponding to an event, you can useemitter.on('test1', handler) emitter.on('test2', handler) console.log(emitter.eventNames()) // [ 'test1', 'test2' ]
and pass in the event name:
function handler1() {} function handler2() {} emitter.on('test', handler1) emitter.on('test', handler2) console.log(emitter.listeners('test'))
Copy after login
The results obtained are as follows:For more node-related knowledge, please visit:
nodejs tutorialThe above is the detailed content of A brief analysis of Node's events module. For more information, please follow other related articles on the PHP Chinese website!
Previous article:A brief analysis of node’s path module
Next article:What is Clean Architecture? How to implement it with Node?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2023-04-26 17:59:18
-
2023-04-26 17:47:48
-
2023-04-26 17:41:42
-
2023-04-26 17:37:05
-
2023-04-26 17:31:25
-
2023-04-26 17:27:32
-
2023-04-25 19:57:58
-
2023-04-25 19:53:11
-
2023-04-25 19:49:11
-
2023-04-25 19:41:54
Latest Issues
How to connect to the database with node.js
How to connect nodejs to database, express connection? How to connect nodejs to database, ...
From 2023-10-10 10:06:27
0
9
184
-
About us
Disclaimer
Sitemap
-
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!