Skip to content

Commit

Permalink
Merge pull request #10 from hexsprite/master
Browse files Browse the repository at this point in the history
fixes for compat with peerlibrary:reactive-publish, fixes #3
  • Loading branch information
johanbrook authored Oct 22, 2016
2 parents fccfd28 + 228ecb6 commit daf517d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions publication-collector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Mongo } from 'meteor/mongo';
import { MongoID } from 'meteor/mongo-id';
import { EventEmitter } from 'events';

const validMongoId = Match.OneOf(String, Mongo.ObjectID);
Expand All @@ -15,10 +16,14 @@ PublicationCollector = class PublicationCollector extends EventEmitter {
check(context.userId, Match.Optional(String));

// Object where the keys are collection names, and then the keys are _ids
this.responseData = {};
this._documents = {};
this.unblock = () => {};
this.userId = context.userId;
this.observeHandles = [];
this._idFilter = {
idStringify: MongoID.idStringify,
idParse: MongoID.idParse
};
}

collect(name, ...args) {
Expand Down Expand Up @@ -56,7 +61,7 @@ PublicationCollector = class PublicationCollector extends EventEmitter {

// Make sure to ignore the _id in fields
const addedDocument = _.extend({_id: id}, _.omit(fields, '_id'));
this.responseData[collection][id] = addedDocument;
this._documents[collection][id] = addedDocument;
}

changed(collection, id, fields) {
Expand All @@ -65,7 +70,7 @@ PublicationCollector = class PublicationCollector extends EventEmitter {

this._ensureCollectionInRes(collection);

const existingDocument = this.responseData[collection][id];
const existingDocument = this._documents[collection][id];
const fieldsNoId = _.omit(fields, '_id');
_.extend(existingDocument, fieldsNoId);

Expand All @@ -83,10 +88,10 @@ PublicationCollector = class PublicationCollector extends EventEmitter {

this._ensureCollectionInRes(collection);

delete this.responseData[collection][id];
delete this._documents[collection][id];

if (_.isEmpty(this.responseData[collection])) {
delete this.responseData[collection];
if (_.isEmpty(this._documents[collection])) {
delete this._documents[collection];
}
}

Expand All @@ -107,13 +112,13 @@ PublicationCollector = class PublicationCollector extends EventEmitter {
}

_ensureCollectionInRes(collection) {
this.responseData[collection] = this.responseData[collection] || {};
this._documents[collection] = this._documents[collection] || {};
}

_generateResponse() {
const output = {};

_.forEach(this.responseData, (documents, collectionName) => {
_.forEach(this._documents, (documents, collectionName) => {
output[collectionName] = _.values(documents);
});

Expand Down
6 changes: 3 additions & 3 deletions tests/publication-collector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('PublicationCollector', () => {
const doc = {_id: id, foo: 'bar'};
collector.added('documents', doc._id, doc);

assert.deepEqual(collector.responseData.documents[id], doc);
assert.deepEqual(collector._documents.documents[id], doc);
});
});

Expand All @@ -133,8 +133,8 @@ describe('PublicationCollector', () => {
collector.collect('publication');
collector.removed('documents', doc._id);

assert.notOk(collector.responseData.documents[doc._id]);
assert.equal(Object.keys(collector.responseData.documents).length, 9);
assert.notOk(collector._documents.documents[doc._id]);
assert.equal(Object.keys(collector._documents.documents).length, 9);
});
});

Expand Down

0 comments on commit daf517d

Please sign in to comment.