Get web3 working + cleanup sagas

This commit is contained in:
HenryNguyen5 2018-02-02 17:34:38 -05:00
parent 35646af573
commit f999272769
7 changed files with 58 additions and 50 deletions

View File

@ -2,7 +2,6 @@ import React from 'react';
import classnames from 'classnames';
import Modal, { IButton } from 'components/ui/Modal';
import translate from 'translations';
import { makeCustomNetworkId } from 'utils/network';
import { CustomNetworkConfig } from 'types/network';
import { CustomNodeConfig } from 'types/node';
import { TAddCustomNetwork, addCustomNetwork, AddCustomNodeAction } from 'actions/config';
@ -131,14 +130,11 @@ class CustomNodeModal extends React.Component<Props, State> {
{net}
</option>
))}
{Object.values(customNetworks).map(net => {
const id = makeCustomNetworkId(net);
return (
<option key={id} value={id}>
{net.name} (Custom)
</option>
);
})}
{Object.entries(customNetworks).map(([id, net]) => (
<option key={id} value={id}>
{net.name} (Custom)
</option>
))}
<option value={CUSTOM}>Custom...</option>
</select>
</div>
@ -336,8 +332,11 @@ class CustomNodeModal extends React.Component<Props, State> {
private makeCustomNodeConfigFromState(): CustomNodeConfig {
const { network } = this.state;
const networkId =
network === CUSTOM ? makeCustomNetworkId(this.makeCustomNetworkConfigFromState()) : network;
network === CUSTOM
? this.makeCustomNetworkId(this.makeCustomNetworkConfigFromState())
: network;
const port = parseInt(this.state.port, 10);
const url = this.state.url.trim();
@ -392,6 +391,10 @@ class CustomNodeModal extends React.Component<Props, State> {
this.props.addCustomNode({ config: node, id: node.id });
};
private makeCustomNetworkId(config: CustomNetworkConfig): string {
return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`;
}
}
const mapStateToProps = (state: AppState): StateProps => ({

View File

@ -0,0 +1,9 @@
import { network } from './network';
import { node } from './node';
import { web3 } from './web3';
import { all } from 'redux-saga/effects';
import { SagaIterator } from 'redux-saga';
export default function*(): SagaIterator {
yield all([...network, ...node, ...web3]);
}

View File

@ -0,0 +1,23 @@
import { select, takeEvery, put } from 'redux-saga/effects';
import { getCustomNodeConfigs, getCustomNetworkConfigs } from 'selectors/config';
import { removeCustomNetwork, TypeKeys } from 'actions/config';
import { SagaIterator } from 'redux-saga';
import { AppState } from 'reducers';
// If there are any orphaned custom networks, purge them
export function* cleanCustomNetworks(): SagaIterator {
const customNodes: AppState['config']['nodes']['customNodes'] = yield select(
getCustomNodeConfigs
);
const customNetworks: AppState['config']['networks']['customNetworks'] = yield select(
getCustomNetworkConfigs
);
Object.values(customNodes).forEach(function*(n) {
if (!customNetworks[n.network]) {
yield put(removeCustomNetwork({ id: n.network }));
}
});
}
export const network = [takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks)];

View File

@ -5,18 +5,15 @@ import {
fork,
put,
take,
takeLatest,
takeEvery,
select,
race,
apply
apply,
takeLatest
} from 'redux-saga/effects';
import { makeCustomNetworkId } from 'utils/network';
import {
getNodeId,
getNodeConfig,
getCustomNodeConfigs,
getCustomNetworkConfigs,
getOffline,
isStaticNodeId,
getCustomNodeFromId,
@ -29,7 +26,6 @@ import {
changeNode,
changeNodeIntent,
setLatestBlock,
removeCustomNetwork,
AddCustomNodeAction,
ChangeNodeIntentAction
} from 'actions/config';
@ -42,7 +38,7 @@ import { Web3Service } from 'libs/nodes/web3';
let hasCheckedOnline = false;
export function* pollOfflineStatus(): SagaIterator {
while (true) {
const node: StaticNodeConfig = yield select(getNodeConfig);
const nodeConfig: StaticNodeConfig = yield select(getNodeConfig);
const isOffline: boolean = yield select(getOffline);
// If our offline state disagrees with the browser, run a check
@ -50,7 +46,7 @@ export function* pollOfflineStatus(): SagaIterator {
const shouldPing = !hasCheckedOnline || navigator.onLine === isOffline;
if (shouldPing && !document.hidden) {
const { pingSucceeded } = yield race({
pingSucceeded: call(node.lib.ping.bind(node.lib)),
pingSucceeded: call(nodeConfig.lib.ping.bind(nodeConfig.lib)),
timeout: call(delay, 5000)
});
@ -190,26 +186,9 @@ export function* switchToNewNode(action: AddCustomNodeAction): SagaIterator {
yield put(changeNodeIntent(action.payload.id));
}
// If there are any orphaned custom networks, purge them
export function* cleanCustomNetworks(): SagaIterator {
const customNodes = yield select(getCustomNodeConfigs);
const customNetworks = yield select(getCustomNetworkConfigs);
const networksInUse = customNodes.reduce((prev, conf) => {
prev[conf.network] = true;
return prev;
}, {});
for (const net of customNetworks) {
if (!networksInUse[makeCustomNetworkId(net)]) {
yield put(removeCustomNetwork(net));
}
}
}
export default function* configSaga(): SagaIterator {
yield takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus);
yield takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent);
yield takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload);
yield takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode);
yield takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks);
}
export const node = [
takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent),
takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus),
takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload),
takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode)
];

View File

@ -6,7 +6,7 @@ import { select, put, takeEvery, call } from 'redux-saga/effects';
import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config';
import { getNodeId, getStaticAltNodeToWeb3 } from 'selectors/config';
import { setupWeb3Node, Web3Service } from 'libs/nodes/web3';
import { Web3NodeConfig } from '../../shared/types/node';
import { Web3NodeConfig } from 'types/node';
export function* initWeb3Node(): SagaIterator {
const { networkId, lib } = yield call(setupWeb3Node);
@ -50,7 +50,6 @@ export function* unsetWeb3Node(): SagaIterator {
}
export const web3 = [
takeEvery(TypeKeys.CONFIG_NODE_WEB3_SET, initWeb3Node),
takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node),
takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent),
takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent)

View File

@ -52,7 +52,7 @@ import { loadWalletConfig, saveWalletConfig } from 'utils/localStorage';
import { getTokenBalances, filterScannedTokenBalances } from './helpers';
import { Token } from 'types/network';
import { Web3NodeConfig } from '../../../shared/types/node';
import { initWeb3Node } from 'sagas/web3';
import { initWeb3Node } from 'sagas/config/web3';
export interface TokenBalanceLookup {
[symbol: string]: TokenBalance;
@ -264,7 +264,7 @@ export function* unlockWeb3(): SagaIterator {
yield put(changeNodeIntent('web3'));
yield take(
action =>
action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeSelection === 'web3'
action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeId === 'web3'
);
const web3Node: Web3NodeConfig | null = yield select(getWeb3Node);

View File

@ -1,5 +0,0 @@
import { CustomNetworkConfig } from 'types/network';
export function makeCustomNetworkId(config: CustomNetworkConfig): string {
return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`;
}