Browse Source

events: have events module exports EventEmitter

This change is 100% backwards compatible.

This change will make using `EventEmitter` slightly simpler / nicer and
adheres to the best practice set forth by substack.

```js
var EventEmitter = require("events")

var emitter = new EventEmitter()
```

The only difference is that we now have to set `EventEmitter` as a
property of `EventEmitter` for backwards compatibility like we do with
[`Stream`][1]

We have also set the `usingDomains` property on the `EventEmitter`
constructor itself because that aligns with it's current usage of
`require("events").usingDomains = true`

There are other internals that would benefit from this change as well
like `StringDecoder`
v0.11.6-release
Raynos 12 years ago
committed by isaacs
parent
commit
6ed861dd7f
  1. 11
      lib/events.js

11
lib/events.js

@ -22,11 +22,9 @@
var domain; var domain;
var util = require('util'); var util = require('util');
exports.usingDomains = false;
function EventEmitter() { function EventEmitter() {
this.domain = null; this.domain = null;
if (exports.usingDomains) { if (EventEmitter.usingDomains) {
// if there is an active domain, then attach to it. // if there is an active domain, then attach to it.
domain = domain || require('domain'); domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) { if (domain.active && !(this instanceof domain.Domain)) {
@ -36,7 +34,12 @@ function EventEmitter() {
this._events = this._events || {}; this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined; this._maxListeners = this._maxListeners || undefined;
} }
exports.EventEmitter = EventEmitter; module.exports = EventEmitter;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;
EventEmitter.usingDomains = false;
EventEmitter.prototype.domain = undefined; EventEmitter.prototype.domain = undefined;
EventEmitter.prototype._events = undefined; EventEmitter.prototype._events = undefined;

Loading…
Cancel
Save