Skip to content

Commit

Permalink
Document no longer inherits from EventEmitter #1351
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 15, 2014
1 parent 08f3891 commit def0afe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
32 changes: 30 additions & 2 deletions lib/document.js
Expand Up @@ -36,6 +36,7 @@ var EventEmitter = require('events').EventEmitter

function Document (obj, fields, skipId) {
this.$__ = new InternalCache;
this.$__.emitter = new EventEmitter();
this.isNew = true;
this.errors = undefined;

Expand All @@ -54,7 +55,7 @@ function Document (obj, fields, skipId) {
this.$__.activePaths.require(required[i]);
}

setMaxListeners.call(this, 0);
this.$__.emitter.setMaxListeners(0);
this._doc = this.$__buildDoc(obj, fields, skipId);

if (obj) {
Expand All @@ -78,7 +79,34 @@ function Document (obj, fields, skipId) {
/*!
* Inherit from EventEmitter.
*/
Document.prototype = Object.create( EventEmitter.prototype );
Document.prototype.on = function() {
return this.$__.emitter.on.apply(this.$__.emitter, arguments);
};

Document.prototype.once = function() {
return this.$__.emitter.once.apply(this.$__.emitter, arguments);
};

Document.prototype.emit = function() {
return this.$__.emitter.emit.apply(this.$__.emitter, arguments);
};

Document.prototype.listeners = function() {
return this.$__.emitter.listeners.apply(this.$__.emitter, arguments);
};

Document.prototype.removeListener = function() {
return this.$__.emitter.removeListener.apply(this.$__.emitter, arguments);
};

Document.prototype.setMaxListeners = function() {
return this.$__.emitter.setMaxListeners.apply(this.$__.emitter, arguments);
};

Document.prototype.removeAllListeners = function() {
return this.$__.emitter.removeAllListeners.apply(this.$__.emitter, arguments);
};

Document.prototype.constructor = Document;

/**
Expand Down
16 changes: 15 additions & 1 deletion lib/utils.js
Expand Up @@ -729,5 +729,19 @@ exports.mergeClone = function(to, from) {
}
}
}
}
};

/**
* Executes a function on each element of an array (like _.each)
*
* @param {Array} arr
* @param {Function} fn
* @api private
*/

exports.each = function(arr, fn) {
for (var i = 0; i < arr.length; ++i) {
fn(arr[i]);
}
};

4 changes: 2 additions & 2 deletions test/types.documentarray.test.js
Expand Up @@ -432,12 +432,12 @@ describe('types.documentarray', function(){
M.create({ docs: [{v: 900}] }, function(error, m) {
m.shouldPrint = true;
assert.ifError(error);
var numListeners = m._events.save.length;
var numListeners = m.listeners('save').length;
assert.ok(numListeners > 0);
m.docs = [{ v: 9000 }];
m.save(function(error, m) {
assert.ifError(error);
assert.equal(numListeners, m._events.save.length);
assert.equal(numListeners, m.listeners('save').length);
done();
});
});
Expand Down

0 comments on commit def0afe

Please sign in to comment.