From 832de3c06fafd0340c44edb11d3171e2e59357b1 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Thu, 28 May 2020 10:54:38 +0200 Subject: [PATCH] Load contributors from cache if present And then update from network later --- app/models/contributor.js | 2 +- app/routes/application.js | 6 +++++ app/services/kredits.js | 54 +++++++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/app/models/contributor.js b/app/models/contributor.js index 67cfa30..40f6448 100644 --- a/app/models/contributor.js +++ b/app/models/contributor.js @@ -19,7 +19,7 @@ export default EmberObject.extend({ wiki_username: null, zoom_display_name: null, - serialize() { + serialize () { return JSON.stringify(this); } }); diff --git a/app/routes/application.js b/app/routes/application.js index 85eabe9..eae822b 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -1,5 +1,6 @@ import { inject as service } from '@ember/service'; import Route from '@ember/routing/route'; +import { schedule } from '@ember/runloop'; export default Route.extend({ kredits: service(), @@ -21,6 +22,11 @@ export default Route.extend({ return this.kredits.loadInitialData() .then(() => { this.kredits.addContractEventHandlers(); + }) + .then(() => { + if (this.kredits.contributorsNeedFetch) { + schedule('afterRender', this.kredits, this.kredits.fetchContributors); + } }); } }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 73c4062..2242dc0 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -20,6 +20,8 @@ import Contribution from 'kredits-web/models/contribution' 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. @@ -173,13 +175,23 @@ export default Service.extend({ .then(total => total.toNumber()); }), + async loadInitialData () { + const numCachedContributors = await this.browserCache.contributors.length(); + if (numCachedContributors > 0) { + await this.loadContributorsFromCache(); + this.set('contributorsNeedFetch', true); + } else { + await this.fetchContributors(); + } - loadInitialData () { - return this.fetchContributors() - // .then(contributors => this.contributors.pushObjects(contributors)) - .then(() => this.cacheContributors()) - .then(() => this.getContributions()) - .then(contributions => this.contributions.pushObjects(contributions)) + const numCachedContributions = await this.browserCache.contributions.length(); + if (numCachedContributions > 0) { + // TODO promises.push(this.loadContributionsFromCache); + } else { + await this.fetchContributions({ page: { size: 30 } }); + } + + return Promise.resolve(); }, addContributor (attributes) { @@ -211,13 +223,19 @@ export default Service.extend({ }, fetchContributors () { + console.debug(`[kredits] Fetching all contributors from the network`); return this.kredits.Contributor.all() .then(contributors => { return contributors.map(data => { const contributor = Contributor.create(processContributorData(data)); + const loadedContributor = this.contributors.findBy('id', contributor.id); + if (loadedContributor) { this.contributors.removeObject(loadedContributor); } this.contributors.pushObject(contributor); return contributor; }); + }) + .then(() => { + return this.cacheContributors(); }); }, @@ -229,6 +247,14 @@ export default Service.extend({ return Promise.resolve(); }, + async loadContributorsFromCache () { + return this.browserCache.contributors.iterate((value, key/* , iterationNumber */) => { + this.contributors.pushObject(Contributor.create(JSON.parse(value))); + }).then(result => { + console.debug(`[kredits] Loaded ${this.contributors.length} contributors from cache`); + }); + }, + addContribution (attributes) { console.debug('[kredits] add contribution', attributes); @@ -244,14 +270,20 @@ export default Service.extend({ }); }, - getContributions () { - return this.kredits.Contribution.all({page: {size: 30}}) + fetchContributions (options = { page: { size: 200 } }) { + console.debug(`[kredits] Fetching contributions from the network`); + return this.kredits.Contribution.all(options) .then(contributions => { - return contributions.map(contribution => { - contribution.contributor = this.contributors.findBy('id', contribution.contributorId.toString()); - return Contribution.create(contribution); + return contributions.map(data => { + data.contributor = this.contributors.findBy('id', data.contributorId.toString()); + const contribution = Contribution.create(data); + this.contributions.pushObject(contribution); + return contribution; }); }); + // TODO .then(() => { + // this.cacheContributions() + // }); }, veto (contributionId) {