Extract data loading from fetch methods

This commit is contained in:
2020-05-29 13:12:39 +02:00
parent bf6d9dbc31
commit eb2c613ac9
+19 -11
View File
@@ -225,12 +225,9 @@ export default Service.extend({
console.debug(`[kredits] Fetching all contributors from the network`); 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.forEach(data => {
const contributor = Contributor.create(processContributorData(data)); this.loadContributorFromData(data);
const loadedContributor = this.contributors.findBy('id', contributor.id); return;
if (loadedContributor) { this.contributors.removeObject(loadedContributor); }
this.contributors.pushObject(contributor);
return contributor;
}); });
}) })
.then(() => { .then(() => {
@@ -238,6 +235,13 @@ export default Service.extend({
}); });
}, },
loadContributorFromData(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);
},
async cacheLoadedContributors () { async cacheLoadedContributors () {
for (const c of this.contributors) { for (const c of this.contributors) {
await this.browserCache.contributors.setItem(c.id, c.serialize()); await this.browserCache.contributors.setItem(c.id, c.serialize());
@@ -274,11 +278,7 @@ export default Service.extend({
return this.kredits.Contribution.all(options) return this.kredits.Contribution.all(options)
.then(contributions => { .then(contributions => {
return contributions.map(data => { return contributions.map(data => {
const contribution = Contribution.create(processContributionData(data)); loadContributionFromData(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; return contribution;
}); });
}) })
@@ -292,6 +292,14 @@ export default Service.extend({
}); });
}, },
loadContributionFromData(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);
},
async cacheLoadedContributions () { async cacheLoadedContributions () {
for (const c of this.contributions) { for (const c of this.contributions) {
await this.browserCache.contributions.setItem(c.id, c.serialize()); await this.browserCache.contributions.setItem(c.id, c.serialize());