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

itemservice: make fetch methods support for remote and local fetching

This commit is contained in:
Kushagra Gour
2019-03-08 14:34:49 +05:30
parent 8b15c479f4
commit cd486fc3ba
2 changed files with 52 additions and 68 deletions

View File

@@ -473,47 +473,20 @@ export default class App extends Component {
* @return {promise} Promise. * @return {promise} Promise.
*/ */
async fetchItems(shouldSaveGlobally, shouldFetchLocally) { async fetchItems(shouldSaveGlobally, shouldFetchLocally) {
var d = deferred();
// HACK: This empty assignment is being used when importing locally saved items // HACK: This empty assignment is being used when importing locally saved items
// to cloud, `fetchItems` runs once on account login which clears the // to cloud, `fetchItems` runs once on account login which clears the
// savedItems object and hence, while merging no saved item matches with itself. // savedItems object and hence, while merging no saved item matches with itself.
this.state.savedItems = {}; this.state.savedItems = {};
var items = []; var items = [];
if (window.user && !shouldFetchLocally) {
items = await itemService.getAllItems(); items = await itemService.getAllItems(shouldFetchLocally);
log('got items'); trackEvent('fn', 'fetchItems', items.length);
if (shouldSaveGlobally) { if (shouldSaveGlobally) {
items.forEach(item => { items.forEach(item => {
this.state.savedItems[item.id] = item; this.state.savedItems[item.id] = item;
}); });
}
d.resolve(items);
return d.promise;
} }
db.local.get('items', result => { return items;
var itemIds = Object.getOwnPropertyNames(result.items || {});
if (!itemIds.length) {
d.resolve([]);
}
trackEvent('fn', 'fetchItems', itemIds.length);
for (let i = 0; i < itemIds.length; i++) {
/* eslint-disable no-loop-func */
db.local.get(itemIds[i], itemResult => {
if (shouldSaveGlobally) {
this.state.savedItems[itemIds[i]] = itemResult[itemIds[i]];
}
items.push(itemResult[itemIds[i]]);
// Check if we have all items now.
if (itemIds.length === items.length) {
d.resolve(items);
}
});
/* eslint-enable no-loop-func */
}
});
return d.promise;
} }
openSavedItemsPane() { openSavedItemsPane() {

View File

@@ -13,59 +13,70 @@ export const itemService = {
}, },
/** /**
* Fetches user's item id list. CURRENTLY RUNS ONLY * Fetches all item ids of current user
* FOR LOGGED IN USER!! * @param {boolean} shouldFetchLocally Should forcefully fetch locally?
*/ */
async getUserItemIds() { async getUserItemIds(shouldFetchLocally) {
if (window.user) { if (window.user && !shouldFetchLocally) {
return new Promise(resolve => { return new Promise(resolve => {
resolve(window.user.items || {}); resolve(window.user.items || {});
}); });
} }
var remoteDb = await window.db.getDb(); return new Promise(resolve => {
return remoteDb db.local.get('items', result => {
.doc(`users/${window.user.uid}`) resolve(result.items || {});
.get()
.then(doc => {
if (!doc.exists) {
return {};
}
return doc.data().items;
}); });
});
}, },
/** /**
* Fetches all items FROM REMOTE ONLY CURRENTLY!! * Fetches all items.
* TODO: make it work for local too. * @param {boolean} shouldFetchLocally Should forcefully fetch locally?
*/ */
async getAllItems() { async getAllItems(shouldFetchLocally) {
var t = Date.now(); var t = Date.now();
var d = deferred(); var d = deferred();
let itemIds = await this.getUserItemIds(); let itemIds = await this.getUserItemIds(shouldFetchLocally);
itemIds = Object.getOwnPropertyNames(itemIds || {}); itemIds = Object.getOwnPropertyNames(itemIds || {});
log('itemids', itemIds); log('itemids', itemIds);
if (!itemIds.length) { if (!itemIds.length) {
d.resolve([]); d.resolve([]);
} }
var remoteDb = await window.db.getDb();
const items = []; const items = [];
remoteDb
.collection('items')
.where('createdBy', '==', window.user.uid)
.onSnapshot(
function(querySnapshot) {
querySnapshot.forEach(function(doc) {
items.push(doc.data());
});
log('Items fetched in ', Date.now() - t, 'ms');
d.resolve(items); if (window.user && !shouldFetchLocally) {
}, var remoteDb = await window.db.getDb();
function() { remoteDb
d.resolve([]); .collection('items')
} .where('createdBy', '==', window.user.uid)
); .onSnapshot(
function(querySnapshot) {
querySnapshot.forEach(function(doc) {
items.push(doc.data());
});
log('Items fetched in ', Date.now() - t, 'ms');
d.resolve(items);
},
function() {
d.resolve([]);
}
);
} else {
for (let i = 0; i < itemIds.length; i++) {
/* eslint-disable no-loop-func */
window.db.local.get(itemIds[i], itemResult => {
items.push(itemResult[itemIds[i]]);
// Check if we have all items now.
if (itemIds.length === items.length) {
d.resolve(items);
}
});
/* eslint-enable no-loop-func */
}
}
return d.promise; return d.promise;
}, },