1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-27 10:48:22 +00:00

use BlockchainActions.subscribe insteadof DiscoveryActions.reconnect

This commit is contained in:
Szymon Lesisz 2019-10-30 16:27:17 +01:00
parent 3d8f64465a
commit ce990f60c3
4 changed files with 15 additions and 32 deletions

View File

@ -2,7 +2,6 @@
import React from 'react';
import { FormattedMessage } from 'react-intl';
import TrezorConnect, { UI } from 'trezor-connect';
import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain';
import * as DISCOVERY from 'actions/constants/discovery';
import * as ACCOUNT from 'actions/constants/account';
import * as NOTIFICATION from 'actions/constants/notification';
@ -10,7 +9,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
import type {
ThunkAction,
AsyncAction,
PromiseAction,
PayloadAction,
GetState,
Dispatch,
@ -331,30 +329,7 @@ const finish = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction
});
};
export const reconnect = (network: string, timeout: number = 30): PromiseAction<void> => async (
dispatch: Dispatch
): Promise<void> => {
// Runs two promises.
// First promise is a subscribe action which will never resolve in case of completely lost connection to the backend
// That's why there is a second promise that rejects after the specified timeout.
return Promise.race([
dispatch(BlockchainActions.subscribe(network)),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)),
])
.catch(() => {
// catch error from first promises that rejects (most likely timeout)
dispatch({
type: BLOCKCHAIN_ACTION.FAIL_SUBSCRIBE,
shortcut: network,
});
})
.then(() => {
// dispatch restore when subscribe promise resolves
dispatch(restore());
});
};
// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE actions in WalletService
// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE or BLOCKCHAIN.CONNECT actions in WalletService
// OR after BlockchainSubscribe in this.reconnect
export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// check if current url has "network" parameter

View File

@ -14,17 +14,21 @@ export default (props: Props) => {
if (notification.type === 'backend') {
// special case: backend is down
// TODO: this is a different component with "auto resolve" button
const inProgress = blockchain && blockchain.connecting;
const status = inProgress
? l10nMessages.TR_RECONNECTING
: l10nMessages.TR_CONNECT_TO_BACKEND;
return (
<Notification
variant="error"
title={notification.title}
message={notification.message}
isActionInProgress={blockchain && blockchain.connecting}
isActionInProgress={inProgress}
actions={[
{
label: props.intl.formatMessage(l10nMessages.TR_CONNECT_TO_BACKEND),
label: props.intl.formatMessage(status),
callback: async () => {
await props.blockchainReconnect(network.shortcut);
if (!inProgress) props.blockchainReconnect(network.shortcut);
},
},
]}

View File

@ -7,6 +7,10 @@ const definedMessages: Messages = defineMessages({
id: 'TR_CONNECT_TO_BACKEND',
defaultMessage: 'Connect',
},
TR_RECONNECTING: {
id: 'TR_RECONNECTING',
defaultMessage: 'Reconnecting...',
},
});
export default definedMessages;

View File

@ -7,7 +7,7 @@ import type { IntlShape } from 'react-intl';
import type { State, Dispatch } from 'flowtype';
import { reconnect } from 'actions/DiscoveryActions';
import { subscribe } from 'actions/BlockchainActions';
import * as NotificationActions from 'actions/NotificationActions';
import StaticNotifications from './components/Static';
@ -29,7 +29,7 @@ export type StateProps = {|
export type DispatchProps = {|
close: typeof NotificationActions.close,
blockchainReconnect: typeof reconnect,
blockchainReconnect: typeof subscribe,
|};
export type Props = {| ...OwnProps, ...StateProps, ...DispatchProps |};
@ -52,7 +52,7 @@ const mapStateToProps = (state: State): StateProps => ({
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
close: bindActionCreators(NotificationActions.close, dispatch),
blockchainReconnect: bindActionCreators(reconnect, dispatch),
blockchainReconnect: bindActionCreators(subscribe, dispatch),
});
export default injectIntl<OwnProps>(