|
|
@ -1,9 +1,9 @@
|
|
|
|
/* @flow */
|
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
|
|
|
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
|
|
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
|
|
|
import * as DiscoveryActions from 'actions/DiscoveryActions';
|
|
|
|
|
|
|
|
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
|
|
|
|
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
|
|
|
|
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
|
|
|
|
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
|
|
|
|
|
|
|
|
import { resolveAfter } from 'utils/promiseUtils';
|
|
|
|
|
|
|
|
|
|
|
|
import type { Dispatch, GetState, PromiseAction, BlockchainFeeLevel } from 'flowtype';
|
|
|
|
import type { Dispatch, GetState, PromiseAction, BlockchainFeeLevel } from 'flowtype';
|
|
|
|
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
|
|
|
|
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
|
|
|
@ -20,10 +20,6 @@ export type BlockchainAction =
|
|
|
|
| {
|
|
|
|
| {
|
|
|
|
type: typeof BLOCKCHAIN.START_SUBSCRIBE,
|
|
|
|
type: typeof BLOCKCHAIN.START_SUBSCRIBE,
|
|
|
|
shortcut: string,
|
|
|
|
shortcut: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
| {
|
|
|
|
|
|
|
|
type: typeof BLOCKCHAIN.FAIL_SUBSCRIBE,
|
|
|
|
|
|
|
|
shortcut: string,
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Conditionally subscribe to blockchain backend
|
|
|
|
// Conditionally subscribe to blockchain backend
|
|
|
@ -153,23 +149,13 @@ const autoReconnect = (shortcut: string): PromiseAction<void> => async (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
getState: GetState
|
|
|
|
): Promise<void> => {
|
|
|
|
): Promise<void> => {
|
|
|
|
const MAX_ATTEMPTS = 4;
|
|
|
|
|
|
|
|
let blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
|
|
|
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
|
|
|
|
if (!blockchain || blockchain.reconnectionAttempts >= 5) return;
|
|
|
|
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);
|
|
|
|
await resolveAfter(5000 * (blockchain.reconnectionAttempts + 1));
|
|
|
|
|
|
|
|
|
|
|
|
// reconnect with 7s timeout
|
|
|
|
blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
if (!blockchain || blockchain.connected || blockchain.connecting) return;
|
|
|
|
await dispatch(DiscoveryActions.reconnect(shortcut, 7000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// wait before next try
|
|
|
|
await dispatch(subscribe(shortcut));
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|