Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Events module of Node.js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the Events module of Node.js". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Node.js 's Events module.

Events module

Refer to the 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 attribute EventEmitter,EventEmitter the core is event emission and event listener.

Most of the modules in Node are inherited from Events modules.

The Events module is Node's implementation of the publish / subscribe model (publish/subscribe). One object passes a message to another object through this module.

This module provides a constructor through the EventEmitter attribute. An instance of this constructor has an on method that can be used to listen for specified events and trigger callbacks.

Any object can publish a specified event, which is monitored by the on method of the EventEmitter instance.

Publish and subscribe model

For the publish and subscribe model, you can refer to my previous blog post.

With regard to the publish and subscribe model in Events, we need to be aware of several common methods.

Subscription method: the on method is used to subscribe to events, and subscription corresponds to a method in an one-to-many relationship.

Publishing method: events that emit uses to perform subscriptions.

Unsubscribe: the off method removes the corresponding event listener.

Subscribe once: the once binding event automatically deletes the subscribed event when executed.

On and emit

The first parameter of the on method is used to set the class name, and the second parameter is also a function that receives the parameters passed in at the time of publication.

The first parameter of the emit method is the class name, and the subsequent arguments are all parameters passed into the on method function.

For specific applications of on and emit, please refer to the following simple Demo.

Const EventEmitter = require ('events'); / / customize a constructor function Cat () {} / / prototype inheritance needs to call the inheritance method Object.setPrototypeOf (Cat.prototype, EventEmitter.prototype); let cat = new Cat (); const sleep = (a, b) = > {console.log (a,' sleep');}; const eat = (a, b) = > {console.log (b, 'eat');} Cat.on ('cat', sleep) cat.on ('cat', eat) setTimeout (() = > {/ / moustache eat / / little fat fairy sleeps cat.emit ('cat', 'little fat fairy', 'moustache')}, 1000)

Now we can implement a set of on and emit methods.

Function EventEmitter () {this._event = {}} / / on method EventEmitter.prototype.on = function (eventName, callBack) {if (! this._event) {this._event = {}} if (this._ [callBack]) {this._ [callBack] / / equivalent to {eventName: [fn1,fn2]} else {this._ [eventname] = [callBack] / equivalent to {eventName: [fn1]}} / / emit method EventEmitter.prototype.emit = function (eventName,... args) {this._ [eventName] .forEach (fn = > {fn (... args)});} off

The first parameter of the off method sets the class name, and the second argument passes in the function callback that needs to be removed.

/ /... setTimeout () = > {/ / Little fat fairy sleeps cat.emit ('cat', 'little fat fairy', 'moustache') cat.off ('cat', 'sleep'); / / moustache eats cat.emit ('cat', 'little fat fairy', 'moustache')}, 1000)

So we can roughly tell, remove the same function as the function we passed in, and we quickly think of the filter method.

/ off method EventEmitter.prototype.off = function (eventName, callBack) {if (this._event & & this._ event [eventName]) {this._ [eventName] = this._ [eventName] .filter (fn = > fn! = = callBack & & fn.c! = = callBack / / fn.c refer to the once method implementation below)}} once

The first parameter of the once method is used to set the class name, and the second argument passes in a function callback that only needs to be executed once.

/ /... const demolition = () = > {console.log ('split');} cat.once ('cat', demolition) setTimeout () = > {/ /. Dismantle cat.emit ('cat', 'little fat fairy', 'moustache')}, 1000)

This way we can implement this method based on the on and off that we implemented earlier.

/ / once method EventEmitter.prototype.once = function (eventName, callBack) {const one = () = > {callBack (); this.off (eventName, one);} this.on (eventName, one);}

It seems that there is nothing wrong with this method, and it is all correct to implement it.

But in a special case, there is still a mistake.

That is if we have removed the once method through the off method before executing it.

The method we implemented does not meet this requirement, so we still need to make some changes to the once method (the off method has already been handled).

Add a custom attribute to "cache" the function.

EventEmitter.prototype.once = function (eventName, callBack) {const one = () = > {/ /...} one.c = callBack; / / customize an attribute / /.}

So we implement the once method.

At this point, I believe you have a deeper understanding of "how to use the Events module of Node.js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report