reverse caching: middleware

This commit is contained in:
Jannis R 2017-01-05 16:56:33 +01:00
parent ac2c678e69
commit b38aa8d01f
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5
2 changed files with 110 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import SignerMiddleware from './providers/signerMiddleware';
import statusMiddleware from '~/views/Status/middleware';
import CertificationsMiddleware from './providers/certifications/middleware';
import ChainMiddleware from './providers/chainMiddleware';
import RegistryMiddleware from './providers/registry/middleware';
export default function (api, browserHistory) {
const errors = new ErrorsMiddleware();
@ -32,13 +33,15 @@ export default function (api, browserHistory) {
const certifications = new CertificationsMiddleware();
const routeMiddleware = routerMiddleware(browserHistory);
const chain = new ChainMiddleware();
const registry = new RegistryMiddleware();
const middleware = [
settings.toMiddleware(),
signer.toMiddleware(),
errors.toMiddleware(),
certifications.toMiddleware(),
chain.toMiddleware()
chain.toMiddleware(),
registry.toMiddleware()
];
return middleware.concat(status, routeMiddleware, thunk);

View File

@ -0,0 +1,106 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Contracts from '~/contracts';
import subscribeToEvent from '~/util/subscribe-to-event';
import registryABI from '~/contracts/abi/registry.json';
import { setReverse, startCachingReverses } from './actions';
export default class RegistryMiddleware {
toMiddleware () {
return (store) => {
const api = Contracts.get()._api;
let contract, confirmedEvents, removedEvents, interval;
let addressesToCheck = {};
const onLog = (log) => {
switch (log.event) {
case 'ReverseConfirmed':
addressesToCheck[log.params.reverse.value] = true;
break;
case 'ReverseRemoved':
delete addressesToCheck[log.params.reverse.value];
break;
}
};
const checkReverses = () => {
Object
.keys(addressesToCheck)
.forEach((address) => {
contract
.instance
.reverse
.call({}, [ address ])
.then((reverse) => store.dispatch(setReverse(address, reverse)));
});
addressesToCheck = {};
};
return (next) => (action) => {
switch (action.type) {
case 'initAll':
next(action);
store.dispatch(startCachingReverses());
break;
case 'startCachingReverses':
const { registry } = Contracts.get();
registry.getInstance()
.then((instance) => api.newContract(registryABI, instance.address))
.then((_contract) => {
contract = _contract;
confirmedEvents = subscribeToEvent(_contract, 'ReverseConfirmed');
confirmedEvents.on('log', onLog);
removedEvents = subscribeToEvent(_contract, 'ReverseRemoved');
removedEvents.on('log', onLog);
interval = setInterval(checkReverses, 2000);
})
.catch((err) => {
console.error('Failed to start caching reverses:', err);
throw err;
});
break;
case 'stopCachingReverses':
if (confirmedEvents) {
confirmedEvents.unsubscribe();
}
if (removedEvents) {
removedEvents.unsubscribe();
}
if (interval) {
clearInterval(interval);
}
break;
default:
next(action);
}
};
};
}
};