I have, a set of action grouped in redux-saga
channel. Each of them (or many simultaneously) can fail because of token expiration.
How can I suspend future actions to prevent next failures, retry already catched action after token refresh and unsuspend channel.
function* watchRequests() {
// a set of simultaneous actions which utilize access token from store
const requestChannel = yield actionChannel('*_REQUEST')
while (true) {
try {
const { payload } = yield take(requestChannel)
yield fork(handleRequest, payload)
} catch (error) {
if (error === 'EXPIRED_ACCESS_TOKEN') {
const newToken = yield call(refreshToken)
yield call(setAccessToken, newToken)
} else {
throw error
}
} finally {
// after success token refresh
// 1. retry catched action
// 2. unsuspend rest actions from channel
}
}
}