From bac4c1c42561cc7271c10ecba5ded701efe25bcd Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 29 May 2020 22:04:03 +0200 Subject: [PATCH] Load all past contributions once everything else is loaded --- app/routes/application.js | 2 ++ app/services/kredits.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/routes/application.js b/app/routes/application.js index 3aec684..d97076f 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -32,6 +32,8 @@ export default Route.extend({ schedule('afterRender', this.kredits.syncContributions, this.kredits.syncContributions.perform); } + schedule('afterRender', this.kredits.fetchAllContributions, + this.kredits.fetchAllContributions.perform); }); } }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 559afd3..5d829f6 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -8,7 +8,7 @@ import { alias, notEmpty } from '@ember/object/computed'; import { isEmpty, isPresent } from '@ember/utils'; 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 processContributorData from 'kredits-web/utils/process-contributor-data'; @@ -328,10 +328,12 @@ export default Service.extend({ }); }, + contributionTasks: taskGroup().enqueue(), + syncContributions: task(function * () { yield this.fetchNewContributions.perform(); yield this.syncUnconfirmedContributions.perform(); - }), + }).group('contributionTasks'), fetchNewContributions: task(function * () { 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 * () { if (this.contributionsUnconfirmed.length > 0) { console.debug(`[kredits] Syncing unconfirmed contributions`);