[英]Redux-saga is stuck on yield call
I try to run the next code on node 6.2.1. 我尝试在节点6.2.1上运行下一个代码。 It logs 1, 2, and then stuck.
它记录1、2,然后卡住。 I cannot undestand why it doesn't continue execution to the line yield take('TEST')... Seems that anotherSaga finishes and logs 2 but control isn't returned to rootSaga.
我无法理解为什么它不继续执行到代码行代码take('TEST')...似乎anotherSaga完成并记录了2,但是控件没有返回给rootSaga。 Can anyone help me please?
谁能帮我吗?
const {runSaga, delay} = require('redux-saga');
const {take, put, call} = require('redux-saga/effects');
function* anotherSaga() {
yield call(delay, 1000);
console.log(2);
}
function* rootSaga() {
while(true) {
console.log(1);
yield call(anotherSaga);
console.log(3);
const action = yield take('TEST');
console.log(4);
yield put(action);
}
}
runSaga(
rootSaga(),
{
subscribe(callback) {
return setInterval(() => (callback({type: 'TEST'})), 1000);
},
dispatch(action) {
console.log(action);
},
getState() {}
}
);
Update : but code without runSaga works as expected logging 1,2,3,4 更新 :但是没有runSaga的代码按预期的方式记录1,2,3,4
const {createStore, applyMiddleware} = require('redux');
const createSagaMiddleware = require('redux-saga').default;
const {delay} = require('redux-saga');
const {take, put, call} = require('redux-saga/effects');
function* anotherSaga() {
yield call(delay, 2000);
console.log(2);
}
function* rootSaga() {
while(true) {
console.log(1);
yield call(anotherSaga);
console.log(3);
const action = yield take('TEST');
console.log(4);
yield put(action);
console.log('---')
}
}
const rootReducer = (state, action) => {
if (state === undefined) {
return {};
}
return state;
}
const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, {}, applyMiddleware(sagaMiddleware));
sagaMiddleware.run(rootSaga);
setInterval(() => (store.dispatch({type: 'TEST'})), 1000);
It looks like this is due to the subscribe
function not returning the correct value. 看来这是由于
subscribe
函数未返回正确的值。 Above it's returning the result of setInterval
, which will be the ID of the interval. 在其上方,返回
setInterval
的结果,该结果将是间隔的ID。 Instead it should be returning a function that redux-saga can use to unsubscribe to events, as per the docs . 相反,根据docs ,它应该返回一个redux-saga可以用来取消订阅事件的函数。 So your
subscribe
function should look more like this: 因此,您的
subscribe
函数应如下所示:
subscribe(callback) {
const intervalId = setInterval(() => (callback({type: 'TEST'})), 1000);
return () => { clearInterval(intervalId); };
},
Running it with this, I was able to see it loop through printing 以此运行它,我可以看到它遍历打印
1
2
3
4
{ type: 'TEST' }
It's weird how this caused your saga to become stuck, but I'm assuming that not receiving an unsubscribe
function from subscribe
caused things to become messed up internally in redux-saga. 这很奇怪,这导致您的传奇陷入困境,但是我假设未收到来自
subscribe
的unsubscribe
功能导致事情在redux-saga中内部混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.