Reworked and cleaned up documentation for Events
This commit is contained in:
parent
f9ad171085
commit
a6c3d7121b
@ -1,86 +1,82 @@
|
|||||||
## EventEmitter
|
## Events
|
||||||
|
|
||||||
Many objects in Node emit events: a TCP server emits an event each time
|
Many objects in Node emit events: a `net.Server` emits an event each time
|
||||||
there is a stream, a child process emits an event when it exits. All
|
a peer connects to it, a `fs.readStream` emits an event when the file is
|
||||||
objects which emit events are instances of `events.EventEmitter`.
|
opened. All objects which emit events are instances of `events.EventEmitter`.
|
||||||
|
You can access this module by doing: `require("events");`
|
||||||
|
|
||||||
Events are represented by a camel-cased string. Here are some examples:
|
Typically, event names are represented by a camel-cased string, however,
|
||||||
`'connection'`, `'data'`, `'messageBegin'`.
|
there aren't any strict restrictions on that, as any string will be accepted.
|
||||||
|
|
||||||
Functions can be then be attached to objects, to be executed when an event
|
Functions can be then be attached to objects, to be executed when an event
|
||||||
is emitted. These functions are called _listeners_.
|
is emitted. These functions are called _listeners_.
|
||||||
|
|
||||||
`require('events').EventEmitter` to access the `EventEmitter` class.
|
|
||||||
|
### events.EventEmitter
|
||||||
|
|
||||||
|
To access the EventEmitter class, `require('events').EventEmitter`.
|
||||||
|
|
||||||
|
When an `EventEmitter` instance experiences an error, the typical action is
|
||||||
|
to emit an `'error'` event. Error events are treated as a special case in node.
|
||||||
|
If there is no listener for it, then the default action is to print a stack
|
||||||
|
trace and exit the program.
|
||||||
|
|
||||||
All EventEmitters emit the event `'newListener'` when new listeners are
|
All EventEmitters emit the event `'newListener'` when new listeners are
|
||||||
added.
|
added.
|
||||||
|
|
||||||
When an `EventEmitter` experiences an error, the typical action is to emit an
|
#### emitter.addListener(event, listener)
|
||||||
`'error'` event. Error events are special--if there is no handler for them
|
#### emitter.on(event, listener)
|
||||||
they will print a stack trace and exit the program.
|
|
||||||
|
|
||||||
### Event: 'newListener'
|
|
||||||
|
|
||||||
`function (event, listener) { }`
|
|
||||||
|
|
||||||
This event is emitted any time someone adds a new listener.
|
|
||||||
|
|
||||||
### Event: 'error'
|
|
||||||
|
|
||||||
`function (exception) { }`
|
|
||||||
|
|
||||||
If an error was encountered, then this event is emitted. This event is
|
|
||||||
special - when there are no listeners to receive the error Node will
|
|
||||||
terminate execution and display the exception's stack trace.
|
|
||||||
|
|
||||||
### emitter.on(event, listener)
|
|
||||||
|
|
||||||
Adds a listener to the end of the listeners array for the specified event.
|
Adds a listener to the end of the listeners array for the specified event.
|
||||||
|
|
||||||
server.on('connection', function (stream) {
|
server.on('connection', function (stream) {
|
||||||
console.log('someone connected!');
|
console.log('someone connected!');
|
||||||
});
|
});
|
||||||
|
|
||||||
### emitter.once(event, listener)
|
#### emitter.once(event, listener)
|
||||||
|
|
||||||
Adds a **one time** listener for the event. The listener is
|
Adds a **one time** listener for the event. The listener is
|
||||||
invoked only the first time the event is fired, after which
|
invoked only the first time the event is fired, after which
|
||||||
it is removed.
|
it is removed.
|
||||||
|
|
||||||
server.once('connection', function (stream) {
|
server.once('connection', function (stream) {
|
||||||
console.log('Ah, we have our first user!');
|
console.log('Ah, we have our first user!');
|
||||||
});
|
});
|
||||||
|
|
||||||
### emitter.removeListener(event, listener)
|
#### emitter.removeListener(event, listener)
|
||||||
|
|
||||||
Remove a listener from the listener array for the specified event.
|
Remove a listener from the listener array for the specified event.
|
||||||
**Caution**: changes array indices in the listener array behind the listener.
|
**Caution**: changes array indices in the listener array behind the listener.
|
||||||
|
|
||||||
var callback = function(stream) {
|
var callback = function(stream) {
|
||||||
console.log('someone connected!');
|
console.log('someone connected!');
|
||||||
};
|
};
|
||||||
server.on('connection', callback);
|
server.on('connection', callback);
|
||||||
// ...
|
// ...
|
||||||
server.removeListener('connection', callback);
|
server.removeListener('connection', callback);
|
||||||
|
|
||||||
|
|
||||||
### emitter.removeAllListeners(event)
|
#### emitter.removeAllListeners(event)
|
||||||
|
|
||||||
Removes all listeners from the listener array for the specified event.
|
Removes all listeners from the listener array for the specified event.
|
||||||
|
|
||||||
|
|
||||||
### emitter.listeners(event)
|
#### emitter.listeners(event)
|
||||||
|
|
||||||
Returns an array of listeners for the specified event. This array can be
|
Returns an array of listeners for the specified event. This array can be
|
||||||
manipulated, e.g. to remove listeners.
|
manipulated, e.g. to remove listeners.
|
||||||
|
|
||||||
server.on('connection', function (stream) {
|
server.on('connection', function (stream) {
|
||||||
console.log('someone connected!');
|
console.log('someone connected!');
|
||||||
});
|
});
|
||||||
console.log(util.inspect(server.listeners('connection'));
|
console.log(util.inspect(server.listeners('connection')); // [ [Function] ]
|
||||||
// [ [Function] ]
|
|
||||||
|
|
||||||
|
#### emitter.emit(event, [arg1], [arg2], [...])
|
||||||
### emitter.emit(event, [arg1], [arg2], [...])
|
|
||||||
|
|
||||||
Execute each of the listeners in order with the supplied arguments.
|
Execute each of the listeners in order with the supplied arguments.
|
||||||
|
|
||||||
|
#### Event: 'newListener'
|
||||||
|
|
||||||
|
`function (event, listener) { }`
|
||||||
|
|
||||||
|
This event is emitted any time someone adds a new listener.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user