Using concatMap in an Ngrx Component-store effect where state is changed and used

159 Views Asked by At

I am trying to "lock down" an @ngrx/component-store effect so that if it is invoked several times each call is sequentially processed in full. Here's the code I came up with:

private readonly _init = this.effect( (createNewSession$: Observable<boolean>) => {
    return createNewSession$.pipe(
        concatMap( createNewSession  => {
            return of(createNewSession).pipe(
                withLatestFrom(this.state$),
                filter( ([_, state]) => state.session == null),
                switchMap(([createNewSession2, state]) => this.orderInitService.init(state.batch, createNewSession2)),
                tapResponse(orderInitResponse => this.setState(() => {
                ...

It works, but feels awkward. I also had success using of(undefined).pipe(, but that doesn't get me away from the feeling that I am missing something.

I need to make sure tapResponse finishes before the next sequence (of rxjs operators) starts as that changes this.state, and hence withLatestFrom(this.state$) should provide me the latest correct values.

0

There are 0 best solutions below