Merge pull request #2778 from emanuelschuetze/inject-improvement

Speed up autoupdates
This commit is contained in:
Emanuel Schütze 2016-12-16 19:12:45 +01:00 committed by GitHub
commit cd89f72a17

View File

@ -223,22 +223,40 @@ angular.module('OpenSlidesApp.core', [
} catch(err) { } catch(err) {
console.error(json); console.error(json);
} }
_.forEach(dataList, function(data) {
console.log("Received object: " + data.collection + ", " + data.id); var dataListByCollection = _.groupBy(dataList, 'collection');
var instance = DS.get(data.collection, data.id); _.forEach(dataListByCollection, function(list, key) {
if (data.action == 'changed') { var changedElements = [];
var deletedElements = [];
var collectionString = key;
_.forEach(list, function(data) {
// uncomment this line for debugging to log all autoupdates:
// console.log("Received object: " + data.collection + ", " + data.id);
// remove (=eject) object from local DS store
var instance = DS.get(data.collection, data.id);
if (instance) { if (instance) {
// The instance is in the local db
dsEject(data.collection, instance); dsEject(data.collection, instance);
} }
DS.inject(data.collection, data.data); // check if object changed or deleted
} else if (data.action == 'deleted') { if (data.action === 'changed') {
if (instance) { changedElements.push(data.data);
// The instance is in the local db } else if (data.action === 'deleted') {
dsEject(data.collection, instance); deletedElements.push(data.id);
} else {
console.error('Error: Undefined action for received object' +
'(' + data.collection + ', ' + data.id + ')');
} }
DS.eject(data.collection, data.id); });
// add (=inject) all given objects into local DS store
if (changedElements.length > 0) {
DS.inject(collectionString, changedElements);
} }
// delete (=eject) all given objects from local DS store
// (note: js-data does not provide 'bulk eject' as for DS.inject)
_.forEach(deletedElements, function(id) {
DS.eject(collectionString, id);
});
}); });
}); });
} }