|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
|
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
|
|
|
|
import * as DiscoveryActions from 'actions/DiscoveryActions';
|
|
|
|
|
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
|
|
|
|
|
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
|
|
|
|
|
|
|
|
|
@ -19,6 +20,10 @@ export type BlockchainAction =
|
|
|
|
|
| {
|
|
|
|
|
type: typeof BLOCKCHAIN.START_SUBSCRIBE,
|
|
|
|
|
shortcut: string,
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
type: typeof BLOCKCHAIN.FAIL_SUBSCRIBE,
|
|
|
|
|
shortcut: string,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Conditionally subscribe to blockchain backend
|
|
|
|
@ -128,6 +133,8 @@ export const onError = (
|
|
|
|
|
const network = config.networks.find(c => c.shortcut === shortcut);
|
|
|
|
|
if (!network) return;
|
|
|
|
|
|
|
|
|
|
dispatch(autoReconnect(shortcut));
|
|
|
|
|
|
|
|
|
|
switch (network.type) {
|
|
|
|
|
case 'ethereum':
|
|
|
|
|
await dispatch(EthereumBlockchainActions.onError(shortcut));
|
|
|
|
@ -140,3 +147,28 @@ export const onError = (
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const autoReconnect = (shortcut: string): PromiseAction<void> => async (
|
|
|
|
|
dispatch: Dispatch,
|
|
|
|
|
getState: GetState
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const MAX_ATTEMPTS = 4;
|
|
|
|
|
let blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
|
|
|
|
// try to automatically reconnect and wait after each attemp (5s * #attempt) untill max number of attemps is reached
|
|
|
|
|
for (let i = 0; i < MAX_ATTEMPTS; i++) {
|
|
|
|
|
const waitTime = 5000 * (i + 1); /// 5s * #attempt
|
|
|
|
|
if (!blockchain || blockchain.connected) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
|
|
|
|
|
|
|
|
|
// reconnect with 7s timeout
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await dispatch(DiscoveryActions.reconnect(shortcut, 7000));
|
|
|
|
|
|
|
|
|
|
// wait before next try
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|