Load contributors from cache if present

And then update from network later
This commit is contained in:
2020-05-28 10:54:38 +02:00
parent 50e1fc728c
commit 832de3c06f
3 changed files with 50 additions and 12 deletions
+6
View File
@@ -1,5 +1,6 @@
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import Route from '@ember/routing/route'; import Route from '@ember/routing/route';
import { schedule } from '@ember/runloop';
export default Route.extend({ export default Route.extend({
kredits: service(), kredits: service(),
@@ -21,6 +22,11 @@ export default Route.extend({
return this.kredits.loadInitialData() return this.kredits.loadInitialData()
.then(() => { .then(() => {
this.kredits.addContractEventHandlers(); this.kredits.addContractEventHandlers();
})
.then(() => {
if (this.kredits.contributorsNeedFetch) {
schedule('afterRender', this.kredits, this.kredits.fetchContributors);
}
}); });
} }
}); });
+43 -11
View File
@@ -20,6 +20,8 @@ import Contribution from 'kredits-web/models/contribution'
export default Service.extend({ export default Service.extend({
browserCache: service(), browserCache: service(),
contributorsNeedFetch: false,
contributionsNeedFetch: false,
currentBlock: null, currentBlock: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. 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()); .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 () { const numCachedContributions = await this.browserCache.contributions.length();
return this.fetchContributors() if (numCachedContributions > 0) {
// .then(contributors => this.contributors.pushObjects(contributors)) // TODO promises.push(this.loadContributionsFromCache);
.then(() => this.cacheContributors()) } else {
.then(() => this.getContributions()) await this.fetchContributions({ page: { size: 30 } });
.then(contributions => this.contributions.pushObjects(contributions)) }
return Promise.resolve();
}, },
addContributor (attributes) { addContributor (attributes) {
@@ -211,13 +223,19 @@ export default Service.extend({
}, },
fetchContributors () { fetchContributors () {
console.debug(`[kredits] Fetching all contributors from the network`);
return this.kredits.Contributor.all() return this.kredits.Contributor.all()
.then(contributors => { .then(contributors => {
return contributors.map(data => { return contributors.map(data => {
const contributor = Contributor.create(processContributorData(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); this.contributors.pushObject(contributor);
return contributor; return contributor;
}); });
})
.then(() => {
return this.cacheContributors();
}); });
}, },
@@ -229,6 +247,14 @@ export default Service.extend({
return Promise.resolve(); 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) { addContribution (attributes) {
console.debug('[kredits] add contribution', attributes); console.debug('[kredits] add contribution', attributes);
@@ -244,14 +270,20 @@ export default Service.extend({
}); });
}, },
getContributions () { fetchContributions (options = { page: { size: 200 } }) {
return this.kredits.Contribution.all({page: {size: 30}}) console.debug(`[kredits] Fetching contributions from the network`);
return this.kredits.Contribution.all(options)
.then(contributions => { .then(contributions => {
return contributions.map(contribution => { return contributions.map(data => {
contribution.contributor = this.contributors.findBy('id', contribution.contributorId.toString()); data.contributor = this.contributors.findBy('id', data.contributorId.toString());
return Contribution.create(contribution); const contribution = Contribution.create(data);
this.contributions.pushObject(contribution);
return contribution;
}); });
}); });
// TODO .then(() => {
// this.cacheContributions()
// });
}, },
veto (contributionId) { veto (contributionId) {