I want create some single redux-saga background effect for all potential open browser tabs.
For example, after successful login i want start my refreshTokenFlow with delay(1000 * 60 * 2).
When the refreshTokenFlow is active, how to tell all other sagas to wait for the execution of this saga, and only after successfully updating the tokens continue the execution of all subsequent sagas.
Login saga
function * loginWatcher () {
while (true) {
const { Login, Pass } = yield take(LOGIN_REQUEST);
const task = yield fork(loginFlow, Login, Pass);
}
}
function * loginFlow (Login, Pass) {
let tokens;
try {
tokens = yield call(loginApi, Login, Pass);
yield put({ type: LOGIN_SUCCESS, payload: tokens });
} catch (error) {
yield put({ type: LOGIN_FAIL});
} finally {
if (yield cancelled()) {
createHistory.push('/')
}
}
return tokens;
}
async function loginApi (Login, Pass) {
try {
const data = await customAxios.post(`${process.env.REACT_APP_API_URL}/auth/authentication`, { Login, Pass });
return data.data;
}
catch (error) {
return error;
}
}
Other saga
export function * vehiclesWatcher () {
let getVehiclesTask;
const requestChan = yield actionChannel(GET_VEHICLES_REQUEST);
while (true) {
yield takeEvery(ADD_SETTINGS_REQUEST, function * selectWim(action) {
const {data} = action;
yield put({ type: ADD_SETTINGS_SUCCESS, payload: data });
createHistory.push("/vehicles")
});
yield take(requestChan);
getVehiclesTask = yield fork(vehiclesFlow);
yield take([CLEAR_VEHICLES_REQUEST]);
yield cancel(getVehiclesTask);
}
}
async function getAllVehicles (token, id, searchBy) {
try {
const res = await customAxios.post(`${API}/vehicle/getsorted?take=${15}`,
JSON.stringify({...searchBy, id }), configHeaders(token));
return res.data;
} catch (error) {
throw error;
}
}
function * vehiclesFlow () {
try {
while (true) {
const accessToken = yield select(getToken);
const searchBy = yield select(result);
const wimId = yield select(getWimId);
const vehicles = yield call(getAllVehicles, accessToken, wimId, searchBy);
yield put ({type: GET_VEHICLES_SUCCESS, payload: vehicles});
yield delay(5000);
}
} catch (error) {
} finally {
if (yield cancelled()) {
yield put({type: CLEAR_VEHICLES_SUCCESS});
}
}
}
Root Saga
export default function* IndexSaga () {
yield all([
loginWatcher(),
vehiclesWatcher(),
]);
}
Is it really possible to create such a saga that will pause all other sagas until it is completed?
Any examples, please.