node. js Api--events (Event)

Source: Internet
Author: User
Tags emit

//Descriptionthe Node API version is v0.10.31.
Chinese Reference: Http://nodeapi.ucdok.com/#/api/,http://blog.sina.com.cn/oleoneoythis Keweibo main note. Directory Events
0 class:events. Eventemitter Emitter.addlistener (event, listener) Emitter.on (event, listener) emitter.once (event, listener) Emitter.removelistener (event, listener) Emitter.removealllistener ([events]) emitter.setmaxlistener (n) emitter.listeners (event) Emitter.emit (event, [Arg1], [arg2], [...]) Class Method:EventEmitter.listenerCount (emitter, event) Event: ' New listener ' Event: ' RemoveListener ' Event
Stability: 4-api freeze
Many objects of Node emit the event:net. The Server emits an event each time the peering connection comes up,Fs.readstream an event when the file is opened. All objects that emit events are instances of events.eventemitter . You can access this module by:require ("events");
Typically, the event name is represented by a camel-formatted string, however, there is no strict constraint, because any string can be accepted.
The function can then be linked to the object, and the function is executed when an event is fired. These functions are called listeners . Inside the listener function, This points to the Eventemitterassociated with the listener. Class:events. Eventemitter

In order to access the Eventemitter class,require (' events '). Eventemitter.
When an Eventemitter instance encounters an error, the typical operation is to emit an ' error ' event. The Error event in node is treated as a special case. If it does not have any listeners, the default action is to print the stack trace information and then exit the program.
All Eventemitter will emit event ' Newlistener 'when the new listener is added, and emit event ' RemoveListener 'when the listener is removed.

Emitter.addlistener (event, listener)
Emitter.on (event, listener)

Adds a listener to the end of the listener array for a particular event.

1 function (stream) {2   console.log (' Someone connected! ') ); 3 });

Returns the emitter, so it can be called in a chained type.

Emitter.once (event, listener)

Adds a listener that executes once for the event. The listener is called only the next time the event is triggered, and then it is removed.

1 function (streams) {2   console.log (' Ah, we have our first user! ' ); 3 });

Returns the emitter, so it can be called in a chained type.

Emitter.removelistener (event, listener)

Removes the specified listener from the listener array for the specified event. warning : This will change the array subscript for the listener elements behind the listener.

1 var function (stream) {2   console.log (' Someone connected! ') ); 3 }; 4 server.on (' Connection ', callback); 5 //  ... 6 server.removelistener (' connection ', callback);

Returns the emitter, so it can be called in a chained type.

Emitter.removealllistener ([Event])

Remove all listeners, or all listeners at a specific time. Removing a listener that is added elsewhere in the code is not a good idea, especially if it is associated with a transmitter that is not created by you (such as a socket or file stream).
Returns the emitter, so it can be called in a chained type.

Emitter.setmaxlisteners (N)

By default, if more than 10 listeners are added to a specific event, Eventemitter will print a warning. This is a useful default value that can help you find a memory leak. Obviously not all transmitters should be limited to 10. This function allows it to grow. If no limit is set to 0.

Emitter.listeners (Event)

Returns an array of listeners for a particular event.

1 function (stream) {2   console.log (' Someone connected! ') ); 3 }); 4 // [[Function]]
Emitter.emit (event, [Arg1], [arg2], [...])

Executes each listener sequentially, using the supplied parameters.
Returns falseif a listener returns true.

Class Method:EventEmitter.listenerCount (emitter, event)

Returns the number of listeners given the event.

Event: ' Newlistener '

Event String type events Name
Listener function Type event handler

This event is emitted each time a new listener is added. If listener is in the list returned by emitter.listeners (event) , the behavior of this function is not explicitly specified.

Event: ' RemoveListener '

Event String type events Name
Listener function Type event handler

This event is emitted each time the listener is removed. If listener is in the list returned by emitter.listeners (event) , the behavior of this function is not explicitly specified.

node. js Api--events (Event)

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.