[英]How I deal with multiple actions calling different functions in redux saga?
Here an example which is going to be easier to understand. 这是一个更容易理解的示例。
function* a() { yield takeEvery(action.LOGIN_SUCCESS, b) } function* b() { yield takeEvery(action.FETCH_SUCCESS, c) } function* c() { yield console.log('action from function a') } function* d() { yield takeEvery(action.FETCH_SUCCESS, e) } function* e() { yield console.log('action from function d') } yield all([ fork(a), fork(d), ])
If LOGIN_SUCCESS happens then function b starts waiting for FETCH_SUCCESS. 如果发生LOGIN_SUCCESS,则函数b开始等待FETCH_SUCCESS。 At the same time, function d is waiting for FETCH_SUCCESS. 同时,函数d正在等待FETCH_SUCCESS。
The question is: how can I stop function d from running after the takeEvery in function a takes the LOGIN_SUCCESS and then FETCH_SUCCESS action? 问题是:在函数a中的takeEvery采取LOGIN_SUCCESS然后执行FETCH_SUCCESS操作之后,如何停止函数d的运行?
In this case I want only function c to be called. 在这种情况下,我只希望调用函数c。 Instead, what happens is that both function c and function e are called because FETCH_SUCCESS happens. 相反,发生的情况是函数c和函数e都被调用,因为发生了FETCH_SUCCESS。
The idea is that I would like to have something in redux saga which remembers the previous action taken by the previous generator so that I can distinguish two different paths: 我的想法是,我想在redux传奇中添加一些东西,以记住上一个生成器采取的先前操作,以便我可以区分两个不同的路径:
1) LOGIN_SUCCESS -> FETCH_SUCCESS -> function c
2) FETCH_SUCCESS -> function e
Hope I have been clear enough :) 希望我已经足够清楚了:)
How about modifying saga d
like so: 如何像这样修改saga d
:
function* d() {
const task = yield takeEvery(action.FETCH_SUCCESS, e)
yield take(action.LOGIN_SUCCESS)
yield cancel(task)
}
This will kill the takeEvery in saga d
once LOGIN_SUCCESS
is dispatched. 这将杀死takeEvery佐贺d
一旦LOGIN_SUCCESS
分派。
Since you are using takeEvery
for LOGIN_SUCCESS
I assume it is something that can happen multiple times. 由于您将takeEvery
用于LOGIN_SUCCESS
我认为这可能会发生多次。 In that case you will probably want to run another saga d
on logout. 在这种情况下,你可能会想运行其他传奇d
上登出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.