Permalink
Browse files

Sync Minimongo observe API to server.

Return old document in callbacks.  Add LiveResultsSet.collection to
match server.

Add observe() unit test suite.
  • Loading branch information...
1 parent 6f676a9 commit bc05e418f2bb127e7731ba4968a02963c038328b @debergalis debergalis committed Feb 24, 2012
Showing with 69 additions and 9 deletions.
  1. +8 −7 packages/minimongo/minimongo.js
  2. +61 −2 packages/minimongo/minimongo_tests.js
@@ -144,10 +144,9 @@ LocalCollection.LiveResultsSet = function () {};
// options to contain:
// * callbacks:
// - added (object, before_index)
-// - changed (new_object, at_index)
+// - changed (new_object, at_index, old_object)
// - moved (object, old_index, new_index) - can only fire with changed()
-// - removed (id, at_index)
-// * sort: sort descriptor
+// - removed (id, at_index, object)
//
// attributes available on returned query handle:
// * stop(): end updates
@@ -335,6 +334,8 @@ LocalCollection.prototype._modifyAndNotify = function (doc, mod) {
for (var qid in self.queries)
matched_before[qid] = self.queries[qid].selector_f(doc);
+ var old_doc = LocalCollection._deepcopy(doc);
+
LocalCollection._modify(doc, mod);
for (var qid in self.queries) {
@@ -346,7 +347,7 @@ LocalCollection.prototype._modifyAndNotify = function (doc, mod) {
else if (!before && after)
LocalCollection._insertInResults(query, doc);
else if (before && after)
- LocalCollection._updateInResults(query, doc);
+ LocalCollection._updateInResults(query, doc, old_doc);
}
};
@@ -384,13 +385,13 @@ LocalCollection._insertInResults = function (query, doc) {
LocalCollection._removeFromResults = function (query, doc) {
var i = LocalCollection._findInResults(query, doc);
- query.removed(doc._id, i);
+ query.removed(doc._id, i, doc);
query.results.splice(i, 1);
};
-LocalCollection._updateInResults = function (query, doc) {
+LocalCollection._updateInResults = function (query, doc, old_doc) {
var orig_idx = LocalCollection._findInResults(query, doc);
- query.changed(LocalCollection._deepcopy(doc), orig_idx);
+ query.changed(LocalCollection._deepcopy(doc), orig_idx, old_doc);
if (!query.sort_f)
return;
@@ -825,6 +825,65 @@ test("minimongo - modify", function () {
// XXX test update() (selecting docs, multi, upsert..)
test("minimongo - observe", function () {
- // XXX needs tests!
- // don't forget tests for stop
+ var operations = [];
+ var cbs = {
+ added: function (obj, idx) {
+ delete obj._id;
+ operations.push(LocalCollection._deepcopy(['added', obj, idx]));
+ },
+ changed: function (obj, at, old_obj) {
+ delete obj._id;
+ delete old_obj._id;
+ operations.push(LocalCollection._deepcopy(['changed', obj, at, old_obj]));
+ },
+ moved: function (obj, old_at, new_at) {
+ delete obj._id;
+ operations.push(LocalCollection._deepcopy(['moved', obj, old_at, new_at]));
+ },
+ removed: function (id, at, old_obj) {
+ delete old_obj._id;
+ operations.push(LocalCollection._deepcopy(['removed', id, at, old_obj]));
+ }
+ };
+ var handle;
+
+ var c = new LocalCollection();
+ handle = c.find({}, {sort: {a: 1}}).observe(cbs);
+ assert.isTrue(handle.collection === c);
+
+ c.insert({a:1});
+ assert.equal(operations.shift(), ['added', {a:1}, 0]);
+ c.update({a:1}, {$set: {a: 2}});
+ assert.equal(operations.shift(), ['changed', {a:2}, 0, {a:1}]);
+ c.insert({a:10});
+ assert.equal(operations.shift(), ['added', {a:10}, 1]);
+ c.update({}, {$inc: {a: 1}}, {multi: true});
+ assert.equal(operations.shift(), ['changed', {a:3}, 0, {a:2}]);
+ assert.equal(operations.shift(), ['changed', {a:11}, 1, {a:10}]);
+ c.update({a:11}, {a:1});
+ assert.equal(operations.shift(), ['changed', {a:1}, 1, {a:11}]);
+ assert.equal(operations.shift(), ['moved', {a:1}, 1, 0]);
+ c.remove({a:2});
+ assert.equal(operations.shift(), undefined);
+ var id = c.findOne({a:3})._id;
+ c.remove({a:3});
+ assert.equal(operations.shift(), ['removed', id, 1, {a:3}]);
+
+ // test stop
+ handle.stop();
+ c.insert({a:2});
+ assert.equal(operations.shift(), undefined);
+
+ // test initial inserts (and backwards sort)
+ handle = c.find({}, {sort: {a: -1}}).observe(cbs);
+ assert.equal(operations.shift(), ['added', {a:2}, 0]);
+ assert.equal(operations.shift(), ['added', {a:1}, 1]);
+ handle.stop();
+
+ // test _suppress_initial
+ handle = c.find({}, {sort: {a: -1}}).observe(_.extend(cbs, {_suppress_initial: true}));
+ assert.equal(operations.shift(), undefined);
+ c.insert({a:100});
+ assert.equal(operations.shift(), ['added', {a:100}, 0]);
+ handle.stop();
});

0 comments on commit bc05e41

Please sign in to comment.