Merge pull request #37 from MyEtherWallet/bity_stop_refresh

Sagas: Adjust bity saga to allow for cancelled polling, and stop poll…
This commit is contained in:
Daniel Ternyak 2017-07-09 14:15:23 -05:00 committed by GitHub
commit 8092a5dec3
6 changed files with 50 additions and 14 deletions

View File

@ -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,

View File

@ -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
};
};

View File

@ -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';

View File

@ -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);
};

View File

@ -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';

View File

@ -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<Effect, void, any> {
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<Effect, void, any> {
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);
}
}