1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-02-22 06:03:22 +01: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.
*/
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() {

View File

@ -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;
},