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. javascript实现查看html网页放大图片功能
  2. 结合悬停和点击功能(jQuery)?
  3. 整理简易分享功能
  4. arcgis api for js入门开发系列十 自定义Navigation控件样式风格
  5. ArcGIS API For JavaScript 实现鼠标移入graphic提醒功能
  6. 五十行javascript代码实现简单的双向数据绑定
  7. 在内容可编辑DIV中的选定文本周围包装bb代码
  8. 常用验证JS代码基础及实例
  9. 试着在我的javascript代码中理解“this”(一件事有效,另一件没有)

随机推荐

  1. android:如何从照片中获取拍摄地址信息
  2. Android之Activities
  3. Android富文本编辑器附源码
  4. Android(安卓)Studio Gradle 增加对.so
  5. Android(安卓)Audio - 支持多个APK同时录
  6. Android工具HierarchyViewer 代码导读
  7. Android流量抓包工具--PacketCapture
  8. 开发Android第三步,签名证书,真机安装编译
  9. Mac下配置Android(安卓)Stuido的HTTP Pro
  10. Android(安卓)WebView优化