2018-02-12 20:43:07 +00:00
|
|
|
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, prune them
|
|
|
|
export function* pruneCustomNetworks(): SagaIterator {
|
|
|
|
const customNodes: AppState['config']['nodes']['customNodes'] = yield select(
|
|
|
|
getCustomNodeConfigs
|
|
|
|
);
|
|
|
|
const customNetworks: AppState['config']['networks']['customNetworks'] = yield select(
|
|
|
|
getCustomNetworkConfigs
|
|
|
|
);
|
|
|
|
|
|
|
|
//construct lookup table of networks
|
2018-03-07 23:36:05 +00:00
|
|
|
const linkedNetworks: { [key: string]: boolean } = Object.values(customNodes).reduce(
|
2018-02-12 20:43:07 +00:00
|
|
|
(networkMap, currentNode) => ({ ...networkMap, [currentNode.network]: true }),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2018-05-29 14:51:42 +00:00
|
|
|
for (const customNetwork of Object.values(customNetworks)) {
|
|
|
|
if (!linkedNetworks[customNetwork.id]) {
|
|
|
|
yield put(removeCustomNetwork(customNetwork.id));
|
2018-02-12 20:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const network = [takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, pruneCustomNetworks)];
|