1
1
## Events
2
2
3
3
Many objects in Node emit events: a ` net.Server ` emits an event each time
4
- a peer connects to it, a ` fs.readStream ` emits an event when the file is
4
+ a peer connects to it, a ` fs.readStream ` emits an event when the file is
5
5
opened. All objects which emit events are instances of ` events.EventEmitter ` .
6
6
You can access this module by doing: ` require("events"); `
7
7
8
- Typically, event names are represented by a camel-cased string, however,
8
+ Typically, event names are represented by a camel-cased string, however,
9
9
there aren't any strict restrictions on that, as any string will be accepted.
10
10
11
11
Functions can be then be attached to objects, to be executed when an event
@@ -16,9 +16,9 @@ is emitted. These functions are called _listeners_.
16
16
17
17
To access the EventEmitter class, ` require('events').EventEmitter ` .
18
18
19
- When an ` EventEmitter ` instance experiences an error, the typical action is
19
+ When an ` EventEmitter ` instance experiences an error, the typical action is
20
20
to emit an ` 'error' ` event. Error events are treated as a special case in node.
21
- If there is no listener for it, then the default action is to print a stack
21
+ If there is no listener for it, then the default action is to print a stack
22
22
trace and exit the program.
23
23
24
24
All EventEmitters emit the event ` 'newListener' ` when new listeners are
@@ -29,31 +29,31 @@ added.
29
29
30
30
Adds a listener to the end of the listeners array for the specified event.
31
31
32
- server.on('connection', function (stream) {
33
- console.log('someone connected!');
34
- });
32
+ server.on('connection', function (stream) {
33
+ console.log('someone connected!');
34
+ });
35
35
36
36
#### emitter.once(event, listener)
37
37
38
38
Adds a ** one time** listener for the event. The listener is
39
39
invoked only the first time the event is fired, after which
40
40
it is removed.
41
41
42
- server.once('connection', function (stream) {
43
- console.log('Ah, we have our first user!');
44
- });
42
+ server.once('connection', function (stream) {
43
+ console.log('Ah, we have our first user!');
44
+ });
45
45
46
46
#### emitter.removeListener(event, listener)
47
47
48
48
Remove a listener from the listener array for the specified event.
49
49
** Caution** : changes array indices in the listener array behind the listener.
50
50
51
- var callback = function(stream) {
52
- console.log('someone connected!');
53
- };
54
- server.on('connection', callback);
55
- // ...
56
- server.removeListener('connection', callback);
51
+ var callback = function(stream) {
52
+ console.log('someone connected!');
53
+ };
54
+ server.on('connection', callback);
55
+ // ...
56
+ server.removeListener('connection', callback);
57
57
58
58
59
59
#### emitter.removeAllListeners(event)
@@ -66,10 +66,10 @@ Removes all listeners from the listener array for the specified event.
66
66
Returns an array of listeners for the specified event. This array can be
67
67
manipulated, e.g. to remove listeners.
68
68
69
- server.on('connection', function (stream) {
70
- console.log('someone connected!');
71
- });
72
- console.log(util.inspect(server.listeners('connection')); // [ [Function] ]
69
+ server.on('connection', function (stream) {
70
+ console.log('someone connected!');
71
+ });
72
+ console.log(util.inspect(server.listeners('connection')); // [ [Function] ]
73
73
74
74
#### emitter.emit(event, [ arg1] , [ arg2] , [ ...] )
75
75
0 commit comments