Su4p
Su4p

Reputation: 865

Passing value from one RxJS operator to another

Here is my code:

@Injectable()
export class TraitementDetailEffects {
    ingoing_loadDetail: { traitementID: number, obs: Promise<any> };
    @Effect()
    loadTraitementDetail$: Observable<Action> = this.actions$.pipe(
        ofType(ETraitementDetailActions.loadTraitementDetail),
        map((action: LoadTraitementDetail) => action.payload),
        switchMap((traitementID) => {
            if (this.ingoing_loadDetail && this.ingoing_loadDetail.traitementID === traitementID) {
                return this.ingoing_loadDetail.obs;
            }
            const obs = this.traitementsService.loadDetail(traitementID);
            this.ingoing_loadDetail = {traitementID: traitementID, obs: obs};
            return obs;
        }),
        map(result => {
            this.ingoing_loadDetail = null;
            //here I don't have access to traitementID :'(
            return new LoadTraitementDetailSuccess(traitementID, result);
        })
    );

    constructor(
        private actions$: Actions,
        private traitementsService: TraitementsService
    ) {

    }
}

I'm trying to pass the variable or value traitementID to the last map.

I tried to avoid the last map with an async await but then I get a weird errors "Effect dispatched an invalid action" and "Actions must have a type property" (FYI all my actions have a type property).

Upvotes: 0

Views: 929

Answers (1)

dsych
dsych

Reputation: 762

Try to bake this id into observable's resolve, like:

            switchMap((traitementID) => {
                return this.traitementsService.loadDetail(traitementID).pipe(
                           map(detail => ({detail,traitementID}))
                       );

            }),
            map(({detail,traitementID}) => {
                ...
            })

Upvotes: 2

Related Questions