use BlockchainActions.subscribe insteadof DiscoveryActions.reconnect

pull/615/head
Szymon Lesisz 5 years ago
parent 3d8f64465a
commit ce990f60c3

@ -2,7 +2,6 @@
import React from 'react'; import React from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import TrezorConnect, { UI } from 'trezor-connect'; import TrezorConnect, { UI } from 'trezor-connect';
import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain';
import * as DISCOVERY from 'actions/constants/discovery'; import * as DISCOVERY from 'actions/constants/discovery';
import * as ACCOUNT from 'actions/constants/account'; import * as ACCOUNT from 'actions/constants/account';
import * as NOTIFICATION from 'actions/constants/notification'; import * as NOTIFICATION from 'actions/constants/notification';
@ -10,7 +9,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
import type { import type {
ThunkAction, ThunkAction,
AsyncAction, AsyncAction,
PromiseAction,
PayloadAction, PayloadAction,
GetState, GetState,
Dispatch, Dispatch,
@ -331,30 +329,7 @@ const finish = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction
}); });
}; };
export const reconnect = (network: string, timeout: number = 30): PromiseAction<void> => async ( // Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE or BLOCKCHAIN.CONNECT actions in WalletService
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
// OR after BlockchainSubscribe in this.reconnect // OR after BlockchainSubscribe in this.reconnect
export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// check if current url has "network" parameter // check if current url has "network" parameter

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

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

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

Loading…
Cancel
Save