2017-07-08 06:00:09 +04:00
|
|
|
// @flow
|
|
|
|
import { delay } from 'redux-saga';
|
|
|
|
import { getAllRates } from 'api/bity';
|
2017-08-28 14:05:38 -04:00
|
|
|
import { call, put, fork, take, cancel, cancelled } from 'redux-saga/effects';
|
2017-07-31 18:14:30 -05:00
|
|
|
import type { Effect } from 'redux-saga/effects';
|
|
|
|
import { loadBityRatesSucceededSwap } from 'actions/swap';
|
2017-07-08 06:00:09 +04:00
|
|
|
|
2017-07-16 16:02:13 -05:00
|
|
|
export function* loadBityRates(_action?: any): Generator<Effect, void, any> {
|
2017-07-08 22:51:14 -05:00
|
|
|
try {
|
|
|
|
while (true) {
|
2017-07-31 18:14:30 -05:00
|
|
|
// TODO - BITY_RATE_REQUESTED
|
2017-07-08 22:51:14 -05:00
|
|
|
// network request
|
|
|
|
const data = yield call(getAllRates);
|
|
|
|
// action
|
2017-07-31 18:14:30 -05:00
|
|
|
yield put(loadBityRatesSucceededSwap(data));
|
2017-07-08 22:51:14 -05:00
|
|
|
// 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!'))
|
|
|
|
}
|
2017-07-08 06:00:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 18:14:30 -05:00
|
|
|
export function* getBityRatesSaga(): Generator<Effect, void, any> {
|
|
|
|
while (yield take('SWAP_LOAD_BITY_RATES_REQUESTED')) {
|
2017-07-08 22:51:14 -05:00
|
|
|
// 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
|
2017-07-27 13:05:09 -04:00
|
|
|
yield take('SWAP_STOP_LOAD_BITY_RATES');
|
2017-07-08 22:51:14 -05:00
|
|
|
// cancel the background task
|
|
|
|
// this will cause the forked loadBityRates task to jump into its finally block
|
|
|
|
yield cancel(loadBityRatesTask);
|
|
|
|
}
|
2017-07-08 06:00:09 +04:00
|
|
|
}
|