Monitoring of NODEJS events and triggering of events

Source: Internet
Author: User
Tags emit event listener

Nodejs Event (events)

First, the realization of the event mechanism

Most of the modules in node. js are inherited from the event module (http://nodejs.org/docs/latest/api/events.html). The event module (Events.eventemitter) is an implementation of a simple incident listener pattern. The method realizes the basic event listening mode such as Addlistener/on,once,removelistener,removealllisteners,emit. It is not the same as the events on the front-end DOM tree, because it does not have bubbling, layer-by-level capture, and other events that belong to the DOM, nor Preventdefault (), Stoppropagation (), Stopimmediatepropagation () The method for handling event delivery.

From another point of view, the event listener pattern is also an event hook mechanism that uses event hooks to export internal data or state to external callers. Many of the objects in node. js, mostly with black box features, have fewer function points, and if the object is not in the form of an event hook, the middle value or internal state of the objects during operation is not available to us. This way of using the event hooks allows the programmer to focus on how the component is started and executed, just by focusing on the desired event point. Second, event triggering

The events module provides only one object: events. Eventemitter. The core of Eventemitter is the encapsulation of event launch and event listener functionality. Each event of the Eventemitter consists of an event name and several parameters, which is a string that usually expresses a certain semantics. For each event, Eventemitter supports a number of event listeners. When an event is emitted, the event listener registered to the event is called sequentially, and the event arguments are passed as a callback function parameter.

Let's explain the process in the following example:

//Introduction of Event Module
varEvents = require ("Events");//create an object for event listenervarEmitter =Newevents. Eventemitter ();//Monitoring Event Some_eventEmitter.addlistener ("some_event", function () {Console.log ("Event Trigger, call this callback function");});//Triggering event Some_eventEmitter.emit ("some_event");

Run Result: Event trigger, call this callback function

Example:

varEvents = require ('Events');varEmitter =Newevents. Eventemitter (); Emitter.on ('someevent', function (Arg1, arg2) {Console.log ('Listener1', Arg1, arg2);}); Emitter.on ('someevent', function (Arg1, arg2) {Console.log ('Listener2', Arg1, arg2);}); Emitter.emit ('someevent','byvoid',1991);

The result of the operation is:

Listener1 Byvoid 1991

listener2 byvoid 1991

In the example above, emitter registered two event listeners for event Someevent and then fired the Someevent event. You can see that two event listener callback functions were called in the running results. This is the simplest use of eventemitter. Next, let's introduce the API commonly used by Eventemitter.
? Eventemitter.on (event, listener) registers a listener for the specified event, accepting a string event and a callback function listener. Eventemitter.emit (event, [Arg1], [arg2], [...]) launches event, passing several optional parameters to the event listener's parameter table.
? Eventemitter.once (event, listener) registers a single listener for the specified event, that is, the listener is fired at most once, and the listener is released immediately after the trigger.
? Eventemitter.removelistener (event, listener) removes a listener for the specified event, listener must be a listener that the event has already registered.
? Eventemitter.removealllisteners ([Event]) removes all listeners for all events and, if specified, removes all listeners for the specified event.
See http://nodejs.org/api/events.html For more detailed API documentation.

Think of a jquery custom event quite like this:

// bind the element Hello event element.on ("hello", Function () {  alert ("  Hello world! " );}); // Trigger Hello Event Element.trigger ("hello");
Third, the advanced application of event mechanism

Inherit the event. Eventemitter

Implementing an inherited Eventemitter class is very simple, and the following is an example of node. JS Streaming Object Inheritance Eventemitter:

varUtil = require ("util");varEvents = require ("Events");//to create a constructor that constructs an event objectfunction Stream () {events. Eventemitter.call ( This);} Util.inherits (Stream, events. Eventemitter);//an object for instance creation event listenervarElem =NewStream ();//Monitoring EventsElem.addlistener ("one_event", function () {Console.log ("Event Trigger, call this callback function");});//Triggering event Some_eventElem.emit ("one_event");

Resources:

Http://www.cnblogs.com/zhongweiv/p/nodejs_events.html (many instances)

http://www.infoq.com/cn/articles/tyq-nodejs-event/

Http://www.toolmao.com/nodejs-zhongwen-events-shijian

Http://www.ynpxrz.com/n691854c2023.aspx

Http://www.jb51.net/article/61079.htm

  

Monitoring of NODEJS events and triggering of events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.