From f5c27fc363ee5bbf5f7ad98adc7ced7fbcee1409 Mon Sep 17 00:00:00 2001 From: Tristian Flanagan Date: Wed, 4 Nov 2015 11:45:27 -0500 Subject: [PATCH] doc: sort events alphabetically Reorders, with minimal contextual duplication, the events documentation alphabetically. PR-URL: https://github.com/nodejs/node/pull/3662 Reviewed-By: Evan Lucas Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- doc/api/events.markdown | 202 ++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 102 deletions(-) diff --git a/doc/api/events.markdown b/doc/api/events.markdown index 9dc384a8505..c14994e5bf4 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -36,7 +36,99 @@ a stack trace and exit the program. All EventEmitters emit the event `'newListener'` when new listeners are added and `'removeListener'` when a listener is removed. +### Inheriting from 'EventEmitter' + +Inheriting from `EventEmitter` is no different from inheriting from any other +constructor function. For example: + + 'use strict'; + const util = require('util'); + const EventEmitter = require('events'); + + function MyEventEmitter() { + // Initialize necessary properties from `EventEmitter` in this instance + EventEmitter.call(this); + } + + // Inherit functions from `EventEmitter`'s prototype + util.inherits(MyEventEmitter, EventEmitter); + +### Class Method: EventEmitter.listenerCount(emitter, event) + + Stability: 0 - Deprecated: Use [emitter.listenerCount][] instead. + +Returns the number of listeners for a given event. + +### Event: 'newListener' + +* `event` {String} The event name +* `listener` {Function} The event handler function + +This event is emitted *before* a listener is added. When this event is +triggered, the listener has not been added to the array of listeners for the +`event`. Any listeners added to the event `name` in the newListener event +callback will be added *before* the listener that is in the process of being +added. + +### Event: 'removeListener' + +* `event` {String} The event name +* `listener` {Function} The event handler function + +This event is emitted *after* a listener is removed. When this event is +triggered, the listener has been removed from the array of listeners for the +`event`. + +### EventEmitter.defaultMaxListeners + +[`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) sets the +maximum on a per-instance basis. +This class property lets you set it for *all* `EventEmitter` instances, +current and future, effective immediately. Use with care. + +Note that [`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) +still has precedence over `EventEmitter.defaultMaxListeners`. + ### emitter.addListener(event, listener) + +Alias for `emitter.on(event, listener)`. + +### emitter.emit(event[, arg1][, arg2][, ...]) + +Calls each of the listeners in order with the supplied arguments. + +Returns `true` if event had listeners, `false` otherwise. + +### emitter.getMaxListeners() + +Returns the current max listener value for the emitter which is either set by +[`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) or defaults to +[`EventEmitter.defaultMaxListeners`](#events_eventemitter_defaultmaxlisteners). + +This can be useful to increment/decrement max listeners to avoid the warning +while not being irresponsible and setting a too big number. + + emitter.setMaxListeners(emitter.getMaxListeners() + 1); + emitter.once('event', function () { + // do stuff + emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0)); + }); + +### emitter.listenerCount(type) + +* `type` {Value} The type of event + +Returns the number of listeners listening to the `type` of event. + +### emitter.listeners(event) + +Returns a copy of the array of listeners for the specified event. + + server.on('connection', function (stream) { + console.log('someone connected!'); + }); + console.log(util.inspect(server.listeners('connection'))); // [ [Function] ] + ### emitter.on(event, listener) Adds a listener to the end of the listeners array for the specified `event`. @@ -62,6 +154,14 @@ it is removed. Returns emitter, so calls can be chained. +### emitter.removeAllListeners([event]) + +Removes all listeners, or those of the specified event. It's not a good idea to +remove listeners that were added elsewhere in the code, especially when it's on +an emitter that you didn't create (e.g. sockets or file streams). + +Returns emitter, so calls can be chained. + ### emitter.removeListener(event, listener) Removes a listener from the listener array for the specified event. @@ -81,14 +181,6 @@ multiple times to remove each instance. Returns emitter, so calls can be chained. -### emitter.removeAllListeners([event]) - -Removes all listeners, or those of the specified event. It's not a good idea to -remove listeners that were added elsewhere in the code, especially when it's on -an emitter that you didn't create (e.g. sockets or file streams). - -Returns emitter, so calls can be chained. - ### emitter.setMaxListeners(n) By default EventEmitters will print a warning if more than 10 listeners are @@ -98,98 +190,4 @@ allows that to be increased. Set to `Infinity` (or `0`) for unlimited. Returns emitter, so calls can be chained. -### emitter.getMaxListeners() - -Returns the current max listener value for the emitter which is either set by -[`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) or defaults to -[`EventEmitter.defaultMaxListeners`](#events_eventemitter_defaultmaxlisteners). - -This can be useful to increment/decrement max listeners to avoid the warning -while not being irresponsible and setting a too big number. - - emitter.setMaxListeners(emitter.getMaxListeners() + 1); - emitter.once('event', function () { - // do stuff - emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0)); - }); - -### EventEmitter.defaultMaxListeners - -[`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) sets the -maximum on a per-instance basis. -This class property lets you set it for *all* `EventEmitter` instances, -current and future, effective immediately. Use with care. - -Note that [`emitter.setMaxListeners(n)`](#events_emitter_setmaxlisteners_n) -still has precedence over `EventEmitter.defaultMaxListeners`. - - -### emitter.listeners(event) - -Returns a copy of the array of listeners for the specified event. - - server.on('connection', function (stream) { - console.log('someone connected!'); - }); - console.log(util.inspect(server.listeners('connection'))); // [ [Function] ] - - -### emitter.emit(event[, arg1][, arg2][, ...]) - -Calls each of the listeners in order with the supplied arguments. - -Returns `true` if event had listeners, `false` otherwise. - - -### emitter.listenerCount(type) - -* `type` {Value} The type of event - -Returns the number of listeners listening to the `type` of event. - -### Class Method: EventEmitter.listenerCount(emitter, event) - - Stability: 0 - Deprecated: Use [emitter.listenerCount][] instead. - -Returns the number of listeners for a given event. - -### Event: 'newListener' - -* `event` {String} The event name -* `listener` {Function} The event handler function - -This event is emitted *before* a listener is added. When this event is -triggered, the listener has not been added to the array of listeners for the -`event`. Any listeners added to the event `name` in the newListener event -callback will be added *before* the listener that is in the process of being -added. - - -### Event: 'removeListener' - -* `event` {String} The event name -* `listener` {Function} The event handler function - -This event is emitted *after* a listener is removed. When this event is -triggered, the listener has been removed from the array of listeners for the -`event`. - -### Inheriting from 'EventEmitter' - -Inheriting from `EventEmitter` is no different from inheriting from any other -constructor function. For example: - - 'use strict'; - const util = require('util'); - const EventEmitter = require('events'); - - function MyEventEmitter() { - // Initialize necessary properties from `EventEmitter` in this instance - EventEmitter.call(this); - } - - // Inherit functions from `EventEmitter`'s prototype - util.inherits(MyEventEmitter, EventEmitter); - - [emitter.listenerCount]: #events_emitter_listenercount_type