rewrite BlockchainActions.autoReconnect method

pull/615/head
Szymon Lesisz 5 years ago
parent 4c3d0d3b92
commit eb20aee923

@ -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));
}
}; };

@ -1,5 +1,5 @@
/* @flow */ /* @flow */
import { DEVICE } from 'trezor-connect'; import { DEVICE, BLOCKCHAIN } from 'trezor-connect';
import { LOCATION_CHANGE } from 'connected-react-router'; import { LOCATION_CHANGE } from 'connected-react-router';
import * as WALLET from 'actions/constants/wallet'; import * as WALLET from 'actions/constants/wallet';
import * as CONNECT from 'actions/constants/TrezorConnect'; import * as CONNECT from 'actions/constants/TrezorConnect';
@ -132,6 +132,12 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
api.dispatch(DiscoveryActions.stop()); api.dispatch(DiscoveryActions.stop());
} }
// try to restore discovery on BLOCKCHAIN.CONNECT event
// edge case when backend throws error during discovery
if (action.type === BLOCKCHAIN.CONNECT) {
api.dispatch(DiscoveryActions.restore());
}
return action; return action;
}; };

Loading…
Cancel
Save