@@ -36,67 +36,68 @@ a stack trace and exit the program.
36
36
All EventEmitters emit the event ` 'newListener' ` when new listeners are
37
37
added and ` 'removeListener' ` when a listener is removed.
38
38
39
- ### emitter.addListener(event, listener)
40
- ### emitter.on(event, listener)
39
+ ### Inheriting from 'EventEmitter'
41
40
42
- Adds a listener to the end of the listeners array for the specified ` event ` .
43
- No checks are made to see if the ` listener ` has already been added. Multiple
44
- calls passing the same combination of ` event ` and ` listener ` will result in the
45
- ` listener ` being added multiple times.
41
+ Inheriting from ` EventEmitter ` is no different from inheriting from any other
42
+ constructor function. For example:
46
43
47
- server.on('connection', function (stream) {
48
- console.log('someone connected! ');
49
- } );
44
+ 'use strict';
45
+ const util = require('util ');
46
+ const EventEmitter = require('events' );
50
47
51
- Returns emitter, so calls can be chained.
48
+ function MyEventEmitter() {
49
+ // Initialize necessary properties from `EventEmitter` in this instance
50
+ EventEmitter.call(this);
51
+ }
52
52
53
- ### emitter.once(event, listener)
53
+ // Inherit functions from `EventEmitter`'s prototype
54
+ util.inherits(MyEventEmitter, EventEmitter);
54
55
55
- Adds a ** one time** listener for the event. This listener is
56
- invoked only the next time the event is fired, after which
57
- it is removed.
56
+ ### Class Method: EventEmitter.listenerCount(emitter, event)
58
57
59
- server.once('connection', function (stream) {
60
- console.log('Ah, we have our first user!');
61
- });
58
+ Stability: 0 - Deprecated: Use [emitter.listenerCount][] instead.
62
59
63
- Returns emitter, so calls can be chained .
60
+ Returns the number of listeners for a given event .
64
61
65
- ### emitter.removeListener(event, listener)
62
+ ### Event: 'newListener'
66
63
67
- Removes a listener from the listener array for the specified event.
68
- ** Caution ** : changes array indices in the listener array behind the listener.
64
+ * ` event ` {String} The event name
65
+ * ` listener ` {Function} The event handler function
69
66
70
- var callback = function(stream) {
71
- console.log('someone connected!');
72
- };
73
- server.on('connection', callback);
74
- // ...
75
- server.removeListener('connection', callback);
67
+ This event is emitted * before* a listener is added. When this event is
68
+ triggered, the listener has not been added to the array of listeners for the
69
+ ` event ` . Any listeners added to the event ` name ` in the newListener event
70
+ callback will be added * before* the listener that is in the process of being
71
+ added.
76
72
77
- ` removeListener ` will remove, at most, one instance of a listener from the
78
- listener array. If any single listener has been added multiple times to the
79
- listener array for the specified ` event ` , then ` removeListener ` must be called
80
- multiple times to remove each instance.
73
+ ### Event: 'removeListener'
81
74
82
- Returns emitter, so calls can be chained.
75
+ * ` event ` {String} The event name
76
+ * ` listener ` {Function} The event handler function
83
77
84
- ### emitter.removeAllListeners([ event] )
78
+ This event is emitted * after* a listener is removed. When this event is
79
+ triggered, the listener has been removed from the array of listeners for the
80
+ ` event ` .
85
81
86
- Removes all listeners, or those of the specified event. It's not a good idea to
87
- remove listeners that were added elsewhere in the code, especially when it's on
88
- an emitter that you didn't create (e.g. sockets or file streams).
82
+ ### EventEmitter.defaultMaxListeners
89
83
90
- Returns emitter, so calls can be chained.
84
+ [ ` emitter.setMaxListeners(n) ` ] ( #events_emitter_setmaxlisteners_n ) sets the
85
+ maximum on a per-instance basis.
86
+ This class property lets you set it for * all* ` EventEmitter ` instances,
87
+ current and future, effective immediately. Use with care.
91
88
92
- ### emitter.setMaxListeners(n)
89
+ Note that [ ` emitter.setMaxListeners(n) ` ] ( #events_emitter_setmaxlisteners_n )
90
+ still has precedence over ` EventEmitter.defaultMaxListeners ` .
93
91
94
- By default EventEmitters will print a warning if more than 10 listeners are
95
- added for a particular event. This is a useful default which helps finding
96
- memory leaks. Obviously not all Emitters should be limited to 10. This function
97
- allows that to be increased. Set to ` Infinity ` (or ` 0 ` ) for unlimited.
92
+ ### emitter.addListener(event, listener)
98
93
99
- Returns emitter, so calls can be chained.
94
+ Alias for ` emitter.on(event, listener) ` .
95
+
96
+ ### emitter.emit(event[ , arg1] [ , arg2 ] [ , ...] )
97
+
98
+ Calls each of the listeners in order with the supplied arguments.
99
+
100
+ Returns ` true ` if event had listeners, ` false ` otherwise.
100
101
101
102
### emitter.getMaxListeners()
102
103
@@ -113,16 +114,11 @@ while not being irresponsible and setting a too big number.
113
114
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
114
115
});
115
116
116
- ### EventEmitter.defaultMaxListeners
117
-
118
- [ ` emitter.setMaxListeners(n) ` ] ( #events_emitter_setmaxlisteners_n ) sets the
119
- maximum on a per-instance basis.
120
- This class property lets you set it for * all* ` EventEmitter ` instances,
121
- current and future, effective immediately. Use with care.
117
+ ### emitter.listenerCount(type)
122
118
123
- Note that [ ` emitter.setMaxListeners(n) ` ] ( #events_emitter_setmaxlisteners_n )
124
- still has precedence over ` EventEmitter.defaultMaxListeners ` .
119
+ * ` type ` {Value} The type of event
125
120
121
+ Returns the number of listeners listening to the ` type ` of event.
126
122
127
123
### emitter.listeners(event)
128
124
@@ -133,63 +129,65 @@ Returns a copy of the array of listeners for the specified event.
133
129
});
134
130
console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
135
131
132
+ ### emitter.on(event, listener)
136
133
137
- ### emitter.emit(event[ , arg1] [ , arg2 ] [ , ...] )
138
-
139
- Calls each of the listeners in order with the supplied arguments.
140
-
141
- Returns ` true ` if event had listeners, ` false ` otherwise.
142
-
143
-
144
- ### emitter.listenerCount(type)
145
-
146
- * ` type ` {Value} The type of event
134
+ Adds a listener to the end of the listeners array for the specified ` event ` .
135
+ No checks are made to see if the ` listener ` has already been added. Multiple
136
+ calls passing the same combination of ` event ` and ` listener ` will result in the
137
+ ` listener ` being added multiple times.
147
138
148
- Returns the number of listeners listening to the ` type ` of event.
139
+ server.on('connection', function (stream) {
140
+ console.log('someone connected!');
141
+ });
149
142
150
- ### Class Method: EventEmitter.listenerCount( emitter, event)
143
+ Returns emitter, so calls can be chained.
151
144
152
- Stability: 0 - Deprecated: Use [ emitter.listenerCount][] instead.
145
+ ### emitter.once(event, listener)
153
146
154
- Returns the number of listeners for a given event.
147
+ Adds a ** one time** listener for the event. This listener is
148
+ invoked only the next time the event is fired, after which
149
+ it is removed.
155
150
156
- ### Event: 'newListener'
151
+ server.once('connection', function (stream) {
152
+ console.log('Ah, we have our first user!');
153
+ });
157
154
158
- * ` event ` {String} The event name
159
- * ` listener ` {Function} The event handler function
155
+ Returns emitter, so calls can be chained.
160
156
161
- This event is emitted * before* a listener is added. When this event is
162
- triggered, the listener has not been added to the array of listeners for the
163
- ` event ` . Any listeners added to the event ` name ` in the newListener event
164
- callback will be added * before* the listener that is in the process of being
165
- added.
157
+ ### emitter.removeAllListeners([ event] )
166
158
159
+ Removes all listeners, or those of the specified event. It's not a good idea to
160
+ remove listeners that were added elsewhere in the code, especially when it's on
161
+ an emitter that you didn't create (e.g. sockets or file streams).
167
162
168
- ### Event: 'removeListener'
163
+ Returns emitter, so calls can be chained.
169
164
170
- * ` event ` {String} The event name
171
- * ` listener ` {Function} The event handler function
165
+ ### emitter.removeListener(event, listener)
172
166
173
- This event is emitted * after* a listener is removed. When this event is
174
- triggered, the listener has been removed from the array of listeners for the
175
- ` event ` .
167
+ Removes a listener from the listener array for the specified event.
168
+ ** Caution** : changes array indices in the listener array behind the listener.
176
169
177
- ### Inheriting from 'EventEmitter'
170
+ var callback = function(stream) {
171
+ console.log('someone connected!');
172
+ };
173
+ server.on('connection', callback);
174
+ // ...
175
+ server.removeListener('connection', callback);
178
176
179
- Inheriting from ` EventEmitter ` is no different from inheriting from any other
180
- constructor function. For example:
177
+ ` removeListener ` will remove, at most, one instance of a listener from the
178
+ listener array. If any single listener has been added multiple times to the
179
+ listener array for the specified ` event ` , then ` removeListener ` must be called
180
+ multiple times to remove each instance.
181
181
182
- 'use strict';
183
- const util = require('util');
184
- const EventEmitter = require('events');
182
+ Returns emitter, so calls can be chained.
185
183
186
- function MyEventEmitter() {
187
- // Initialize necessary properties from `EventEmitter` in this instance
188
- EventEmitter.call(this);
189
- }
184
+ ### emitter.setMaxListeners(n)
190
185
191
- // Inherit functions from `EventEmitter`'s prototype
192
- util.inherits(MyEventEmitter, EventEmitter);
186
+ By default EventEmitters will print a warning if more than 10 listeners are
187
+ added for a particular event. This is a useful default which helps finding
188
+ memory leaks. Obviously not all Emitters should be limited to 10. This function
189
+ allows that to be increased. Set to ` Infinity ` (or ` 0 ` ) for unlimited.
193
190
191
+ Returns emitter, so calls can be chained.
194
192
195
193
[ emitter.listenerCount ] : #events_emitter_listenercount_type
0 commit comments