Unordered observe based on observeChanges now.

This commit is contained in:
Naomi Seyfer
2013-01-30 12:07:04 -08:00
parent 62e83ec2bc
commit f8bfb06001
4 changed files with 29 additions and 4 deletions

View File

@@ -203,7 +203,7 @@ _.extend(LocalCollection.Cursor.prototype, {
},
_observeUnordered: function (options) {
var self = this;
return self._observeInternal(false, options);
return LocalCollection._observeUnordered(self, options);
},
observeChanges: function (callbacks) {
var self = this;
@@ -872,4 +872,30 @@ LocalCollection._makeChangedFields = function (newDoc, oldDoc) {
return fields;
};
LocalCollection._observeUnordered = function (cursor, callbacks) {
var docs = {};
return cursor.observeChanges({
added: function (id, fields) {
var strId = LocalCollection._idStringify(id);
var doc = EJSON.clone(fields);
doc._id = id;
docs[strId] = doc;
callbacks.added(doc);
},
changed: function (id, fields) {
var strId = LocalCollection._idStringify(id);
var doc = docs[strId];
var oldDoc = EJSON.clone(docs[strId]);
// writes through to the doc set
LocalCollection._applyChanges(doc, fields);
callbacks.changed(doc, oldDoc);
},
removed: function (id) {
var strId = LocalCollection._idStringify(id);
var doc = docs[strId];
delete docs[strId];
callbacks.removed(doc);
}
});
};
})();

View File

@@ -9,7 +9,6 @@ Package.on_use(function (api, where) {
// It would be sort of nice if minimongo didn't depend on
// underscore, so we could ship it separately.
api.use(['underscore', 'json', 'ejson', 'ordereddict'], where);
api.add_files([
'minimongo.js',
'selector.js',

View File

@@ -354,8 +354,7 @@ Cursor.prototype.observe = function (callbacks) {
Cursor.prototype._observeUnordered = function (callbacks) {
var self = this;
return self._mongo._observe(
self._cursorDescription, false, callbacks);
return LocalCollection._observeUnordered(self, callbacks);
};
Cursor.prototype.observeChanges = function (callbacks) {

View File

@@ -36,6 +36,7 @@
}
if (self._first === null || self._first === elt.next)
self._first = elt;
self._dict[key] = item;
},
remove: function (key) {
var self = this;