From cdcbd7a56b1624d07ed0329af93942331aa7005f Mon Sep 17 00:00:00 2001 From: Daniel Ternyak Date: Sat, 8 Jul 2017 22:51:14 -0500 Subject: [PATCH] Sagas: Adjust bity saga to allow for cancelled polling, and stop polling when part 2 of swap ends. --- .eslintrc.json | 1 + common/actions/swap.js | 9 ++++- common/actions/swapConstants.js | 1 + .../Tabs/Swap/components/receivingAddress.js | 4 +- common/containers/Tabs/Swap/index.js | 9 +++-- common/sagas/bity.js | 40 ++++++++++++++----- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7d23e68a..0ca86358 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -24,6 +24,7 @@ "no-underscore-dangle": 0, "no-console": 0, "no-unused-vars": 0, + "no-constant-condition": 0, "no-trailing-spaces": [1, { "skipBlankLines": true }], "no-unreachable": 1, "no-alert": 0, diff --git a/common/actions/swap.js b/common/actions/swap.js index 9e8198fc..fa891b12 100644 --- a/common/actions/swap.js +++ b/common/actions/swap.js @@ -8,7 +8,8 @@ import { SWAP_PART_TWO_COMPLETE, SWAP_DESTINATION_ADDRESS, SWAP_RESTART, - SWAP_LOAD_BITY_RATES + SWAP_LOAD_BITY_RATES, + SWAP_STOP_LOAD_BITY_RATES } from './swapConstants'; export const originKindSwap = value => { @@ -78,3 +79,9 @@ export const loadBityRates = () => { type: SWAP_LOAD_BITY_RATES }; }; + +export const stopLoadBityRates = () => { + return { + type: SWAP_STOP_LOAD_BITY_RATES + }; +}; diff --git a/common/actions/swapConstants.js b/common/actions/swapConstants.js index c64741c9..fda3a5d7 100644 --- a/common/actions/swapConstants.js +++ b/common/actions/swapConstants.js @@ -8,3 +8,4 @@ export const SWAP_PART_TWO_COMPLETE = 'SWAP_PART_TWO_COMPLETE'; export const SWAP_DESTINATION_ADDRESS = 'SWAP_DESTINATION_ADDRESS'; export const SWAP_RESTART = 'SWAP_RESTART'; export const SWAP_LOAD_BITY_RATES = 'SWAP_LOAD_BITY_RATES'; +export const SWAP_STOP_LOAD_BITY_RATES = 'SWAP_STOP_LOAD_BITY_RATES'; diff --git a/common/containers/Tabs/Swap/components/receivingAddress.js b/common/containers/Tabs/Swap/components/receivingAddress.js index 882f8fb6..c1599862 100644 --- a/common/containers/Tabs/Swap/components/receivingAddress.js +++ b/common/containers/Tabs/Swap/components/receivingAddress.js @@ -10,7 +10,8 @@ export default class ReceivingAddress extends Component { destinationKind: PropTypes.string.isRequired, destinationAddressSwap: PropTypes.func.isRequired, destinationAddress: PropTypes.string, - partTwoCompleteSwap: PropTypes.func + partTwoCompleteSwap: PropTypes.func, + stopLoadBityRates: PropTypes.func }; onChangeDestinationAddress = (event: SyntheticInputEvent) => { @@ -19,6 +20,7 @@ export default class ReceivingAddress extends Component { }; onClickPartTwoComplete = () => { + this.props.stopLoadBityRates(); this.props.partTwoCompleteSwap(true); }; diff --git a/common/containers/Tabs/Swap/index.js b/common/containers/Tabs/Swap/index.js index 6eb756bc..bea2c188 100644 --- a/common/containers/Tabs/Swap/index.js +++ b/common/containers/Tabs/Swap/index.js @@ -33,7 +33,8 @@ class Swap extends Component { destinationAddressSwap: PropTypes.func, restartSwap: PropTypes.func, partTwoCompleteSwap: PropTypes.func, - partTwoComplete: PropTypes.bool + partTwoComplete: PropTypes.bool, + stopLoadBityRates: PropTypes.func }; componentDidMount() { @@ -59,7 +60,8 @@ class Swap extends Component { destinationAddress, restartSwap, partTwoCompleteSwap, - partTwoComplete + partTwoComplete, + stopLoadBityRates } = this.props; let wantToSwapMyProps = { @@ -88,7 +90,8 @@ class Swap extends Component { destinationKind, destinationAddressSwap, destinationAddress, - partTwoCompleteSwap + partTwoCompleteSwap, + stopLoadBityRates }; const referenceNumber = '2341asdfads'; diff --git a/common/sagas/bity.js b/common/sagas/bity.js index 166b48da..92c73458 100644 --- a/common/sagas/bity.js +++ b/common/sagas/bity.js @@ -1,23 +1,45 @@ // @flow -import { takeLatest, call, apply, put, select, fork } from 'redux-saga/effects'; +import { call, put, fork, take, cancel, cancelled } from 'redux-saga/effects'; + import type { Effect } from 'redux-saga/effects'; import { delay } from 'redux-saga'; import { updateBityRatesSwap } from 'actions/swap'; -import { SWAP_LOAD_BITY_RATES } from 'actions/swapConstants'; +import { + SWAP_LOAD_BITY_RATES, + SWAP_STOP_LOAD_BITY_RATES +} from 'actions/swapConstants'; import type { UnlockPrivateKeyAction } from 'actions/wallet'; import { getAllRates } from 'api/bity'; export function* loadBityRates(action?: any): Generator { - if (!action) return; - // eslint-disable-next-line - while (true) { - const data = yield call(getAllRates); - yield put(updateBityRatesSwap(data)); - yield call(delay, 5000); + try { + while (true) { + // TODO - yield put(actions.requestStart()) if we want to display swap refresh status + // network request + const data = yield call(getAllRates); + // action + yield put(updateBityRatesSwap(data)); + // wait 5 seconds before refreshing rates + yield call(delay, 5000); + } + } finally { + if (yield cancelled()) { + // TODO - implement request cancel if needed + // yield put(actions.requestFailure('Request cancelled!')) + } } } export default function* bitySaga(): Generator { - yield takeLatest(SWAP_LOAD_BITY_RATES, loadBityRates); + while (yield take(SWAP_LOAD_BITY_RATES)) { + // starts the task in the background + const loadBityRatesTask = yield fork(loadBityRates); + + // wait for the user to get to point where refresh is no longer needed + yield take(SWAP_STOP_LOAD_BITY_RATES); + // cancel the background task + // this will cause the forked loadBityRates task to jump into its finally block + yield cancel(loadBityRatesTask); + } }