Load contributors from cache if present
And then update from network later
This commit is contained in:
@@ -19,7 +19,7 @@ export default EmberObject.extend({
|
||||
wiki_username: null,
|
||||
zoom_display_name: null,
|
||||
|
||||
serialize() {
|
||||
serialize () {
|
||||
return JSON.stringify(this);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
+43
-11
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user