From 232f34e131a900935e3a91569ee01d3b572b671c Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Wed, 8 Jun 2016 20:05:09 -0300 Subject: [PATCH] Try to improve perfomance of data loading --- .../client/lib/cachedCollection.js | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index 064775bf55a..0b14733f83a 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -22,6 +22,12 @@ class CachedCollectionManager { } } + countQueries() { + for (const item of this.items) { + item.countQueries(); + } + } + set syncEnabled(value) { check(value, Boolean); this._syncEnabled = value; @@ -73,6 +79,17 @@ class CachedCollection { } } + countQueries() { + this.log(`${Object.keys(this.collection._collection.queries).length} queries`); + } + + recomputeCollectionQueries() { + this.log(`recomputing ${Object.keys(this.collection._collection.queries).length} queries`); + _.each(this.collection._collection.queries, (query) => { + this.collection._collection._recomputeResults(query); + }); + } + loadFromCache(callback = () => {}) { if (this.useCache === false) { return callback(false); @@ -96,7 +113,7 @@ class CachedCollection { this.log(`${data.records.length} records loaded from cache`); data.records.forEach((record) => { record.__cache__ = true; - this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); + this.collection._collection._docs.set(record._id, record); if (record._updatedAt) { const _updatedAt = new Date(record._updatedAt); @@ -105,6 +122,7 @@ class CachedCollection { } } }); + this.recomputeCollectionQueries(); callback(true); } else { @@ -117,12 +135,13 @@ class CachedCollection { Meteor.call(this.methodName, (error, data) => { this.log(`${data.length} records loaded from server`); data.forEach((record) => { - this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); + this.collection._collection._docs.set(record._id, record); if (record._updatedAt && record._updatedAt > this.updatedAt) { this.updatedAt = record._updatedAt; } }); + this.recomputeCollectionQueries(); if (this.updatedAt < new Date) { this.updatedAt = new Date;