Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop passing active peer to followed actions - Closes #1144 #1186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/actions/followedAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export const followedAccountsRetrieved = accounts => ({
type: actionTypes.followedAccountsRetrieved,
});

export const followedAccountFetchedAndUpdated = ({ activePeer, account }) =>
(dispatch) => {
export const followedAccountFetchedAndUpdated = ({ account }) =>
(dispatch, getState) => {
const activePeer = getState().peers.data;
getAccount(activePeer, account.address).then((result) => {
if (result.balance !== account.balance) {
account.balance = result.balance;
Expand Down
7 changes: 5 additions & 2 deletions src/actions/followedAccounts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('actions: followedAccount', () => {
balance: accounts.genesis.balance,
title: accounts.genesis.address,
};
const getState = () => ({
peers: { data: {} },
});

it('should create an action to retrieve the followed accounts list', () => {
const expectedAction = {
Expand Down Expand Up @@ -57,13 +60,13 @@ describe('actions: followedAccount', () => {
// Case 1: balance does not change
accountApi.getAccount.resolves({ balance: accounts.genesis.balance });

followedAccountFetchedAndUpdated({ activePeer: {}, account: data })(dispatch);
followedAccountFetchedAndUpdated({ account: data })(dispatch, getState);
expect(dispatch).to.not.have.been.calledWith();

// Case 2: balance does change
accountApi.getAccount.resolves({ balance: 0 });

followedAccountFetchedAndUpdated({ activePeer: {}, account: data })(dispatch);
followedAccountFetchedAndUpdated({ account: data })(dispatch, getState);
expect(dispatch).to.been.calledWith(followedAccountUpdated({
publicKey: accounts.genesis.publicKey,
balance: 0,
Expand Down
13 changes: 5 additions & 8 deletions src/store/middlewares/followedAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import actionTypes from '../../constants/actions';
import { followedAccountFetchedAndUpdated } from '../../actions/followedAccounts';

const followedAccountsMiddleware = (store) => {
const updateFollowedAccounts = (peers, accounts) => {
const updateFollowedAccounts = (accounts) => {
accounts.forEach((account) => {
store.dispatch(followedAccountFetchedAndUpdated({
activePeer: peers.data,
account,
}));
});
};

const checkTransactionsAndUpdateFollowedAccounts = (peers, tx, followedAccounts) => {
const checkTransactionsAndUpdateFollowedAccounts = (tx, followedAccounts) => {
const changedAccounts = followedAccounts.accounts.filter((account) => {
const relevantTransactions = tx.filter((transaction) => {
const { senderId, recipientId } = transaction;
Expand All @@ -21,28 +20,26 @@ const followedAccountsMiddleware = (store) => {
return relevantTransactions.length > 0;
});

updateFollowedAccounts(peers, changedAccounts);
updateFollowedAccounts(changedAccounts);
};

return next => (action) => {
next(action);
const { peers, followedAccounts } = store.getState();
const { followedAccounts } = store.getState();
switch (action.type) {
case actionTypes.newBlockCreated:
checkTransactionsAndUpdateFollowedAccounts(
peers,
action.data.block.transactions || [],
followedAccounts,
);
break;
case actionTypes.followedAccountAdded:
store.dispatch(followedAccountFetchedAndUpdated({
activePeer: peers.data,
account: action.data,
}));
break;
case actionTypes.activePeerSet:
updateFollowedAccounts(peers, followedAccounts.accounts);
updateFollowedAccounts(followedAccounts.accounts);
break;
default:
break;
Expand Down
1 change: 0 additions & 1 deletion src/store/middlewares/followedAccounts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('FollowedAccounts middleware', () => {
store = mock();
store.dispatch = spy();
store.getState = () => ({
peers: { data: {} },
followedAccounts: { accounts: [account, account2] },
});
next = spy();
Expand Down