mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-18 12:31:12 +02:00
itemservice: make fetching items a lot faster 🚀
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getAllItems() {
|
async getAllItems() {
|
||||||
|
var t=Date.now()
|
||||||
var d = deferred();
|
var d = deferred();
|
||||||
let itemIds = await this.getUserItemIds();
|
let itemIds = await this.getUserItemIds();
|
||||||
itemIds = Object.getOwnPropertyNames(itemIds || {});
|
itemIds = Object.getOwnPropertyNames(itemIds || {});
|
||||||
@@ -36,19 +37,21 @@
|
|||||||
if (!itemIds.length) {
|
if (!itemIds.length) {
|
||||||
d.resolve([]);
|
d.resolve([]);
|
||||||
}
|
}
|
||||||
|
var remoteDb = await window.db.getDb();
|
||||||
const items = [];
|
const items = [];
|
||||||
for (let i = 0; i < itemIds.length; i++) {
|
remoteDb
|
||||||
const id = itemIds[i];
|
.collection('items')
|
||||||
utils.log('Starting to fetch item ', id);
|
.where('createdBy', '==', window.user.uid)
|
||||||
this.getItem(id).then(item => {
|
.onSnapshot(function(querySnapshot) {
|
||||||
items.push(item);
|
querySnapshot.forEach(function(doc) {
|
||||||
// Check if we have all items now.
|
items.push(doc.data());
|
||||||
if (itemIds.length === items.length) {
|
});
|
||||||
d.resolve(items);
|
utils.log('Items fetched in ', Date.now()-t, 'ms')
|
||||||
}
|
|
||||||
|
d.resolve(items);
|
||||||
|
}, function() {
|
||||||
|
d.resolve([])
|
||||||
});
|
});
|
||||||
}
|
|
||||||
return d.promise;
|
return d.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@@ -509,7 +509,7 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn
|
|||||||
var items = [];
|
var items = [];
|
||||||
if (!window.IS_EXTENSION && window.user) {
|
if (!window.IS_EXTENSION && window.user) {
|
||||||
items = await itemService.getAllItems();
|
items = await itemService.getAllItems();
|
||||||
|
utils.log('got items')
|
||||||
if (shouldSaveGlobally) {
|
if (shouldSaveGlobally) {
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
savedItems[item.id] = item;
|
savedItems[item.id] = item;
|
||||||
|
Reference in New Issue
Block a user