dva如何在effect的回调中执行呢,能否提供上下文注入呢

Created on 23 Jan 2017  ·  4Comments  ·  Source: dvajs/dva

updateChatStatus并没有执行

let callback = function*(){
        yield put({type: 'updateChatStatus', payload: {chat: mychat, status: 'sendfailed'}}); 
      }

      yield put({type: 'addMyChat', payload: {objectObserver: new objectObserver(mychat, callback), chat: mychat}}); 
question

Most helpful comment

好好学习一下sagas和promise吧。
https://redux-saga.github.io/redux-saga/docs/advanced/RacingEffects.html

import { race, take, put } from 'redux-saga/effects'
import { delay } from 'redux-saga'

function* fetchPostsWithTimeout() {
  const {posts, timeout} = yield race({
    posts: call(fetchApi, '/posts'),
    timeout: call(delay, 1000)
  })

  if (posts)
    put({type: 'POSTS_RECEIVED', posts})
  else
    put({type: 'TIMEOUT_ERROR'})
}

All 4 comments

为什么要在回调里调用 reducer 呢,直接调用不就好了,具体场景抛出来吧

做了一个聊天系统,发送一条信息后会等待服务端的返回结果,如果成功了就更新这条信息的状态,如果等了十秒还没有返回结果就算失败,需要重新发送。这个十秒后的操作就需要回调执行了

好好学习一下sagas和promise吧。
https://redux-saga.github.io/redux-saga/docs/advanced/RacingEffects.html

import { race, take, put } from 'redux-saga/effects'
import { delay } from 'redux-saga'

function* fetchPostsWithTimeout() {
  const {posts, timeout} = yield race({
    posts: call(fetchApi, '/posts'),
    timeout: call(delay, 1000)
  })

  if (posts)
    put({type: 'POSTS_RECEIVED', posts})
  else
    put({type: 'TIMEOUT_ERROR'})
}

多谢LS,这个和race还有点不一样,race返回最新成功的,因为我采用的是WS通信,所以timeout就执行不到了,已经使用下面这个方法解决了

sendMsg: [function*({ payload }, { put, call, select }) {
  // 设置状态
  yield put({type: 'delayUpdateChatStatus', payload: {chat: mychat, status: 'sendfailed'}});
}, { type: 'takeLatest' }],

delayUpdateChatStatus: [function*({ payload }, { put, call, select }) {

  yield call(delay, 15000);
  yield put({type: 'updateChatStatus', payload: payload});

}

最初的方法是使用onComplete回调实现的

Was this page helpful?
0 / 5 - 0 ratings