2017-08-09 28 views
2

我想從我的效果中多次發送一個動作,爲此目的我使用concatMap,但由於我正在調度相同的動作,它將被下一次發送取消。是否有辦法在其先前的調度完成時分派一個操作。如何從ngrx效果多次發送相同的動作

下面的代碼將有助於更好地理解問題。

@Effect() 
    setCategory$ = this.actions$ 
    .ofType(GET_SESSION_AD_SUCCESS) 
    .concatMap((action: Action) => { 

     const category = action.payload; 
     const categoryPath = category.path; 
     const categoryDispatchArray = []; 

     categoryPath.forEach((path, index) => { 
     categoryDispatchArray.push({ 
      type: GET_SUBCATEGORIES_BY_PARENT, 
      payload: { categoryId: path.id, index } 
     }); 
     }) 

     return dispatchArray; 

} 

回答

0

我覺得你的問題是,當最後一個觀察者傳遞給它的關閉時,concatMapped Observable會關閉。您需要的是mergeMap(又名flatMap)。

而且我會用.map創建dispatchArray:

@Effect() 
    setCategory$ = this.actions$ 
    .ofType(GET_SESSION_AD_SUCCESS) 
    .mergeMap((action: Action) => { 

     const category = action.payload; 
     const categoryPath = category.path; 

     const categoryDispatchArray = categoryPath 
     .map((path, index) => ({ 
      type: GET_SUBCATEGORIES_BY_PARENT, 
      payload: { categoryId: path.id, index } 
     }); 

     return categoryDispatchArray; 
    }