uma
uma

Reputation: 1575

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 }));
        })
      )
    )));

Upvotes: 0

Views: 6543

Answers (2)

Rafi Henig
Rafi Henig

Reputation: 6424

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
      })
    ))
  )
)

Upvotes: 1

Mike Jerred
Mike Jerred

Reputation: 10525

You need to import from rxjs not rxjs/oeprators and use it like this:

import { combineLatest } from 'rxjs';

combineLatest([
  this.actions$.pipe(ofType(ActionType.A)),
  this.service.getoc()
]).pipe(mergeMap(...));

Upvotes: 6

Related Questions