Redux-saga停留在收益率调用上

时间:2023-01-15 18:58:31

I try to run the next code on node 6.2.1. It logs 1, 2, and then stuck. 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. Can anyone help me please?

我尝试在节点6.2.1上运行下一个代码。它记录1,2,然后卡住。我无法理解为什么它不继续执行到行收益率('TEST')...似乎anotherSaga完成并记录2但是控制权没有返回到rootSaga。有人可以帮我吗?

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);

1 个解决方案

#1


0  

It looks like this is due to the subscribe function not returning the correct value. Above it's returning the result of setInterval, which will be the ID of the interval. Instead it should be returning a function that redux-saga can use to unsubscribe to events, as per the docs. So your subscribe function should look more like this:

看起来这是由于订阅功能没有返回正确的值。在它上面返回setInterval的结果,它将是间隔的ID。相反,它应该返回一个函数,redux-saga可以用来取消订阅事件,根据文档。所以你的订阅功能看起来应该更像这样:

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.

奇怪的是,这导致你的传奇卡住了,但是我假设没有从订阅中获得取消订阅功能导致内容在redux-saga内部变得混乱。

#1


0  

It looks like this is due to the subscribe function not returning the correct value. Above it's returning the result of setInterval, which will be the ID of the interval. Instead it should be returning a function that redux-saga can use to unsubscribe to events, as per the docs. So your subscribe function should look more like this:

看起来这是由于订阅功能没有返回正确的值。在它上面返回setInterval的结果,它将是间隔的ID。相反,它应该返回一个函数,redux-saga可以用来取消订阅事件,根据文档。所以你的订阅功能看起来应该更像这样:

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.

奇怪的是,这导致你的传奇卡住了,但是我假设没有从订阅中获得取消订阅功能导致内容在redux-saga内部变得混乱。