2017-08-31 10:30:46 -06:00
|
|
|
import { showNotification } from 'actions/notifications';
|
2017-09-24 19:06:28 -07:00
|
|
|
import { loadBityRatesSucceededSwap } from 'actions/swap';
|
2017-11-16 14:25:30 -06:00
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
2017-09-24 19:06:28 -07:00
|
|
|
import { getAllRates } from 'api/bity';
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
|
|
|
import { call, cancel, fork, put, take, takeLatest } from 'redux-saga/effects';
|
2017-07-08 06:00:09 +04:00
|
|
|
|
2017-11-16 14:25:30 -06:00
|
|
|
const POLLING_CYCLE = 30000;
|
|
|
|
|
2017-10-18 22:29:49 -04:00
|
|
|
export function* loadBityRates(): SagaIterator {
|
2017-08-31 10:30:46 -06:00
|
|
|
while (true) {
|
|
|
|
try {
|
2017-07-08 22:51:14 -05:00
|
|
|
const data = yield call(getAllRates);
|
2017-07-31 18:14:30 -05:00
|
|
|
yield put(loadBityRatesSucceededSwap(data));
|
2017-08-31 10:30:46 -06:00
|
|
|
} catch (error) {
|
2017-09-24 19:06:28 -07:00
|
|
|
yield put(showNotification('danger', error.message));
|
2017-07-08 22:51:14 -05:00
|
|
|
}
|
2017-11-16 14:25:30 -06:00
|
|
|
yield call(delay, POLLING_CYCLE);
|
2017-07-08 06:00:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:30:46 -06:00
|
|
|
// Fork our recurring API call, watch for the need to cancel.
|
2017-11-29 23:07:16 -05:00
|
|
|
export function* handleBityRates(): SagaIterator {
|
2017-08-31 10:30:46 -06:00
|
|
|
const loadBityRatesTask = yield fork(loadBityRates);
|
2017-11-16 14:25:30 -06:00
|
|
|
yield take(TypeKeys.SWAP_STOP_LOAD_BITY_RATES);
|
2017-08-31 10:30:46 -06:00
|
|
|
yield cancel(loadBityRatesTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for latest SWAP_LOAD_BITY_RATES_REQUESTED action.
|
2017-09-24 19:06:28 -07:00
|
|
|
export function* getBityRatesSaga(): SagaIterator {
|
2017-11-16 14:25:30 -06:00
|
|
|
yield takeLatest(TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED, handleBityRates);
|
2017-07-08 06:00:09 +04:00
|
|
|
}
|