Can anyone explain the purpose of elt === precedenceList[0]
in the function at the bottom? I pulled the code from livedata_server.js
I’ve found interesting problems with having multiple subscriptions returning different subsets of data for a while - but until now its always been work-aroundable.
However, I’ve recently come across a problem that I can’t get around. It involves a dynamic set of fields being displayed in a table, if the user changes those fields, I resubscribe to the same publication with new options, then after the subscription has completed, I kill the old subscription - the purpose of doing it in this order is to avoid UI flicker when one subscription stopping wipes the data, and the second subscription running re-fills it. Something like this (very simplified):
let sub;
let subToStop;
Tracker.autorun(() => {
const fields = someReactiveContext(); //
subToStop = sub;
sub = Meteor.subscribe("myCollection", fields);
});
Tracker.autorun(() => {
if (sub.ready() && subToStop) {
subToStop.stop();
}
});
This ensures we only have one subscription running at a time (ignoring the obvious race condition), which always has the latest requested fields, but avoids draining and refilling the collection by stopping/starting (as a regular autorun subscription would). This works great if the fields being changed are top level fields on the document, however if you’re adding fields within the same object, e.g., profile.firstName, profile.lastName
becomes profile.firstName, profile.lastName, profile.phone
- it does NOT. The cause appears to be elt === precedenceList[0]
- this check only sends the change if the subscription handle is the first subscription and I can’t think why this would be desirable.
changeField: function (subscriptionHandle, key, value,
changeCollector, isAdd) {
var self = this;
// Publish API ignores _id if present in fields
if (key === "_id")
return;
// Don't share state with the data passed in by the user.
value = EJSON.clone(value);
if (!_.has(self.dataByKey, key)) {
self.dataByKey[key] = [{subscriptionHandle: subscriptionHandle,
value: value}];
changeCollector[key] = value;
return;
}
var precedenceList = self.dataByKey[key];
var elt;
if (!isAdd) {
elt = _.find(precedenceList, function (precedence) {
return precedence.subscriptionHandle === subscriptionHandle;
});
}
if (elt) {
if (elt === precedenceList[0] && !EJSON.equals(value, elt.value)) {
// this subscription is changing the value of this field.
changeCollector[key] = value;
}
elt.value = value;
} else {
// this subscription is newly caring about this field
precedenceList.push({subscriptionHandle: subscriptionHandle, value: value});
}
}