Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call observer handlers even if maintainCollections is false #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 25 additions & 17 deletions lib/ddp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ DDPClient.prototype._message = function(data) {

// add document to collection
} else if (data.msg === "added") {
if (self.maintainCollections && data.collection) {
var name = data.collection, id = data.id;
var name = data.collection, id = data.id;
if (self.maintainCollections && name) {

if (! self.collections[name]) { self.collections[name] = {}; }
if (! self.collections[name][id]) { self.collections[name][id] = {}; }
Expand All @@ -192,32 +192,33 @@ DDPClient.prototype._message = function(data) {
self.collections[name][id][key] = value;
});
}

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

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

// remove document from collection
} else if (data.msg === "removed") {
var name = data.collection, id = data.id;
var oldValue = undefined;

if (self.maintainCollections && data.collection) {
var name = data.collection, id = data.id;

if (! self.collections[name][id]) {
return;
}

var oldValue = self.collections[name][id];
oldValue = self.collections[name][id];

delete self.collections[name][id];

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

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

// change document in collection
Expand Down Expand Up @@ -251,6 +252,13 @@ DDPClient.prototype._message = function(data) {
observer.changed(id, oldFields, clearedFields, newFields);
});
}
} else if (data.collection) {
var name = data.collection, id = data.id;
if (self._observers[name]) {
_.each(self._observers[name], function(observer) {
observer.changed(id, data.fields, data.cleared || []);
});
}
}

// subscriptions ready
Expand Down
59 changes: 59 additions & 0 deletions test/ddp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,25 @@ describe('Collection maintenance and observation', function() {
assert.equal(ddpclient.collections.posts['2trpvcQ4pn32ZYXco'].text, "A cat was here");
assert.equal(ddpclient.collections.posts['2trpvcQ4pn32ZYXco'].value, true);
});

it('should response to "added" with fields even if maintainCollections is false', function() {
var ddpclient = new DDPClient({ maintainCollections: false }), observed = false;
var addedFields = undefined;
observer = ddpclient.observe("posts");
observer.added = function(id, fields) {
if (id === '2trpvcQ4pn32ZYXco') {
observed = true;
addedFields = fields;
}
}

ddpclient._message(addedMessage);
// ensure there are no collections
assert(!ddpclient.collections);
assert(observed, "addition observed");
assert.equal(addedFields.text, "A cat was here");
assert.equal(addedFields.value, true);
});

it('should response to "changed" messages', function() {
var ddpclient = new DDPClient(), observed = false;
Expand Down Expand Up @@ -334,6 +353,28 @@ describe('Collection maintenance and observation', function() {
assert(!ddpclient.collections.posts['2trpvcQ4pn32ZYXco'].hasOwnProperty('value'));
assert(observed, "cleared change observed")
});

it('should response to "changed" even if maintainCollections is false', function() {
var ddpclient = new DDPClient({ maintainCollections: false }), observed = false;
observer = ddpclient.observe("posts");
observer.changed = function(id, fields) {
if (id === "2trpvcQ4pn32ZYXco" && fields.text === "A dog was here") {
observed = true;
}
};

ddpclient._message(addedMessage);

// ensure there are no collections
assert(!ddpclient.collections);

ddpclient._message(changedMessage);

// ensure there are no collections
assert(!ddpclient.collections);

assert(observed, "field change observed");
});

it('should response to "removed" messages', function() {
var ddpclient = new DDPClient(), oldval;
Expand All @@ -347,6 +388,24 @@ describe('Collection maintenance and observation', function() {
assert.equal(oldval.text, "A cat was here");
assert.equal(oldval.value, true);
});

it('should response to "removed" even if maintainCollections is false', function() {
var ddpclient = new DDPClient({ maintainCollections: false }), docid;
observer = ddpclient.observe("posts");
observer.removed = function(id) { docid = id; };

ddpclient._message(addedMessage);

// ensure there are no collections
assert(!ddpclient.collections);

ddpclient._message(removedMessage);

// ensure there are no collections
assert(!ddpclient.collections);

assert.equal(docid, "2trpvcQ4pn32ZYXco");
});
});


Expand Down