Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

cache registry reverses in local storage #4182

Merged
merged 10 commits into from
Jan 20, 2017
Prev Previous commit
Next Next commit
registry caching: store last block
  • Loading branch information
derhuerst committed Jan 16, 2017

Verified

This commit was signed with the committer’s verified signature.
MindTooth Birger Johan Nordølum
commit 1c2adc31d72fe89ffc65c049ec795d3a9697dc64
40 changes: 27 additions & 13 deletions js/src/redux/providers/registry/middleware.js
Original file line number Diff line number Diff line change
@@ -23,21 +23,28 @@ import registryABI from '~/contracts/abi/registry.json';
import { setReverse, startCachingReverses } from './actions';

const read = () => {
const data = window.localStorage.getItem('registry-reverses');
if (!data) {
const reverses = window.localStorage.getItem('registry-reverses');
const lastBlock = window.localStorage.getItem('registry-reverses-last-block');
if (!reverses || !lastBlock) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCD-related comment (plus I'm old) - some spacing between vars and action (i.e. blank line here) would be appreciated.

return null;
}

try {
return JSON.parse(data);
return {
reverses: JSON.parse(reverses),
lastBlock: JSON.parse(lastBlock)
};
} catch (_) {
return null;
}
};

const write = debounce((getReverses) => {
const write = debounce((getReverses, getLastBlock) => {
const reverses = getReverses();
const lastBlock = getLastBlock();

window.localStorage.setItem('registry-reverses', JSON.stringify(reverses));
window.localStorage.setItem('registry-reverses-last-block', JSON.stringify(lastBlock));
}, 20000);

export default (api) => (store) => {
@@ -84,12 +91,23 @@ export default (api) => (store) => {
case 'startCachingReverses':
const { registry } = Contracts.get();

const cached = read();
if (cached) {
Object
.entries(cached.reverses)
.forEach(([ address, reverse ]) => store.dispatch(setReverse(address, reverse)));
}

registry.getInstance()
.then((instance) => api.newContract(registryABI, instance.address))
.then((_contract) => {
contract = _contract;

subscription = subscribeToEvents(_contract, ['ReverseConfirmed', 'ReverseRemoved']);
subscription = subscribeToEvents(_contract, [
'ReverseConfirmed', 'ReverseRemoved'
], {
from: cached ? cached.lastBlock : 0
});
subscription.on('log', onLog);

timeout = setTimeout(checkReverses, 10000);
@@ -100,13 +118,6 @@ export default (api) => (store) => {
throw err;
});

const cached = read();
if (cached) {
Object
.entries(cached)
.forEach(([ address, reverse ]) => store.dispatch(setReverse(address, reverse)));
}

break;
case 'stopCachingReverses':
if (subscription) {
@@ -123,7 +134,10 @@ export default (api) => (store) => {

break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for blank line before.

case 'setReverse':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before.

write(() => store.getState().registry.reverse);
write(
() => store.getState().registry.reverse,
() => +store.getState().nodeStatus.blockNumber
);
next(action);

break;
2 changes: 1 addition & 1 deletion js/src/redux/providers/registry/reducer.js
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export default (state = initialState, action) => {
return state;
}

return { ...state, reverse: {
return { reverse: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer spacey blocks reverse on next line just aligning with what we have. (Plus each entry on it's own line)

...state.reverse, [ action.address ]: action.reverse
} };
}
11 changes: 11 additions & 0 deletions js/src/util/subscribe-to-events.spec.js
Original file line number Diff line number Diff line change
@@ -111,4 +111,15 @@ describe('util/subscribe-to-events', () => {
expect(onBar.callCount).to.be.at.least(1);
expect(onBar.firstCall.args).to.eql([ liveLogs[0] ]);
});

it('accepts a custom block range', async function () {
const { api, contract } = this;

subscribeToEvents(contract, [ 'Foo' ], { from: 123, to: 321 });

await delay(0);
expect(api.eth.newFilter.callCount).to.equal(1);
expect(api.eth.newFilter.firstCall.args[0].fromBlock).to.equal(123);
expect(api.eth.newFilter.firstCall.args[0].toBlock).to.equal(321);
});
});