2017-04-24 68 views
1

我正在寫一個史詩使用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 
    } 
} 

但是我不能在一個史詩中有兩個返回方法嗎?

回答

0

這聽起來像你想使用combineEpics

import { combineEpics } from "redux-observable"; 

const landingEpic1 = // put epic1 definition here 

const landingEpic2 = // put epic2 definition here 

export default combineEpics(
    landingEpic1, 
    landingEpic2, 
    // ... 
); 
+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', 
     }), 
); 
}