Load all past contributions once everything else is loaded

This commit is contained in:
2020-05-29 22:04:03 +02:00
parent c54d754c89
commit bac4c1c425
2 changed files with 32 additions and 2 deletions
+2
View File
@@ -32,6 +32,8 @@ export default Route.extend({
schedule('afterRender', this.kredits.syncContributions, schedule('afterRender', this.kredits.syncContributions,
this.kredits.syncContributions.perform); this.kredits.syncContributions.perform);
} }
schedule('afterRender', this.kredits.fetchAllContributions,
this.kredits.fetchAllContributions.perform);
}); });
} }
}); });
+30 -2
View File
@@ -8,7 +8,7 @@ import { alias, notEmpty } from '@ember/object/computed';
import { isEmpty, isPresent } from '@ember/utils'; import { isEmpty, isPresent } from '@ember/utils';
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency'; import { task, taskGroup } from 'ember-concurrency';
import groupBy from 'kredits-web/utils/group-by'; import groupBy from 'kredits-web/utils/group-by';
import processContributorData from 'kredits-web/utils/process-contributor-data'; import processContributorData from 'kredits-web/utils/process-contributor-data';
@@ -328,10 +328,12 @@ export default Service.extend({
}); });
}, },
contributionTasks: taskGroup().enqueue(),
syncContributions: task(function * () { syncContributions: task(function * () {
yield this.fetchNewContributions.perform(); yield this.fetchNewContributions.perform();
yield this.syncUnconfirmedContributions.perform(); yield this.syncUnconfirmedContributions.perform();
}), }).group('contributionTasks'),
fetchNewContributions: task(function * () { fetchNewContributions: task(function * () {
const count = yield this.kredits.Contribution.functions.contributionsCount(); const count = yield this.kredits.Contribution.functions.contributionsCount();
@@ -350,6 +352,32 @@ export default Service.extend({
} }
}), }),
fetchAllContributions: task(function * () {
const count = yield this.kredits.Contribution.functions.contributionsCount();
const allIds = [...Array(count+1).keys()];
allIds.shift(); // remove first item, which is 0
const loadedContributions = new Set(this.contributions.mapBy('id'));
const toFetch = allIds.filter(id => !loadedContributions.has(id));
if (toFetch.length === 0) {
console.debug(`[kredits] No contributions left to fetch`);
return;
}
console.debug(`[kredits] Fetching ${toFetch.length} past contributions`);
let countFetched = 0;
for (let id = count; id > 0; id--) {
if (loadedContributions.has(id)) {
continue;
} else {
const data = yield this.kredits.Contribution.getById(id);
const c = this.loadContributionFromData(data);
yield this.browserCache.contributions.setItem(c.id.toString(), c.serialize());
countFetched++;
}
}
console.debug(`[kredits] Cached ${countFetched} past contributions`);
}).group('contributionTasks'),
syncUnconfirmedContributions: task(function * () { syncUnconfirmedContributions: task(function * () {
if (this.contributionsUnconfirmed.length > 0) { if (this.contributionsUnconfirmed.length > 0) {
console.debug(`[kredits] Syncing unconfirmed contributions`); console.debug(`[kredits] Syncing unconfirmed contributions`);