Search code examples
typescriptrxjsrxjs6ngrx-effectscombinelatest

How to replace method 'combineLatest' from rxJs with a not deprecated method?


I implemented method using combinlatest rsjx/operator one, it work properly but sonar issue given said it deprecated.so , I need to convert it latest one. but I try, just replace import it gave error. I need some expert help to do this .

gX$ = createEffect(() => this.actions$.pipe(
    ofType(ActionType.A),
    combineLatest(this.service.getoc()),
    mergeMap(([, oc]) => this.reviewService.findBy(oc.id,
      new Date(),
      new Date(new Date().setDate(new Date().getDate() + 1)))
      .pipe(
        mergeMap(d => {
          return of(reviewLoadSuccess({ reviews: getReviews(d) }));
        }
        ),
        catchError(error => {
          return of(reviewLoadFailure({ error: error }));
        })
      )
    )));

Solution

  • Since it seems that you only need the value returned by this.service.getoc(), I would recommend of using switchMapTo operator instead, as demonstrated below

     gX$ = createEffect(() => this.actions$.pipe(
       ofType(ActionType.A),
       switchMapTo(this.service.getoc()),
       mergeMap(oc => this.reviewService.findBy(oc.id,
         new Date(),
         new Date(new Date().setDate(new Date().getDate() + 1)))
         .pipe(
           mergeMap(d => {
             return of(reviewLoadSuccess({ reviews: getReviews(d) }));
           }
           ),
           catchError(error => {
             return of(reviewLoadFailure({ error: error }));
           })
         )
       )));
    

    Consider applying the following changes, should you want to use the action as well :

    gX$ = createEffect(() => this.actions$
      .pipe(
        ofType(ActionType.A),
        switchMap(action => this.service.getoc().pipe(
          switchMap(oc => {
            //  you have access to both action, and oc
            // ... continue running your code
          })
        ))
      )
    )