Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
[lib] trailing whitespace removed, named functions, internally used f…
Browse files Browse the repository at this point in the history
…unctions privatized (reduced surface area), onAny returns , [doc] lines wrapped
  • Loading branch information
hij1nx committed Aug 4, 2011
1 parent 189ebb5 commit 82397be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 72 deletions.
33 changes: 10 additions & 23 deletions doc/api/events.markdown
@@ -1,46 +1,33 @@
## Events

Many objects in Node emit events: a `net.Server` emits an event each time
a peer connects to it, a `fs.readStream` emits an event when the file is
opened. All objects which emit events are instances of `events.EventEmitter`.
You can access this module by doing: `require("events");`
Many objects in Node emit events: a `net.Server` emits an event each time a peer connects to it, a `fs.readStream` emits an event when the file is opened. All objects which emit events are instances of `events.EventEmitter`. You can access this module by doing: `require("events");`

Typically, event names are represented by a camel-cased string, however,
there aren't any strict restrictions on that, as any string will be accepted.
Typically, event names are represented by a camel-cased string, however, there aren't any strict restrictions on that, as any string will be accepted.

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


**Namespaces** with **Wildcards**
To use namespaces/wildcards, pass the `wildcard` option into the EventEmitter constructor.
When namespaces/wildcards are enabled, events can either be strings (`foo.bar`) separated
by a delimiter or arrays (`['foo', 'bar']`). The delimiter is also configurable as a
constructor option.
To use namespaces/wildcards, pass the `wildcard` option into the EventEmitter constructor. When namespaces/wildcards are enabled, events can either be strings (`foo.bar`) separated by a delimiter or arrays (`['foo', 'bar']`). The delimiter is also configurable as a constructor option.

An event name passed to any event emitter method can contain a wild card (the `*` character).
If the event name is a string, a wildcard may appear as `foo.*`. If the event name is an array,
the wildcard may appear as `['foo', '*']`.
An event name passed to any event emitter method can contain a wild card (the `*` character). If the event name is a string, a wildcard may appear as `foo.*`. If the event name is an array, the wildcard may appear as `['foo', '*']`.

If either of the above described events were passed to the `on` method, subsequent emits such
as the following would be observed...
If either of the above described events were passed to the `on` method, subsequent emits such as the following would be observed...


emitter.emit(['foo.bazz']);
emitter.emit(['foo', 'bar']);


When emitting or listening for an event that contains a wildcard, the name of the actual event is available by accessing `this.event` from within the callback.

### 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.
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
added.
All EventEmitters emit the event `'newListener'` when new listeners are added.

#### emitter.addListener(event, listener)
#### emitter.on(event, listener)
Expand Down
103 changes: 54 additions & 49 deletions lib/events.js
Expand Up @@ -19,49 +19,37 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var EventEmitter = function EventEmitter(conf) {
this.init();
this.setConf(conf);
};

exports.EventEmitter = EventEmitter;
var isArray = Array.isArray;
var defaultMaxListeners = 10;

EventEmitter.prototype.init = function() {
this._events = new Object; // faster in v8
this.defaultMaxListeners = 10;
};
function init() {
this._events = new Object;
}

var isArray = Array.isArray;
function configure(conf) {

// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. 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.
if (conf) {
this.wildcard = conf.wildcard;
this.delimiter = conf.delimiter || '.';

EventEmitter.prototype.setConf = function(conf) {
this.wildcard = conf && conf.wildcard;
this.delimiter = conf && conf.delimiter || '.';
if(this.wildcard) {
this.listenerTree = new Object;
if (this.wildcard) {
this.listenerTree = new Object;
}
}
};
}

EventEmitter.prototype.setMaxListeners = function(n) {
this._events || this.init();
this._events.maxListeners = n;
};

EventEmitter.prototype.event = '';
function EventEmitter(conf) {
this._events = new Object;
configure.call(this, conf);
}

var searchListenerTree = function(handlers, type, tree, i) {
function searchListenerTree(handlers, type, tree, i) {
if (!tree) {
return
return;
}

var listeners;

if (i === type.length && tree._listeners) {
//
// If at the end of the event(s) list and the tree has listeners
Expand Down Expand Up @@ -103,11 +91,11 @@ var searchListenerTree = function(handlers, type, tree, i) {
//
searchListenerTree(handlers, type, tree['*'], i+1);
}

return listeners;
};

var growListenerTree = function(type, listener) {
function growListenerTree(type, listener) {

type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();

Expand Down Expand Up @@ -136,7 +124,7 @@ var growListenerTree = function(type, listener) {

if (!tree._listeners.warned) {

var m = this.defaultMaxListeners;
var m = defaultMaxListeners;

if (m > 0 && tree._listeners.length > m) {

Expand All @@ -156,6 +144,20 @@ var growListenerTree = function(type, listener) {
return true;
};

// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. 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.

EventEmitter.prototype.setMaxListeners = function(n) {
this._events || init.call(this);
this._events.maxListeners = n;
};

EventEmitter.prototype.event = '';

EventEmitter.prototype.once = function(event, fn) {
this.many(event, 1, fn);
return this;
Expand Down Expand Up @@ -183,11 +185,11 @@ EventEmitter.prototype.many = function(event, ttl, fn) {
};

EventEmitter.prototype.emit = function() {
this._events || this.init();
this._events || init.call(this);

var type = arguments[0];
this.event = type;

// If there is no 'error' event listener then throw.

if (type === 'newListener') {
Expand Down Expand Up @@ -225,7 +227,7 @@ EventEmitter.prototype.emit = function() {
searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
}
else {
handler = this._events[type];
handler = this._events[type];
}

if (typeof handler === 'function') {
Expand All @@ -248,7 +250,7 @@ EventEmitter.prototype.emit = function() {
handler.apply(this, args);
}
return true;
}
}
else if (handler) {
var l = arguments.length;
var args = new Array(l - 1);
Expand All @@ -264,7 +266,7 @@ EventEmitter.prototype.emit = function() {
};

EventEmitter.prototype.on = function(type, listener) {
this._events || this.init();
this._events || init.call(this);

// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
Expand Down Expand Up @@ -294,7 +296,7 @@ EventEmitter.prototype.on = function(type, listener) {
if (this._events.maxListeners !== undefined) {
m = this._events.maxListeners;
} else {
m = this.defaultMaxListeners;
m = defaultMaxListeners;
}

if (m && m > 0 && this._events[type].length > m) {
Expand Down Expand Up @@ -323,6 +325,7 @@ EventEmitter.prototype.onAny = function(fn) {

// Add the function to the event listener collection.
this._all.push(fn);
return this;
};

EventEmitter.prototype.addListener = EventEmitter.prototype.on;
Expand All @@ -337,7 +340,7 @@ EventEmitter.prototype.un = function(type, listener) {
if(this.wildcard) {
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
var leaf = searchListenerTree.call(this, null, ns, this.listenerTree, 0);

if('undefined' === typeof leaf) { return this; }
handlers = leaf._listeners;
}
Expand All @@ -360,8 +363,8 @@ EventEmitter.prototype.un = function(type, listener) {
}
}

if (position < 0) {
return this;
if (position < 0) {
return this;
}

if(this.wildcard) {
Expand All @@ -379,7 +382,7 @@ EventEmitter.prototype.un = function(type, listener) {
delete this._events[type];
}
}
}
}
else if (handlers === listener ||
(handlers.listener && handlers.listener === listener) ||
(handlers._origin && handlers._origin === listener)) {
Expand Down Expand Up @@ -414,7 +417,7 @@ EventEmitter.prototype.removeListener = EventEmitter.prototype.un;

EventEmitter.prototype.removeAllListeners = function(type) {
if (arguments.length === 0) {
!this._events || this.init();
!this._events || init.call(this);
return this;
}

Expand All @@ -440,11 +443,13 @@ EventEmitter.prototype.listeners = function(type) {
return handlers;
}

this._events || this.init();
this._events || init.call(this);

if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
return this._events[type];
};

exports.EventEmitter = EventEmitter;

0 comments on commit 82397be

Please sign in to comment.