2021-09-07 04:51:42 +08:00
|
|
|
import SubscriptionRegistry from '/imports/ui/services/subscription-registry/subscriptionRegistry';
|
2021-10-20 04:35:39 +08:00
|
|
|
import CollectionEventsBroker from '/imports/ui/services/LiveDataEventBroker/LiveDataEventBroker';
|
2021-09-07 04:51:42 +08:00
|
|
|
|
2021-10-20 04:35:39 +08:00
|
|
|
/*
|
2022-10-28 21:33:21 +08:00
|
|
|
This class connects a local collection with the LiveDataEventBroker, propagating the changes of a server-side published cursor to a local collection.
|
2021-10-20 04:35:39 +08:00
|
|
|
|
|
|
|
It also guarantee that in case of a reconnection or a re-subscription, the data is only removed after subscription is ready, avoiding the situation of missing data during re-synchronization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LocalCollectionSynchronizer {
|
2021-09-07 04:51:42 +08:00
|
|
|
constructor(serverCollection, localCollection, options = {}) {
|
|
|
|
this.serverCollection = serverCollection;
|
|
|
|
this.localCollection = localCollection;
|
|
|
|
|
|
|
|
this.lastSubscriptionId = '';
|
|
|
|
this.options = options;
|
|
|
|
this.ignoreDeletes = false;
|
2022-10-28 21:33:21 +08:00
|
|
|
this.checkForStaleData = this.checkForStaleData.bind(this);
|
2021-09-07 04:51:42 +08:00
|
|
|
}
|
|
|
|
|
2021-10-20 04:35:39 +08:00
|
|
|
/*
|
|
|
|
This method allow to enable/disable the ignoreDeletes feature.
|
|
|
|
When enabled, system will skip the received deletes ( not apply on local collection ).
|
|
|
|
Don't panic: these deletes will be processed when the subscription gets ready - see removeOldSubscriptionData method.
|
|
|
|
*/
|
2021-09-07 04:51:42 +08:00
|
|
|
setIgnoreDeletes(value) {
|
|
|
|
this.ignoreDeletes = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicates remote collection to local collection ( avoiding cleanup during the forced resync )
|
2021-10-20 04:35:39 +08:00
|
|
|
setupListeners() {
|
2021-09-07 04:51:42 +08:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
const addedCallback = function (item) {
|
|
|
|
if (item.id === 'publication-stop-marker' && item.stopped) {
|
|
|
|
self.ignoreDeletes = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-28 21:33:21 +08:00
|
|
|
self.checkForStaleData();
|
2021-09-07 04:51:42 +08:00
|
|
|
const selector = { referenceId: item.referenceId };
|
|
|
|
const itemExistInCollection = self.localCollection.findOne(selector);
|
|
|
|
|
|
|
|
const { _id, ...restItem } = item;
|
|
|
|
const payload = {
|
|
|
|
subscriptionId: self.lastSubscriptionId,
|
|
|
|
...restItem,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (itemExistInCollection) {
|
|
|
|
self.localCollection.update(selector, payload);
|
|
|
|
} else {
|
|
|
|
self.localCollection.insert(payload);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const changedCallback = function (item) {
|
|
|
|
const {
|
|
|
|
_id,
|
|
|
|
...restFields
|
|
|
|
} = item;
|
|
|
|
const selector = { referenceId: item.referenceId };
|
|
|
|
const payload = {
|
|
|
|
$set: {
|
|
|
|
...restFields,
|
|
|
|
subscriptionId: self.lastSubscriptionId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
self.localCollection.update(selector, payload);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removedCallback = function (item) {
|
2021-10-20 04:35:39 +08:00
|
|
|
const globalIgnoreDeletes = Session.get('globalIgnoreDeletes');
|
|
|
|
if (self.ignoreDeletes || globalIgnoreDeletes) {
|
2021-09-07 04:51:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selector = { referenceId: item.referenceId };
|
|
|
|
|
|
|
|
self.localCollection.remove(selector);
|
|
|
|
};
|
|
|
|
|
|
|
|
CollectionEventsBroker.addListener(this.serverCollection._name, 'added', addedCallback);
|
|
|
|
CollectionEventsBroker.addListener(this.serverCollection._name, 'changed', changedCallback);
|
|
|
|
CollectionEventsBroker.addListener(this.serverCollection._name, 'removed', removedCallback);
|
|
|
|
}
|
|
|
|
|
2021-10-20 04:35:39 +08:00
|
|
|
/*
|
|
|
|
This method calls the function received as parameter when the subscription gets ready.
|
|
|
|
*/
|
|
|
|
callWhenSubscriptionReady(func) {
|
2021-09-07 04:51:42 +08:00
|
|
|
const temp = (res) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
const subscription = SubscriptionRegistry.getSubscription(this.serverCollection._name);
|
|
|
|
if (subscription.ready()) {
|
|
|
|
func();
|
|
|
|
return res();
|
|
|
|
}
|
|
|
|
return temp(res);
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
const tempPromise = new Promise((res) => {
|
|
|
|
temp(res);
|
|
|
|
});
|
|
|
|
return tempPromise;
|
|
|
|
}
|
|
|
|
|
2022-10-28 21:33:21 +08:00
|
|
|
checkForStaleData() {
|
|
|
|
const subscription = SubscriptionRegistry.getSubscription(this.serverCollection._name);
|
|
|
|
|
|
|
|
// If the subscriptionId changes means the subscriptions was redone
|
|
|
|
// or theres more than one subscription per collection
|
|
|
|
if (subscription && (this.lastSubscriptionId !== subscription.subscriptionId)) {
|
|
|
|
const wasEmpty = this.lastSubscriptionId === '';
|
|
|
|
this.lastSubscriptionId = subscription.subscriptionId;
|
|
|
|
if (!wasEmpty) {
|
|
|
|
this.callWhenSubscriptionReady(() => {
|
|
|
|
this.ignoreDeletes = false;
|
|
|
|
Session.set('globalIgnoreDeletes', false);
|
|
|
|
this.removeOldSubscriptionData();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 04:35:39 +08:00
|
|
|
/*
|
|
|
|
This method removes data from previous subscriptions after the current one is ready.
|
|
|
|
*/
|
2021-09-07 04:51:42 +08:00
|
|
|
removeOldSubscriptionData() {
|
|
|
|
const subscription = SubscriptionRegistry.getSubscription(this.serverCollection._name);
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
subscriptionId: {
|
|
|
|
$nin: [subscription.subscriptionId],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const oldSubscriptionItems = this.localCollection.find(selector).fetch();
|
|
|
|
oldSubscriptionItems.forEach((item) => {
|
|
|
|
this.localCollection.remove({
|
|
|
|
meetingId: item.meetingId,
|
|
|
|
...selector,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 04:35:39 +08:00
|
|
|
export default LocalCollectionSynchronizer;
|