具有多个滤镜的redux-observable史诗

时间:2017-04-24 22:23:02

标签: redux-observable

我正在使用redux-observable编写史诗,并尝试使用多个过滤器(oftype)编写史诗。以下是我的示例代码

export const landingEpic = action$ => {
    console.log('inside landing epic');
    return action$.ofType('APPLY_SHOPPING_LISTS').map(() => (
        {
            type: 'APPLYING_SHOPPING_LISTS',
        })
    );

    return action$.ofType('APPLIED_SHOPPING_LIST'){
      //here I want to return something else
    }
}

但是我不能在一个史诗中有两个回归方法吗?

2 个答案:

答案 0 :(得分:3)

你想要将它们与Observable.merge()结合起来然后返回 - 但我也强烈建议将它们分成两个单独的史诗。它会让测试变得更容易,但那当然是你的电话。

export const landingEpic = action$ => {
  return Observable.merge(
    action$.ofType('APPLY_SHOPPING_LISTS')
      .map(() => ({
        type: 'APPLYING_SHOPPING_LISTS',
      }),

    action$.ofType('APPLIED_SHOPPING_LIST')
      .map(() => ({
        type: 'SOMETHING_ELSE',
      }),
  );
}

答案 1 :(得分:0)

听起来你想使用combineEpics

import { combineEpics } from "redux-observable";

const landingEpic1 = // put epic1 definition here

const landingEpic2 = // put epic2 definition here

export default combineEpics(
    landingEpic1, 
    landingEpic2,
    // ...
);
相关问题