Merge pull request #205 from 67P/feature/improve_caching

Improve caching, fix Firefox private mode usage
This commit was merged in pull request #205.
This commit is contained in:
Râu Cao
2022-11-14 15:48:33 +01:00
committed by GitHub
4 changed files with 35 additions and 19 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ export default Controller.extend({
vetoContribution (contributionId) { vetoContribution (contributionId) {
this.kredits.veto(contributionId).then(transaction => { this.kredits.veto(contributionId).then(transaction => {
console.debug('[controllers:index] Veto submitted to Ethereum blockhain: '+transaction.hash); console.debug('[controllers:index] Veto submitted to chain: '+transaction.hash);
}); });
}, },
+14 -13
View File
@@ -305,11 +305,11 @@ export default Service.extend({
}), }),
addContribution (attributes) { addContribution (attributes) {
console.debug('[kredits] add contribution', attributes); console.debug('[kredits] Adding contribution', attributes);
return this.kredits.Contribution.addContribution(attributes, { gasLimit: 300000 }) return this.kredits.Contribution.add(attributes, { gasLimit: 300000 })
.then(data => { .then(data => {
console.debug('[kredits] add contribution response', data); console.debug('[kredits] Contribution.add response', data);
attributes.contributor = this.contributors.findBy('id', attributes.contributorId); attributes.contributor = this.contributors.findBy('id', attributes.contributorId);
const contribution = Contribution.create(attributes); const contribution = Contribution.create(attributes);
contribution.set('pendingTx', data); contribution.set('pendingTx', data);
@@ -667,22 +667,23 @@ export default Service.extend({
}); });
if (pendingContribution) { if (pendingContribution) {
const attributes = await this.kredits.Contribution.getById(id);
attributes.contributor = this.contributors.findBy('id', attributes.contributorId);
const newContribution = Contribution.create(attributes);
this.contributions.addObject(newContribution);
this.contributions.removeObject(pendingContribution); this.contributions.removeObject(pendingContribution);
} }
const data = await this.kredits.Contribution.getById(id);
const c = this.loadContributionFromData(data);
await this.browserCache.contributions.setItem(c.id.toString(), c.serialize());
}, },
handleContributionVetoed (contributionId) { async handleContributionVetoed (contributionId) {
console.debug('[kredits] ContributionVetoed event received for ', contributionId); console.debug('[kredits] ContributionVetoed event received for ', contributionId);
const contribution = this.contributions.findBy('id', contributionId); const c = this.contributions.findBy('id', contributionId);
console.debug('[kredits] contribution', contribution);
if (contribution) { if (c) {
contribution.set('vetoed', true); console.debug('[kredits] Updating contribution', c);
contribution.set('pendingTx', null); c.set('vetoed', true);
c.set('pendingTx', null);
await this.browserCache.contributions.setItem(c.id.toString(), c.serialize());
} }
}, },
+6 -2
View File
@@ -1,4 +1,4 @@
export default function processContributionData(data) { export default function processContributionData(data, options={}) {
const processed = {} const processed = {}
if (data.confirmedAtBlock && (typeof data.confirmedAtBlock.toNumber === 'function')) { if (data.confirmedAtBlock && (typeof data.confirmedAtBlock.toNumber === 'function')) {
@@ -9,12 +9,16 @@ export default function processContributionData(data) {
const otherProperties = [ const otherProperties = [
'id', 'contributorId', 'amount', 'vetoed', 'ipfsHash', 'kind', 'id', 'contributorId', 'amount', 'vetoed', 'ipfsHash', 'kind',
'description', 'details', 'url', 'date', 'time', 'pendingTx' 'description', 'url', 'date', 'time', 'pendingTx'
]; ];
otherProperties.forEach(prop => { otherProperties.forEach(prop => {
processed[prop] = data[prop]; processed[prop] = data[prop];
}); });
if (options.includeDetails) {
processed.details = data.details;
}
return processed; return processed;
} }
@@ -4,16 +4,18 @@ import testData from '../../fixtures/contribution-data';
module('Unit | Utility | process-contribution-data', function() { module('Unit | Utility | process-contribution-data', function() {
let result = processContributionData(testData);
test('formats the data correctly', function(assert) { test('formats the data correctly', function(assert) {
const result = processContributionData(testData);
assert.ok(typeof result.confirmedAt === 'number'); assert.ok(typeof result.confirmedAt === 'number');
}); });
test('copies other properties', function(assert) { test('copies other properties', function(assert) {
const result = processContributionData(testData);
[ [
'id', 'contributorId', 'amount', 'vetoed', 'id', 'contributorId', 'amount', 'vetoed',
'ipfsHash', 'kind', 'description', 'details', 'ipfsHash', 'kind', 'description',
'url', 'date', 'time', 'pendingTx' 'url', 'date', 'time', 'pendingTx'
].forEach(p => { ].forEach(p => {
assert.ok(Object.prototype.hasOwnProperty.call(result, p), `copies property ${p}`); assert.ok(Object.prototype.hasOwnProperty.call(result, p), `copies property ${p}`);
@@ -21,8 +23,17 @@ module('Unit | Utility | process-contribution-data', function() {
}); });
test('does not copy unnecessary properties', function(assert) { test('does not copy unnecessary properties', function(assert) {
const result = processContributionData(testData);
['exists', '5'].forEach(p => { ['exists', '5'].forEach(p => {
assert.notOk(Object.prototype.hasOwnProperty.call(result, p)); assert.notOk(Object.prototype.hasOwnProperty.call(result, p));
}) })
}); });
test('includeDetails option', function(assert) {
const result = processContributionData(testData, { includeDetails: true });
assert.ok(Object.prototype.hasOwnProperty.call(result, 'details'), 'includes the details property/value');
});
}); });