switching between two redux-saga flows -
in scenario user can redeem vouchers. while not logged in can still test redeem feature understand how works.
i have 2 flows, notloggedinredeemflow
case when not logged in , loggedinredeemflow
in case logged in.
i have on root saga, should start 1 flow , stop other based on users login state.
i came using race between 1 flow , cancellation
action flow.
on app startup notloggedinredeemflow
active , should end when user logs in.
then loggedinredeemflow
starts , runs until user logs out.
function* redeemflows() { while (true) { const result = yield race({ fake: call(notloggedinredeemflow), gotaccesstoken: take([accountactions.login_success, accountactions.start_signup_success]), }) yield race({ run: call(loggedinredeemflow, result.gotaccesstoken.payload.access_token), logout: take(accountactions.logout_success), }) } }
my questions if best way handle such scenario in redux-sagas?
if i'm understanding use case correctly, believe need redeem action fired ui , saga interprets whether logged in or not. if logged in, put redeem_logged_in action otherwise put redeem_not_logged_in action. , there sub-sagas handle redeem_logged_in , redeem_not_logged_in actions respectively.
import {takelatest} 'redux-saga'; export default function* rootsaga () { yield takelatest('redeem', handlegenericredeem); yield takelatest('redeem_not_logged_in', handleredeemnotloggedin); yield takelatest('redeem_logged_in', handleredeemloggedin); }
the exact action names , whether use takelatest , specific use case. might not need nor want takelatest, code snippet should give idea of how can solve problem 1 way.
Comments
Post a Comment