mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
* remove spark dependency on minimongo * simpler to use orderedDict with obj k
Removed spark dependency on minimongo by duplicating a very small amount of code. There is an XXX to mark it. OrderedDict can have a function as the first argument to its constructor; it will use the function to transform whatever object is used as a key to strings.
This commit is contained in:
@@ -828,17 +828,16 @@ LocalCollection._observeUnorderedFromObserveChanges =
|
||||
|
||||
LocalCollection._observeOrderedFromObserveChanges =
|
||||
function (cursor, callbacks) {
|
||||
var docs = new OrderedDict();
|
||||
var docs = new OrderedDict(LocalCollection._idStringify);
|
||||
var suppressed = !!callbacks._suppress_initial;
|
||||
var handle = cursor.observeChanges({
|
||||
addedBefore: function (id, fields, before) {
|
||||
var strId = LocalCollection._idStringify(id);
|
||||
var doc = EJSON.clone(fields);
|
||||
doc._id = id;
|
||||
docs.putBefore(strId, doc, before ? LocalCollection._idStringify(before) : null);
|
||||
docs.putBefore(id, doc, before ? before : null);
|
||||
if (!suppressed) {
|
||||
if (callbacks.addedAt) {
|
||||
var index = docs.indexOf(strId);
|
||||
var index = docs.indexOf(id);
|
||||
callbacks.addedAt(EJSON.clone(doc), index);
|
||||
} else if (callbacks.added) {
|
||||
callbacks.added(EJSON.clone(doc));
|
||||
@@ -846,28 +845,26 @@ LocalCollection._observeOrderedFromObserveChanges =
|
||||
}
|
||||
},
|
||||
changed: function (id, fields) {
|
||||
var strId = LocalCollection._idStringify(id);
|
||||
var doc = docs.get(strId);
|
||||
var doc = docs.get(id);
|
||||
var oldDoc = EJSON.clone(doc);
|
||||
// writes through to the doc set
|
||||
LocalCollection._applyChanges(doc, fields);
|
||||
if (callbacks.changedAt) {
|
||||
var index = docs.indexOf(strId);
|
||||
var index = docs.indexOf(id);
|
||||
callbacks.changedAt(EJSON.clone(doc), oldDoc, index);
|
||||
} else if (callbacks.changed) {
|
||||
callbacks.changed(EJSON.clone(doc), oldDoc);
|
||||
}
|
||||
},
|
||||
movedBefore: function (id, before) {
|
||||
var strId = LocalCollection._idStringify(id);
|
||||
var doc = docs.get(strId);
|
||||
var doc = docs.get(id);
|
||||
var from;
|
||||
// only capture indexes if we're going to call the callback that needs them.
|
||||
if (callbacks.movedTo)
|
||||
from = docs.indexOf(strId);
|
||||
docs.moveBefore(strId, before ? LocalCollection._idStringify(before) : null);
|
||||
from = docs.indexOf(id);
|
||||
docs.moveBefore(id, before ? before : null);
|
||||
if (callbacks.movedTo) {
|
||||
var to = docs.indexOf(strId);
|
||||
var to = docs.indexOf(id);
|
||||
callbacks.movedTo(EJSON.clone(doc), from, to);
|
||||
} else if (callbacks.moved) {
|
||||
callbacks.moved(EJSON.clone(doc));
|
||||
@@ -875,12 +872,11 @@ LocalCollection._observeOrderedFromObserveChanges =
|
||||
|
||||
},
|
||||
removed: function (id) {
|
||||
var strId = LocalCollection._idStringify(id);
|
||||
var doc = docs.get(strId);
|
||||
var doc = docs.get(id);
|
||||
var index;
|
||||
if (callbacks.removedAt)
|
||||
index = docs.indexOf(strId);
|
||||
docs.remove(strId);
|
||||
index = docs.indexOf(id);
|
||||
docs.remove(id);
|
||||
callbacks.removedAt && callbacks.removedAt(doc, index);
|
||||
callbacks.removed && callbacks.removed(doc);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
// The implementation is a dictionary that contains nodes of a doubly-linked
|
||||
// list as its values.
|
||||
var k = function (key) { return " " + key; };
|
||||
|
||||
// constructs a new element struct
|
||||
// next and prev are whole elements, not keys.
|
||||
@@ -23,7 +22,11 @@
|
||||
self._dict = {};
|
||||
self._first = null;
|
||||
self._last = null;
|
||||
_.each(arguments, function (kv) {
|
||||
var args = _.toArray(arguments);
|
||||
self._stringify = function (x) { return x; };
|
||||
if (typeof args[0] === 'function')
|
||||
self._stringify = args.shift();
|
||||
_.each(args, function (kv) {
|
||||
self.putBefore(kv[0], kv[1], null);
|
||||
});
|
||||
};
|
||||
@@ -35,6 +38,8 @@
|
||||
return !self._first;
|
||||
},
|
||||
|
||||
_k: function (key) { return " " + this._stringify(key); },
|
||||
|
||||
_linkEltIn: function (elt) {
|
||||
var self = this;
|
||||
if (!elt.next) {
|
||||
@@ -65,19 +70,19 @@
|
||||
putBefore: function (key, item, before) {
|
||||
var self = this;
|
||||
var elt = before ?
|
||||
element(key, item, self._dict[k(before)]) :
|
||||
element(key, item, self._dict[self._k(before)]) :
|
||||
element(key, item, null);
|
||||
if (elt.next === undefined)
|
||||
throw new Error("could not find item to put this one before");
|
||||
self._linkEltIn(elt);
|
||||
self._dict[k(key)] = elt;
|
||||
self._dict[self._k(key)] = elt;
|
||||
},
|
||||
remove: function (key) {
|
||||
var self = this;
|
||||
var elt = self._dict[k(key)];
|
||||
var elt = self._dict[self._k(key)];
|
||||
if (elt !== undefined) {
|
||||
self._linkEltOut(elt);
|
||||
delete self._dict[k(key)];
|
||||
delete self._dict[self._k(key)];
|
||||
return elt.value;
|
||||
} else {
|
||||
return undefined;
|
||||
@@ -86,12 +91,12 @@
|
||||
get: function (key) {
|
||||
var self = this;
|
||||
if (self.has(key))
|
||||
return self._dict[k(key)].value;
|
||||
return self._dict[self._k(key)].value;
|
||||
return undefined;
|
||||
},
|
||||
has: function (key) {
|
||||
var self = this;
|
||||
return _.has(self._dict, k(key));
|
||||
return _.has(self._dict, self._k(key));
|
||||
},
|
||||
// Iterate through the items in this dictionary in order, calling
|
||||
// iter(value, key, index) on each one.
|
||||
@@ -153,8 +158,8 @@
|
||||
},
|
||||
moveBefore: function (key, before) {
|
||||
var self = this;
|
||||
var elt = self._dict[k(key)];
|
||||
var eltBefore = before ? self._dict[k(before)] : null;
|
||||
var elt = self._dict[self._k(key)];
|
||||
var eltBefore = before ? self._dict[self._k(before)] : null;
|
||||
if (elt === undefined)
|
||||
throw new Error("Item to move is not present");
|
||||
if (eltBefore === undefined) {
|
||||
@@ -173,7 +178,7 @@
|
||||
var self = this;
|
||||
var ret = null;
|
||||
self.forEach(function (v, k, i) {
|
||||
if (k === key) {
|
||||
if (self._k(k) === self._k(key)) {
|
||||
ret = i;
|
||||
return OrderedDict.BREAK;
|
||||
}
|
||||
|
||||
@@ -878,6 +878,17 @@ Spark.isolate = function (htmlFunc) {
|
||||
/* Lists */
|
||||
/******************************************************************************/
|
||||
|
||||
// XXX duplicated code from minimongo.js. It's small though.
|
||||
var applyChanges = function (doc, changeFields) {
|
||||
_.each(changeFields, function (value, key) {
|
||||
if (value === undefined)
|
||||
delete doc[key];
|
||||
else
|
||||
doc[key] = value;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var idStringify;
|
||||
var idParse;
|
||||
|
||||
@@ -888,10 +899,8 @@ if (typeof LocalCollection !== 'undefined') {
|
||||
else
|
||||
return LocalCollection._idStringify(id);
|
||||
};
|
||||
idParse = LocalCollection._idParse;
|
||||
} else {
|
||||
idStringify = function (id) { return id; };
|
||||
idParse = function (id) { return id; };
|
||||
}
|
||||
|
||||
Spark.list = function (cursor, itemFunc, elseFunc) {
|
||||
@@ -908,19 +917,14 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
|
||||
});
|
||||
|
||||
// Get the current contents of the cursor.
|
||||
// XXX currently we count on observe() using only added() to deliver
|
||||
// the initial contents. are we allow to do that, or do we need to
|
||||
// implement removed/moved/changed here as well?
|
||||
|
||||
var itemDict = new OrderedDict();
|
||||
var itemDict = new OrderedDict(idStringify);
|
||||
_.extend(callbacks, {
|
||||
addedBefore: function (id, item, before) {
|
||||
var doc = EJSON.clone(item);
|
||||
doc._id = id;
|
||||
var elt = {doc: doc, liveRange: null};
|
||||
itemDict.putBefore(idStringify(id),
|
||||
elt,
|
||||
idStringify(before));
|
||||
itemDict.putBefore(id, elt, before);
|
||||
}
|
||||
});
|
||||
var handle = cursor.observeChanges(observerCallbacks);
|
||||
@@ -992,8 +996,6 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
|
||||
_.extend(callbacks, {
|
||||
addedBefore: function (id, fields, before) {
|
||||
later(function () {
|
||||
var idStr = idStringify(id);
|
||||
var befStr = idStringify(before);
|
||||
var doc = EJSON.clone(fields);
|
||||
doc._id = id;
|
||||
var frag = Spark.render(_.bind(itemFunc, null, doc));
|
||||
@@ -1005,23 +1007,22 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
|
||||
} else if (before === null) {
|
||||
itemDict.lastValue().liveRange.insertAfter(frag);
|
||||
} else {
|
||||
itemDict.get(befStr).liveRange.insertBefore(frag);
|
||||
itemDict.get(before).liveRange.insertBefore(frag);
|
||||
}
|
||||
itemDict.putBefore(idStr, {doc: doc, liveRange: range}, befStr);
|
||||
itemDict.putBefore(id, {doc: doc, liveRange: range}, before);
|
||||
});
|
||||
},
|
||||
|
||||
removed: function (id) {
|
||||
later(function () {
|
||||
var idStr = idStringify(id);
|
||||
if (itemDict.first() === itemDict.last()) {
|
||||
var frag = Spark.render(elseFunc);
|
||||
DomUtils.wrapFragmentForContainer(frag, outerRange.containerNode());
|
||||
Spark.finalize(outerRange.replaceContents(frag));
|
||||
} else
|
||||
Spark.finalize(itemDict.get(idStr).liveRange.extract());
|
||||
Spark.finalize(itemDict.get(id).liveRange.extract());
|
||||
|
||||
itemDict.remove(idStr);
|
||||
itemDict.remove(id);
|
||||
|
||||
notifyParentsRendered();
|
||||
});
|
||||
@@ -1029,25 +1030,22 @@ Spark.list = function (cursor, itemFunc, elseFunc) {
|
||||
|
||||
movedBefore: function (id, before) {
|
||||
later(function () {
|
||||
var idStr = idStringify(id);
|
||||
var befStr = idStringify(before);
|
||||
var frag = itemDict.get(idStr).liveRange.extract();
|
||||
var frag = itemDict.get(id).liveRange.extract();
|
||||
if (before === null) {
|
||||
itemDict.lastValue().liveRange.insertAfter(frag);
|
||||
}
|
||||
else {
|
||||
itemDict.get(befStr).liveRange.insertBefore(frag);
|
||||
itemDict.get(before).liveRange.insertBefore(frag);
|
||||
}
|
||||
itemDict.moveBefore(idStr, befStr);
|
||||
itemDict.moveBefore(id, before);
|
||||
notifyParentsRendered();
|
||||
});
|
||||
},
|
||||
|
||||
changed: function (id, fields) {
|
||||
later(function () {
|
||||
var idStr = idStringify(id);
|
||||
var elt = itemDict.get(idStr);
|
||||
LocalCollection._applyChanges(elt.doc, fields);
|
||||
var elt = itemDict.get(id);
|
||||
applyChanges(elt.doc, fields);
|
||||
Spark.renderToRange(elt.liveRange, _.bind(itemFunc, null, elt.doc));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user