Cache contributors in IndexedDB
This commit is contained in:
@@ -19,4 +19,7 @@ export default EmberObject.extend({
|
|||||||
wiki_username: null,
|
wiki_username: null,
|
||||||
zoom_display_name: null,
|
zoom_display_name: null,
|
||||||
|
|
||||||
|
serialize() {
|
||||||
|
return JSON.stringify(this);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+18
-4
@@ -7,6 +7,7 @@ import EmberObject from '@ember/object';
|
|||||||
import { computed } from '@ember/object';
|
import { computed } from '@ember/object';
|
||||||
import { alias, notEmpty } from '@ember/object/computed';
|
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 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';
|
||||||
@@ -18,6 +19,8 @@ import Contribution from 'kredits-web/models/contribution'
|
|||||||
|
|
||||||
export default Service.extend({
|
export default Service.extend({
|
||||||
|
|
||||||
|
browserCache: service(),
|
||||||
|
|
||||||
currentBlock: null,
|
currentBlock: null,
|
||||||
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
|
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
|
||||||
currentUser: null,
|
currentUser: null,
|
||||||
@@ -172,8 +175,9 @@ export default Service.extend({
|
|||||||
|
|
||||||
|
|
||||||
loadInitialData () {
|
loadInitialData () {
|
||||||
return this.getContributors()
|
return this.fetchContributors()
|
||||||
.then(contributors => this.contributors.pushObjects(contributors))
|
// .then(contributors => this.contributors.pushObjects(contributors))
|
||||||
|
.then(() => this.cacheContributors())
|
||||||
.then(() => this.getContributions())
|
.then(() => this.getContributions())
|
||||||
.then(contributions => this.contributions.pushObjects(contributions))
|
.then(contributions => this.contributions.pushObjects(contributions))
|
||||||
},
|
},
|
||||||
@@ -206,15 +210,25 @@ export default Service.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getContributors () {
|
fetchContributors () {
|
||||||
return this.kredits.Contributor.all()
|
return this.kredits.Contributor.all()
|
||||||
.then(contributors => {
|
.then(contributors => {
|
||||||
return contributors.map(data => {
|
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) {
|
addContribution (attributes) {
|
||||||
console.debug('[kredits] add contribution', attributes);
|
console.debug('[kredits] add contribution', attributes);
|
||||||
|
|
||||||
|
|||||||
@@ -14,5 +14,4 @@ module('Unit | Service | browser-cache', function(hooks) {
|
|||||||
assert.equal(cache.contributors._config.name, "kredits:contributors");
|
assert.equal(cache.contributors._config.name, "kredits:contributors");
|
||||||
assert.equal(cache.contributions._config.name, "kredits:contributions");
|
assert.equal(cache.contributions._config.name, "kredits:contributions");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user