Skip to content

Commit

Permalink
Inherit Spooky from EventEmitter correctly
Browse files Browse the repository at this point in the history
Previously, the constructor did not pass new instances through EventEmitter, so
all Spooky instances shared the same EventEmitter instance state. Instead, use
util.inherits and call EventEmitter when constructing Spooky instances.

Thanks to @tomchentw for figuring this one out.

Close SpookyJS#51, SpookyJS#60
  • Loading branch information
Lon Ingram authored and jeresig committed Jun 27, 2013
1 parent e9ba13b commit c322bfa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/spooky.js
Expand Up @@ -54,6 +54,7 @@ function isJsonRpcResponse(s) {
}

function Spooky(options, callback) {
EventEmitter.call(this);
this.options = options = _.defaults(_.clone(options || {}), defaults);

for (var k in defaults) {
Expand Down Expand Up @@ -177,8 +178,7 @@ function Spooky(options, callback) {
}).bind(this));
}

Spooky.prototype = new EventEmitter();
Spooky.prototype.constructor = Spooky;
util.inherits(Spooky, EventEmitter);

Spooky._instances = {};
Spooky._nextInstanceId = 0;
Expand Down
15 changes: 15 additions & 0 deletions tests/test/issue-51.js
@@ -0,0 +1,15 @@
var Spooky = require('../../lib/spooky');
var expect = require('expect.js');

describe('Spooky instances', function () {
it('do not share events', function () {
var called = false;
var x = new Spooky({});
var y = new Spooky({});

x.on('test', function () { called = true; });
y.emit('test');

expect(called).not.to.be(true);
});
});

0 comments on commit c322bfa

Please sign in to comment.