如何将Redux Saga转换为Observable

时间:2019-01-17 23:52:51

标签: redux-saga redux-observable

我必须深入研究Redux-Observable,而我的奋斗目标是转换Sagas

function* loadTabs(action) {
  const tabs = yield call(api.fetchTabs)
  yield put(actions.setTabs(tabs))
  const tabId = tabs[0].id
  yield put(actions.selectTab(tabId))
  yield put(actions.loadWidgets(tabId))
}

function* loadWidgets({ tabId }) {
  const widgets = yield call(api.fetchWidgets, tabId)
  yield put(actions.setWidgets(widgets))
  yield put(actions.setDashboardReady(true))
}

/* WATCHER */
function* mySaga() {
  yield takeEvery(types.TABS_REQUESTED, loadTabs)
  yield takeEvery(types.WIDGETS_REQUESTED, loadWidgets)
}

进入史诗。我所做的是

const loadTabs = (action$, state$) => action$.pipe(
  ofType(types.TABS_REQUESTED),
  mergeMap(() => api.fetchTabs()),
  map(tabs => actions.setTabs(tabs))
)

const loadWidgets = action$ => action$.pipe(
  ofType(types.WIDGETS_REQUESTED),
  mergeMap(() => api.fetchTabs()),
  map(widgets => actions.setWidgets(widgets)),
)

const rootEpic = combineEpics(loadTabs, loadWidgets)

“调度链”应为“ TABS_REQUESTED> setTabs> selectTab> loadWidgets> setDashboardReady”。我已经停了 'TABS_REQUESTED> setTabs'

请用遗漏的零件踢我。我现在手头上满是手册。谢谢。

1 个答案:

答案 0 :(得分:0)

这就像日食。停下来,考虑将流动作入/动作出,然后重试。.

所以,我已经这样做了

const loadTabs = (action$, state$) => action$.pipe(
  ofType(types.TABS_REQUESTED),
  mergeMap(() => api.fetchTabs()),
  switchMap(tabs => [
    actions.setTabs(tabs),
    actions.selectTab(tabs[0].id),
    actions.loadWidgets(tabs[0].id)
  ])
)

const loadWidgets = action$ => action$.pipe(
  ofType(types.WIDGETS_REQUESTED),
  mergeMap(() => api.fetchWidgets()),
  switchMap(widgets => [
    actions.setWidgets(widgets),
    actions.setDashboardReady(true)
  ]),
)