Skip to content

Latest commit

 

History

History
116 lines (71 loc) · 3.08 KB

README.md

File metadata and controls

116 lines (71 loc) · 3.08 KB

Events module

This is a AMD port of the node.js events module. Many objects and modules emit events and these are instances of events.EventEmitter.

Functions can be attached to objects, to be executed when an event is emitted. These functions are called listeners.

API

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. If there is no listener for it, then the default action is for the error to throw.

All EventEmitters emit the event 'newListener' when new listeners are added.

define('myModule', ['events'], function(events) {

// create an event emitter
var emitter = new events.EventEmitter();

});

emitter.setMaxListeners(n)

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.

  • n - Number - The maximum number of listeners

emitter.emit(event, [arg1], [arg2], [...])

Execute each of the listeners in order with the supplied arguments.

  • event - String - The event name/id to fire

emitter.on(event, listener) | emitter.addListener(event, listener)

Adds a listener to the end of the listeners array for the specified event.

  • event - String - The event name/id to listen for
  • listener - Function - The function to bind to the event
session.on('change', function (userCtx) {
    console.log('session changed!');
});

emitter.once(event, listener)

Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.

  • event- - String - The event name/id to listen for
  • listener - Function - The function to bind to the event
db.once('unauthorized', function (req) {
    // this event listener will fire once, then be unbound
});

emitter.removeListener(event, listener)

Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener.

  • event - String - The event name/id to remove the listener from
  • listener - Function - The listener function to remove
var callback = function (init) {
    console.log('duality app loaded');
};
devents.on('init', callback);
// ...
devents.removeListener('init', callback);

emitter.removeAllListeners([event])

Removes all listeners, or those of the specified event.

  • event - String - Event name/id to remove all listeners for (optional)

emitter.listeners(event)

Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.

  • events - String - The event name/id to return listeners for
session.on('change', function (stream) {
    console.log('session changed');
});
console.log(util.inspect(session.listeners('change'))); // [ [Function] ]