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