2017-07-08 02:00:09 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-07-09 03:51:14 +00:00
|
|
|
import { call, put, fork, take, cancel, cancelled } from 'redux-saga/effects';
|
|
|
|
|
2017-07-08 02:00:09 +00:00
|
|
|
import type { Effect } from 'redux-saga/effects';
|
|
|
|
import { delay } from 'redux-saga';
|
|
|
|
import { updateBityRatesSwap } from 'actions/swap';
|
2017-07-09 03:51:14 +00:00
|
|
|
import {
|
|
|
|
SWAP_LOAD_BITY_RATES,
|
|
|
|
SWAP_STOP_LOAD_BITY_RATES
|
|
|
|
} from 'actions/swapConstants';
|
2017-07-08 02:00:09 +00:00
|
|
|
import type { UnlockPrivateKeyAction } from 'actions/wallet';
|
|
|
|
import { getAllRates } from 'api/bity';
|
|
|
|
|
|
|
|
export function* loadBityRates(action?: any): Generator<Effect, void, any> {
|
2017-07-09 03:51:14 +00:00
|
|
|
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!'))
|
|
|
|
}
|
2017-07-08 02:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function* bitySaga(): Generator<Effect, void, any> {
|
2017-07-09 03:51:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-07-08 02:00:09 +00:00
|
|
|
}
|