Cancel Requests on Wallet Switch (#873)

* add cancellation to nonce

* add cancellation to account/token balance updates

* update tests
This commit is contained in:
Danny Skubak 2018-01-21 13:23:09 -05:00 committed by Daniel Ternyak
parent ab5fa1a799
commit 2d6dbbcb18
4 changed files with 58 additions and 10 deletions

View File

@ -1,6 +1,6 @@
import { getNonceSucceeded, getNonceFailed, TypeKeys as TK, inputNonce } from 'actions/transaction';
import { SagaIterator } from 'redux-saga';
import { apply, put, select, takeEvery } from 'redux-saga/effects';
import { apply, put, select, takeEvery, fork, cancel, take } from 'redux-saga/effects';
import { INode } from 'libs/nodes/INode';
import { AppState } from 'reducers';
import { getNodeLib, getOffline } from 'selectors/config';
@ -33,5 +33,15 @@ export function* handleNonceRequest(): SagaIterator {
}
}
export function* handleNonceRequestWrapper(): SagaIterator {
const nonceRequest = yield fork(handleNonceRequest);
yield take(WalletTK.WALLET_SET);
yield cancel(nonceRequest);
}
//leave get nonce requested for nonce refresh later on
export const nonce = takeEvery([TK.GET_NONCE_REQUESTED, WalletTK.WALLET_SET], handleNonceRequest);
export const nonce = takeEvery(
[TK.GET_NONCE_REQUESTED, WalletTK.WALLET_SET],
handleNonceRequestWrapper
);

View File

@ -163,8 +163,12 @@ export function* handleSetWalletTokens(action: SetWalletTokensAction): SagaItera
}
export function* updateBalances(): SagaIterator {
yield fork(updateAccountBalance);
yield fork(updateTokenBalances);
const updateAccount = yield fork(updateAccountBalance);
const updateToken = yield fork(updateTokenBalances);
yield take(TypeKeys.WALLET_SET);
yield cancel(updateAccount);
yield cancel(updateToken);
}
export function* handleNewWallet(): SagaIterator {

View File

@ -1,10 +1,11 @@
import { getNonceSucceeded, getNonceFailed, inputNonce } from 'actions/transaction';
import { apply, put, select } from 'redux-saga/effects';
import { apply, put, select, fork, take, cancel } from 'redux-saga/effects';
import { getNodeLib, getOffline } from 'selectors/config';
import { getWalletInst } from 'selectors/wallet';
import { showNotification } from 'actions/notifications';
import { handleNonceRequest } from 'sagas/transaction/network/nonce';
import { cloneableGenerator } from 'redux-saga/utils';
import { handleNonceRequest, handleNonceRequestWrapper } from 'sagas/transaction/network/nonce';
import { cloneableGenerator, createMockTask } from 'redux-saga/utils';
import { TypeKeys as WalletTK } from 'actions/wallet';
import { Nonce } from 'libs/units';
describe('handleNonceRequest*', () => {
@ -77,3 +78,24 @@ describe('handleNonceRequest*', () => {
expect(gens.gen.next().value).toEqual(put(getNonceSucceeded(retrievedNonce)));
});
});
describe('handleNonceRequestWrapper*', () => {
const gen = handleNonceRequestWrapper();
const nonceRequest = createMockTask();
it('should fork handleNonceRequest', () => {
expect(gen.next().value).toEqual(fork(handleNonceRequest));
});
it('should take on WALLET_SET', () => {
expect(gen.next(nonceRequest).value).toEqual(take(WalletTK.WALLET_SET));
});
it('should cancel nonceRequest', () => {
expect(gen.next().value).toEqual(cancel(nonceRequest));
});
it('should be done', () => {
expect(gen.next().done).toEqual(true);
});
});

View File

@ -8,13 +8,14 @@ import {
unlockMnemonic as unlockMnemonicActionGen,
setTokenBalancesFulfilled,
setTokenBalancesPending,
setTokenBalancesRejected
setTokenBalancesRejected,
TypeKeys
} from 'actions/wallet';
import { Wei } from 'libs/units';
import { changeNodeIntent, web3UnsetNode } from 'actions/config';
import { INode } from 'libs/nodes/INode';
import { initWeb3Node, Token, N_FACTOR } from 'config';
import { apply, call, fork, put, select, take } from 'redux-saga/effects';
import { apply, call, fork, put, select, take, cancel } from 'redux-saga/effects';
import { getNodeLib, getOffline } from 'selectors/config';
import { getWalletInst, getWalletConfigTokens } from 'selectors/wallet';
import {
@ -190,13 +191,24 @@ describe('updateTokenBalances*', () => {
describe('updateBalances*', () => {
const gen = updateBalances();
const updateAccount = createMockTask();
const updateToken = createMockTask();
it('should fork updateAccountBalance', () => {
expect(gen.next().value).toEqual(fork(updateAccountBalance));
});
it('should fork updateTokenBalances', () => {
expect(gen.next().value).toEqual(fork(updateTokenBalances));
expect(gen.next(updateAccount).value).toEqual(fork(updateTokenBalances));
});
it('should take on WALLET_SET', () => {
expect(gen.next(updateToken).value).toEqual(take(TypeKeys.WALLET_SET));
});
it('should cancel updates', () => {
expect(gen.next().value).toEqual(cancel(updateAccount));
expect(gen.next().value).toEqual(cancel(updateToken));
});
it('should be done', () => {