Skip to content

Commit

Permalink
Merge pull request #2540 from LearnBoost/gh-1351
Browse files Browse the repository at this point in the history
Document no longer inherits from EventEmitter, instead exposes API wrapper around emitter
  • Loading branch information
vkarpov15 committed Dec 15, 2014
2 parents 08f3891 + 3afccde commit a05f84e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/browserDocument.js
Expand Up @@ -63,6 +63,7 @@ function Document (obj, schema, fields, skipId, skipInit) {
this.$__setSchema(schema);

this.$__ = new InternalCache;
this.$__.emitter = new EventEmitter();
this.isNew = true;
this.errors = undefined;

Expand Down Expand Up @@ -101,9 +102,9 @@ function Document (obj, schema, fields, skipId, skipInit) {
}

/*!
* Inherit from EventEmitter.
* Inherit from the NodeJS document
*/
Document.prototype = Object.create( NodeJSDocument.prototype );
Document.prototype = Object.create(NodeJSDocument.prototype);
Document.prototype.constructor = Document;


Expand Down
16 changes: 13 additions & 3 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 @@ -76,9 +77,18 @@ function Document (obj, fields, skipId) {
}

/*!
* Inherit from EventEmitter.
* Document exposes the NodeJS event emitter API, so you can use
* `on`, `once`, etc.
*/
Document.prototype = Object.create( EventEmitter.prototype );
utils.each(
['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
'removeAllListeners'],
function(emitterFn) {
Document.prototype[emitterFn] = function() {
return this.$__.emitter[emitterFn].apply(this.$__.emitter, arguments);
};
});

Document.prototype.constructor = Document;

/**
Expand Down
1 change: 0 additions & 1 deletion lib/model.js
Expand Up @@ -2678,7 +2678,6 @@ Model.compile = function compile (name, schema, collectionName, connection, base
}

model.schema = model.prototype.schema;
model.options = model.prototype.options;
model.collection = model.prototype.collection;

return model;
Expand Down
4 changes: 1 addition & 3 deletions lib/schema.js
Expand Up @@ -284,20 +284,18 @@ Schema.prototype.add = function add (obj, prefix) {
Schema.reserved = Object.create(null);
var reserved = Schema.reserved;
reserved.on =
reserved.once =
reserved.db =
reserved.set =
reserved.get =
reserved.init =
reserved.isNew =
reserved.errors =
reserved.schema =
reserved.options =
reserved.modelName =
reserved.collection =
reserved.toObject =
reserved.domain =
reserved.emit = // EventEmitter
reserved._events = // EventEmitter
reserved._pres = reserved._posts = 1 // hooks.js

/**
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]);
}
};

7 changes: 5 additions & 2 deletions test/query.middleware.test.js
Expand Up @@ -13,7 +13,8 @@ describe('query middleware', function() {
schema = new Schema({
title: String,
author: String,
publisher: { type: Schema.ObjectId, ref: 'gh-2138-1' }
publisher: { type: Schema.ObjectId, ref: 'gh-2138-1' },
options: String
});

var publisherSchema = new Schema({
Expand Down Expand Up @@ -42,7 +43,8 @@ describe('query middleware', function() {
var doc = {
title: 'Professional AngularJS',
author: 'Val',
publisher: publisher._id
publisher: publisher._id,
options: 'bacon'
};

Author.create(doc, function(error) {
Expand Down Expand Up @@ -78,6 +80,7 @@ describe('query middleware', function() {
schema.post('find', function(results, next) {
assert.equal(1, results.length);
assert.equal('Val', results[0].author);
assert.equal('bacon', results[0].options);
++postCount;
next();
});
Expand Down
6 changes: 0 additions & 6 deletions test/schema.test.js
Expand Up @@ -1312,12 +1312,6 @@ describe('schema', function(){
});
}, /`on` may not be used as a schema pathname/);

assert.throws(function(){
new Schema({
options: String
});
}, /`options` may not be used as a schema pathname/);

assert.throws(function(){
new Schema({
collection: String
Expand Down
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 a05f84e

Please sign in to comment.