This article will take you to understand the Events module inNode.jsand introduce the publish and subscribe model in Events. I hope it will be helpful to everyone!
Reference official website: events event trigger | Node.js
http ://nodejs.cn/api/events.html
Events
module is the most important module of Node. It provides an attributeEventEmitter
,# The core of ##EventEmitteris event emission and event listener. Most of the modules in
Nodeinherit from theEventsmodule.
module is Node’s implementation of
publish/subscribe mode(publish/subscribe). One object passes messages to another object through this module.
attribute. Instances of this constructor have
onmethods that can be used to listen to specified events and trigger callback functions.
instance.
ForRegarding the publish and subscribe model inpublish and subscribe model, you can refer to my previous blog article.
Events, we must first understand several of its common methods.
method is used to subscribe to events. Subscription maps methods into a one-to-many relationship.
Used to execute subscribed events.
method can remove the corresponding event listener.
The bound event automatically deletes the subscribed event after execution.
onThe first parameter of the method is used to set the class name, and the second parameter is also a Function, which can receive parameters passed in when publishing.
emitThe first parameter of the method is the class name, and the subsequent parameters are the parameters passed into the
onmethod function.
onand
emitFor specific applications, please refer to the simple Demo below.
const EventEmitter = require('events'); // 自定义一个 构造函数 function Cat() {} // 原型继承 需要通过实例来调用继承方法 Object.setPrototypeOf(Cat.prototype, EventEmitter.prototype); let cat = new Cat(); const sleep = (a, b) => { console.log(a, '睡'); }; const eat = (a, b) => { console.log(b, '吃'); }; cat.on('猫咪', sleep) cat.on('猫咪', eat) setTimeout(() => { // 小胡子 吃 // 小胖仙 睡 cat.emit('猫咪', '小胖仙', '小胡子') }, 1000);
onand
emitmethods.
function EventEmitter() { this._event = {} } // on 方法 EventEmitter.prototype.on = function (eventName, callBack) { if (!this._event) { this._event = {} } if (this._event[eventName]) { this._event[eventName].push(callBack) // 相当于 {eventName:[fn1,fn2]} } else { this._event[eventName] = [callBack]; // 相当于 {eventName:[fn1]} } } // emit 方法 EventEmitter.prototype.emit = function (eventName, ...args) { this._event[eventName].forEach(fn => { fn(...args) }); }
offThe first parameter of the method is used to set the class name, and the second parameter passed in needs to be removed function callback.
// ... setTimeout(() => { // 小胡子 吃 // 小胖仙 睡 cat.emit('猫咪', '小胖仙', '小胡子') cat.off('猫咪', sleep); // 小胡子 吃 cat.emit('猫咪', '小胖仙', '小胡子') }, 1000);
filtermethod.
// off 方法 EventEmitter.prototype.off = function (eventName, callBack) { if (this._event && this._event[eventName]) { this._event[eventName] = this._event[eventName].filter( fn => fn !== callBack && fn.c !== callBack // fn.c参考下面的once方法实现 ) } }
onceThe first parameter of the method is used to set the class name. The second parameter is passed in and only needs to be executed once. function callback.
// ... const demolition =() => { console.log('拆家'); } cat.once('猫咪', demolition) setTimeout(() => { // ...... 拆家 cat.emit('猫咪', '小胖仙', '小胡子') }, 1000);
onand
off.
// once 方法 EventEmitter.prototype.once = function (eventName, callBack) { const one = () => { callBack(); this.off(eventName, one); } this.on(eventName, one); }
offmethod before executing the
oncemethod.
oncemethod
(theoffmethod has already been processed)
.
EventEmitter.prototype.once = function (eventName, callBack) { const one = () => { // ... } one.c = callBack; // 自定义一个属性 // ... }
oncemethod.
nodejs tutorial! !
The above is the detailed content of Node.js learning and chatting about the Events module. For more information, please follow other related articles on the PHP Chinese website!