Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,57 @@ class DDPClient extends EventEmitter{
}

self.collections[name].upsert(item);

if (self._observers[name]) {
_.each(self._observers[name], function(observer) {
observer.added(id, item);
});
}
}

// remove document from collection
} else if (data.msg === "removed") {
if (self.maintainCollections && data.collection) {
var name = data.collection, id = data.id;
var name = data.collection,
id = data.id
oldFields = self.collections[name].get(id);

self.collections[name].remove({"_id": id});

if (self._observers[name]) {
_.each(self._observers[name], function(observer) {
observer.removed(id, oldFields);
});
}
}

// change document in collection
// change document in collection
} else if (data.msg === "changed") {
if (self.maintainCollections && data.collection) {
var name = data.collection, id = data.id;
var name = data.collection,
id = data.id,
oldFields = {},
newFields = {},
clearedFields = data.cleared || [];

var item = {
"_id": id
};

if (data.fields) {
oldFields = self.collections[name].get(id);
_.each(data.fields, function(value, key) {
item[key] = value;
})
}

self.collections[name].upsert(item);
newFields = self.collections[name].upsert(item);

if (self._observers[name]) {
_.each(self._observers[name], function(observer) {
observer.changed(id, oldFields, clearedFields, newFields);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navelpluisje pardon me if I'm mistaken, but by this point in the code, newFields is still an empty object right? If you look here in the master branch, we add every key-value pair to newFields and then pass it into observer.changed.

In this case however, I believe just passing in item in place of newFields would suffice as it is on line 191 here in your PR.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I can't leave a line note on line 466, but the changed DDP message's observer function should be renamed back to changed from updated to be consistent with the DDP message type and the code you've written here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunny-g Thanx for mentioning about the newFields. I will submit a change in a minute. Like you mentioned it is possible to set item as the parameter, but if the submit to the cache fails this is not the right value. I'm setting the value of newFields with the result of the upsert. Just forgot to copy this part;-)

});
}
}

// subscriptions ready
Expand Down Expand Up @@ -420,10 +446,10 @@ class DDPClient extends EventEmitter{
/**
* Adds an observer to a collection and returns the observer.
* Observation can be stopped by calling the stop() method on the observer.
* Functions for added, updated and removed can be added to the observer
* Functions for added, removed and changed can be added to the observer
* afterward.
*/
observe(name, added, updated, removed) {
observe(name, added, changed, removed) {
var self = this;
var observer = {};
var id = self._getNextId();
Expand All @@ -437,7 +463,7 @@ class DDPClient extends EventEmitter{
Object.defineProperty(observer, "_id", { get: function() { return id; }});

observer.added = added || function(){};
observer.updated = updated || function(){};
observer.changed = changed || function(){};
observer.removed = removed || function(){};

observer.stop = function() {
Expand Down