2017-10-11 05:04:49 +00:00
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
|
|
|
import {
|
|
|
|
call,
|
|
|
|
cancel,
|
|
|
|
fork,
|
|
|
|
put,
|
|
|
|
take,
|
|
|
|
takeLatest,
|
|
|
|
takeEvery,
|
|
|
|
select
|
|
|
|
} from 'redux-saga/effects';
|
2017-10-04 04:37:06 +00:00
|
|
|
import { NODES } from 'config/data';
|
2017-11-10 03:30:20 +00:00
|
|
|
import { Web3Wallet } from 'libs/wallet';
|
|
|
|
import { getNode, getNodeConfig } from 'selectors/config';
|
|
|
|
import { getWalletInst } from 'selectors/wallet';
|
2017-10-11 05:04:49 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import { TypeKeys } from 'actions/config/constants';
|
2017-11-16 18:16:09 +00:00
|
|
|
import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants';
|
2017-11-10 03:30:20 +00:00
|
|
|
import {
|
|
|
|
toggleOfflineConfig,
|
|
|
|
changeNode,
|
|
|
|
changeNodeIntent
|
|
|
|
} from 'actions/config';
|
2017-11-12 19:45:52 +00:00
|
|
|
import {
|
|
|
|
State as ConfigState,
|
|
|
|
INITIAL_STATE as configInitialState
|
|
|
|
} from 'reducers/config';
|
2017-10-11 05:04:49 +00:00
|
|
|
|
|
|
|
export const getConfig = (state: AppState): ConfigState => state.config;
|
|
|
|
|
|
|
|
export function* pollOfflineStatus(): SagaIterator {
|
|
|
|
while (true) {
|
|
|
|
const offline = !navigator.onLine;
|
|
|
|
const config = yield select(getConfig);
|
|
|
|
const offlineState = config.offline;
|
|
|
|
if (offline !== offlineState) {
|
|
|
|
yield put(toggleOfflineConfig());
|
|
|
|
}
|
|
|
|
yield call(delay, 250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork our recurring API call, watch for the need to cancel.
|
|
|
|
function* handlePollOfflineStatus(): SagaIterator {
|
|
|
|
const pollOfflineStatusTask = yield fork(pollOfflineStatus);
|
|
|
|
yield take('CONFIG_STOP_POLL_OFFLINE_STATE');
|
|
|
|
yield cancel(pollOfflineStatusTask);
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:05:38 +00:00
|
|
|
// @HACK For now we reload the app when doing a language swap to force non-connected
|
|
|
|
// data to reload. Also the use of timeout to avoid using additional actions for now.
|
2017-09-25 02:06:28 +00:00
|
|
|
function* reload(): SagaIterator {
|
|
|
|
setTimeout(() => location.reload(), 250);
|
2017-08-28 18:05:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 04:37:06 +00:00
|
|
|
function* handleNodeChangeIntent(action): SagaIterator {
|
|
|
|
const nodeConfig = yield select(getNodeConfig);
|
|
|
|
const currentNetwork = nodeConfig.network;
|
|
|
|
const actionNetwork = NODES[action.payload].network;
|
2017-11-10 03:30:20 +00:00
|
|
|
const currentWallet = yield select(getWalletInst);
|
|
|
|
|
2017-10-04 04:37:06 +00:00
|
|
|
yield put(changeNode(action.payload));
|
2017-11-10 03:30:20 +00:00
|
|
|
|
|
|
|
// if there's no wallet, do not reload as there's no component state to resync
|
|
|
|
if (currentWallet && currentNetwork !== actionNetwork) {
|
2017-10-04 04:37:06 +00:00
|
|
|
yield call(reload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
// unset web3 as the selected node if a non-web3 wallet has been selected
|
|
|
|
function* unsetWeb3Node(action): SagaIterator {
|
|
|
|
const node = yield select(getNode);
|
|
|
|
const nodeConfig = yield select(getNodeConfig);
|
|
|
|
const newWallet = action.payload;
|
|
|
|
const isWeb3Wallet = newWallet instanceof Web3Wallet;
|
|
|
|
|
|
|
|
if (node !== 'web3' || isWeb3Wallet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch back to a node with the same network as MetaMask/Mist
|
|
|
|
const equivalentNode = Object.keys(NODES)
|
|
|
|
.filter(key => key !== 'web3')
|
|
|
|
.reduce((found, key) => {
|
|
|
|
const config = NODES[key];
|
|
|
|
if (found.length) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
if (nodeConfig.network === config.network) {
|
|
|
|
return (found = key);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}, '');
|
|
|
|
|
|
|
|
// if no equivalent node was found, use the app default
|
|
|
|
const newNode = equivalentNode.length
|
|
|
|
? equivalentNode
|
|
|
|
: configInitialState.nodeSelection;
|
|
|
|
|
|
|
|
yield put(changeNodeIntent(newNode));
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:04:49 +00:00
|
|
|
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);
|
2017-11-16 18:16:09 +00:00
|
|
|
yield takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3Node);
|
|
|
|
yield takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3Node);
|
2017-08-28 18:05:38 +00:00
|
|
|
}
|