From 50e1fc728cce709f5b84c34d360f7399878f2707 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Wed, 27 May 2020 17:33:36 +0200 Subject: [PATCH] Cache contributors in IndexedDB --- app/models/contributor.js | 3 +++ app/services/kredits.js | 22 ++++++++++++++++++---- tests/unit/services/browser-cache-test.js | 1 - 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/models/contributor.js b/app/models/contributor.js index 8e9f39e..67cfa30 100644 --- a/app/models/contributor.js +++ b/app/models/contributor.js @@ -19,4 +19,7 @@ export default EmberObject.extend({ wiki_username: null, zoom_display_name: null, + serialize() { + return JSON.stringify(this); + } }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 06d8d3f..73c4062 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -7,6 +7,7 @@ import EmberObject from '@ember/object'; import { computed } from '@ember/object'; import { alias, notEmpty } from '@ember/object/computed'; import { isEmpty, isPresent } from '@ember/utils'; +import { inject as service } from '@ember/service'; import groupBy from 'kredits-web/utils/group-by'; import processContributorData from 'kredits-web/utils/process-contributor-data'; @@ -18,6 +19,8 @@ import Contribution from 'kredits-web/models/contribution' export default Service.extend({ + browserCache: service(), + currentBlock: null, currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. currentUser: null, @@ -172,8 +175,9 @@ export default Service.extend({ loadInitialData () { - return this.getContributors() - .then(contributors => this.contributors.pushObjects(contributors)) + return this.fetchContributors() + // .then(contributors => this.contributors.pushObjects(contributors)) + .then(() => this.cacheContributors()) .then(() => this.getContributions()) .then(contributions => this.contributions.pushObjects(contributions)) }, @@ -206,15 +210,25 @@ export default Service.extend({ }); }, - getContributors () { + fetchContributors () { return this.kredits.Contributor.all() .then(contributors => { return contributors.map(data => { - return Contributor.create(processContributorData(data)); + const contributor = Contributor.create(processContributorData(data)); + this.contributors.pushObject(contributor); + return contributor; }); }); }, + async cacheContributors () { + for (const c of this.contributors) { + await this.browserCache.contributors.setItem(c.id, c.serialize()); + } + console.debug(`[kredits] Cached ${this.contributors.length} contributors in browser storage`); + return Promise.resolve(); + }, + addContribution (attributes) { console.debug('[kredits] add contribution', attributes); diff --git a/tests/unit/services/browser-cache-test.js b/tests/unit/services/browser-cache-test.js index 6cdd09e..7c77937 100644 --- a/tests/unit/services/browser-cache-test.js +++ b/tests/unit/services/browser-cache-test.js @@ -14,5 +14,4 @@ module('Unit | Service | browser-cache', function(hooks) { assert.equal(cache.contributors._config.name, "kredits:contributors"); assert.equal(cache.contributions._config.name, "kredits:contributions"); }); - });