events: have events module exports EventEmitter
authorRaynos <raynos2@gmail.com>
Mon, 13 May 2013 19:04:06 +0000 (13:04 -0600)
committerisaacs <i@izs.me>
Tue, 20 Aug 2013 00:27:08 +0000 (17:27 -0700)
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`

lib/events.js

index 7298008..e2e39fd 100644 (file)
 var domain;
 var util = require('util');
 
-exports.usingDomains = false;
-
 function EventEmitter() {
   this.domain = null;
-  if (exports.usingDomains) {
+  if (EventEmitter.usingDomains) {
     // if there is an active domain, then attach to it.
     domain = domain || require('domain');
     if (domain.active && !(this instanceof domain.Domain)) {
@@ -36,7 +34,12 @@ function EventEmitter() {
   this._events = this._events || {};
   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._events = undefined;