1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-18 04:21:12 +02:00

itemservice: make fetching items a lot faster 🚀

This commit is contained in:
Kushagra Gour
2018-02-11 15:11:04 +05:30
parent 2670897d69
commit 2c7b1e50cf
2 changed files with 15 additions and 12 deletions

View File

@@ -28,6 +28,7 @@
},
async getAllItems() {
var t=Date.now()
var d = deferred();
let itemIds = await this.getUserItemIds();
itemIds = Object.getOwnPropertyNames(itemIds || {});
@@ -36,19 +37,21 @@
if (!itemIds.length) {
d.resolve([]);
}
var remoteDb = await window.db.getDb();
const items = [];
for (let i = 0; i < itemIds.length; i++) {
const id = itemIds[i];
utils.log('Starting to fetch item ', id);
this.getItem(id).then(item => {
items.push(item);
// Check if we have all items now.
if (itemIds.length === items.length) {
d.resolve(items);
}
remoteDb
.collection('items')
.where('createdBy', '==', window.user.uid)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
items.push(doc.data());
});
utils.log('Items fetched in ', Date.now()-t, 'ms')
d.resolve(items);
}, function() {
d.resolve([])
});
}
return d.promise;
},