From cd486fc3ba8f053de11494c8c3407e434dfe3c4e Mon Sep 17 00:00:00 2001 From: Kushagra Gour Date: Fri, 8 Mar 2019 14:34:49 +0530 Subject: [PATCH] itemservice: make fetch methods support for remote and local fetching --- src/components/app.jsx | 43 +++++------------------ src/itemService.js | 77 ++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 68 deletions(-) diff --git a/src/components/app.jsx b/src/components/app.jsx index 793bc76..37982b5 100644 --- a/src/components/app.jsx +++ b/src/components/app.jsx @@ -473,47 +473,20 @@ export default class App extends Component { * @return {promise} Promise. */ async fetchItems(shouldSaveGlobally, shouldFetchLocally) { - var d = deferred(); // HACK: This empty assignment is being used when importing locally saved items // to cloud, `fetchItems` runs once on account login which clears the // savedItems object and hence, while merging no saved item matches with itself. this.state.savedItems = {}; var items = []; - if (window.user && !shouldFetchLocally) { - items = await itemService.getAllItems(); - log('got items'); - if (shouldSaveGlobally) { - items.forEach(item => { - this.state.savedItems[item.id] = item; - }); - } - d.resolve(items); - return d.promise; + + items = await itemService.getAllItems(shouldFetchLocally); + trackEvent('fn', 'fetchItems', items.length); + if (shouldSaveGlobally) { + items.forEach(item => { + this.state.savedItems[item.id] = item; + }); } - db.local.get('items', result => { - 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; + return items; } openSavedItemsPane() { diff --git a/src/itemService.js b/src/itemService.js index e657b7e..02ed8aa 100644 --- a/src/itemService.js +++ b/src/itemService.js @@ -13,59 +13,70 @@ export const itemService = { }, /** - * Fetches user's item id list. CURRENTLY RUNS ONLY - * FOR LOGGED IN USER!! + * Fetches all item ids of current user + * @param {boolean} shouldFetchLocally Should forcefully fetch locally? */ - async getUserItemIds() { - if (window.user) { + async getUserItemIds(shouldFetchLocally) { + if (window.user && !shouldFetchLocally) { return new Promise(resolve => { resolve(window.user.items || {}); }); } - var remoteDb = await window.db.getDb(); - return remoteDb - .doc(`users/${window.user.uid}`) - .get() - .then(doc => { - if (!doc.exists) { - return {}; - } - return doc.data().items; + return new Promise(resolve => { + db.local.get('items', result => { + resolve(result.items || {}); }); + }); }, /** - * Fetches all items FROM REMOTE ONLY CURRENTLY!! - * TODO: make it work for local too. + * Fetches all items. + * @param {boolean} shouldFetchLocally Should forcefully fetch locally? */ - async getAllItems() { + async getAllItems(shouldFetchLocally) { var t = Date.now(); var d = deferred(); - let itemIds = await this.getUserItemIds(); + let itemIds = await this.getUserItemIds(shouldFetchLocally); itemIds = Object.getOwnPropertyNames(itemIds || {}); log('itemids', itemIds); if (!itemIds.length) { d.resolve([]); } - var remoteDb = await window.db.getDb(); 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); - }, - function() { - d.resolve([]); - } - ); + if (window.user && !shouldFetchLocally) { + var remoteDb = await window.db.getDb(); + 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); + }, + 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; },