Cache data in browser storage, and load from local cache when present #189

Merged
raucao merged 8 commits from feature/data_loading into master 2020-05-31 08:44:37 +00:00
2 changed files with 15 additions and 5 deletions
Showing only changes of commit d4b546bfcd - Show all commits
+1
View File
@@ -27,6 +27,7 @@ export default Route.extend({
if (this.kredits.contributorsNeedFetch) {
schedule('afterRender', this.kredits, this.kredits.fetchContributors);
}
schedule('afterRender', this.kredits, this.kredits.fetchContributions);
});
}
});
+14 -5
View File
@@ -22,7 +22,6 @@ export default Service.extend({
browserCache: service(),
contributorsNeedFetch: false,
contributionsNeedFetch: false,
currentBlock: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
@@ -186,11 +185,11 @@ export default Service.extend({
}
const numCachedContributions = await this.browserCache.contributions.length();
// if (numCachedContributions > 0) {
// TODO promises.push(this.loadContributionsFromCache);
// } else {
if (numCachedContributions > 0) {
await this.loadContributionsFromCache();
} else {
await this.fetchContributions({ page: { size: 30 } });
// }
}
return Promise.resolve();
},
@@ -278,6 +277,8 @@ export default Service.extend({
return contributions.map(data => {
const contribution = Contribution.create(processContributionData(data));
contribution.set('contributor', this.contributors.findBy('id', data.contributorId.toString()));
const loadedContribution = this.contributions.findBy('id', contribution.id);
if (loadedContribution) { this.contributions.removeObject(loadedContribution); }
this.contributions.pushObject(contribution);
return contribution;
});
@@ -300,6 +301,14 @@ export default Service.extend({
return Promise.resolve();
},
async loadContributionsFromCache () {
return this.browserCache.contributions.iterate((value/*, key , iterationNumber */) => {
this.contributions.pushObject(Contribution.create(JSON.parse(value)));
}).then((/* result */) => {
console.debug(`[kredits] Loaded ${this.contributions.length} contributions from cache`);
});
},
veto (contributionId) {
console.debug('[kredits] veto against', contributionId);
const contribution = this.contributions.findBy('id', contributionId);