From 4b9be816b369cf8416b013ee401f4bf8e66d671e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Sch=C3=BCtze?= Date: Thu, 15 Dec 2016 11:44:54 +0100 Subject: [PATCH] Speed up autoupdates Use 'bulk mode' for DS.inject by calling inject only once per each collection. --- openslides/core/static/js/core/base.js | 40 +++++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/openslides/core/static/js/core/base.js b/openslides/core/static/js/core/base.js index be693a63c..89bcf616f 100644 --- a/openslides/core/static/js/core/base.js +++ b/openslides/core/static/js/core/base.js @@ -223,22 +223,40 @@ angular.module('OpenSlidesApp.core', [ } catch(err) { console.error(json); } - _.forEach(dataList, function(data) { - console.log("Received object: " + data.collection + ", " + data.id); - var instance = DS.get(data.collection, data.id); - if (data.action == 'changed') { + + var dataListByCollection = _.groupBy(dataList, 'collection'); + _.forEach(dataListByCollection, function(list, key) { + 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) { - // The instance is in the local db dsEject(data.collection, instance); } - DS.inject(data.collection, data.data); - } else if (data.action == 'deleted') { - if (instance) { - // The instance is in the local db - dsEject(data.collection, instance); + // check if object changed or deleted + if (data.action === 'changed') { + changedElements.push(data.data); + } else if (data.action === 'deleted') { + 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); + }); }); }); }