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 customize node.js to implement EventEmitter

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces node.js how to customize the implementation of EventEmitter related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this node.js how to customize the implementation of EventEmitter article will have a harvest, let's take a look at it.

First, what is it?

We have learned that Node adopts an event-driven mechanism, and EventEmitter is the basis for Node to implement event-driven.

On the basis of EventEmitter, almost all Node modules inherit this class. These modules have their own events, can bind / trigger listeners, and implement asynchronous operations.

Many objects in Node.js distribute events, such as fs.readStream objects that trigger an event when a file is opened

These objects that generate events are instances of events.EventEmitter and have an eventEmitter.on () function that binds one or more functions to named events.

II. How to use EventEmitter in nodejs

Node's events module provides only one EventEmitter class, which implements the observer pattern, the basic pattern of Node's asynchronous event-driven architecture.

In this mode, the observed (subject) maintains a group of observers sent (registered) by other objects. If any new object is interested in the subject, register the observer, cancel the subscription if not interested, and notify the observers in turn if the subject has an update.

Const EventEmitter = require ("events") class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter () function callback () {console.log ("event event triggered!") } myEmitter.on ("event", callback) myEmitter.emit ("event") myEmitter.removeListener ("event", callback); III. Implementation process

The basic code is as follows:

/ / event dispatch mechanism (function () {var EventDispatcher = function () {var EventDispatcherClosure = function () {} EventDispatcherClosure.prototype = {/ * Registration event * @ param {Object} key * @ param {Object} fn * / on: function (key, fn) {/ / get the current event object var curEvents = this._getCurEvents (key) / / check whether the event has been registered with var flag = false; for (var I = 0, len = curEvents.length; I < len). Flag +) {if (curEvents [I] .name = = fn.name) {/ / has already appeared, with the newly registered function as the main flag = true; curEvents [I] = fn; break }} if (! flag) {curEvents [curEvents.length] = fn;} this._register (key, curEvents) }, / * dispatch event * @ param {Object} key * @ param {Object} data * / dispatch: function (key) {/ / get the current event object var curEvents = this._getCurEvents (key); var shouldDispatch = true For (var I = 0, len = curEvents.length; shouldDispatch & & I < len; iTunes +) {try {/ / get the parameter var args = []; for (var j = 1, len1 = arguments.length; j < len1) Catch +) {args.push (arguments [j]);} shouldDispatch = curEvents [I] .apply ({}, args);} catch (e) {shouldDispatch = false;}} return shouldDispatch }, remove: function (key) {if (this._getCurEvents (key)) {delete EventDispatcherClosure.events [key] }}, / * obtain event list according to key * @ param {Object} key * / _ getCurEvents: function (key) {return EventDispatcherClosure.events [key] | | [] }, / * Registration time * @ param {Object} key * @ param {Object} events * / _ register: function (key, events) {EventDispatcherClosure.events [key] = events;},}; EventDispatcherClosure.events = {} Return {create: function () {return new EventDispatcherClosure ();};}; window.EventDispatcher = new EventDispatcher (). Create ();}) ()

First define an anonymous function of a global variable, and then hang the global variable on window, which allows us to call during the development process. Add methods such as event distribution, event listening, event deletion, etc., to the prototype chain of anonymous functions.

Calls to event distribution

EventDispatcher.dispatch ("test", obj)

Event monitoring

EventDispatcher.on ("test", function callback (obj) {})

Event deletion

EventDispatcher.on ("test")

The code encapsulation is relatively simple.

This is the end of the article on "how to customize node.js to implement EventEmitter". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to customize node.js to achieve EventEmitter". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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