From d4b546bfcd9cf74057e0eb7cd8b49d585fef90d0 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Thu, 28 May 2020 13:59:10 +0200 Subject: [PATCH] Load contributions from cache if present And then fetch more from network. Also, only load 30 from network before first render (with nothing cached), then load more after the page has rendered. --- app/routes/application.js | 1 + app/services/kredits.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/routes/application.js b/app/routes/application.js index eae822b..b5fdfc9 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -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); }); } }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 313d504..c33b347 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -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);