DEV Community

Elia
Elia

Posted on

This nest Behavior are triggered

Hi,

I try to implements this function:

mainBehavior$
    .pipe(
      switchMap((mainBehavior) =>
        combineLatest([secondBehavior$, thirdBehavior$]).pipe(
          tap(([secondBehavior, thirdBehavior]) =>
            console.log(
              'PROBLEM: ',
              mainBehavior,
              secondBehavior,
              thirdBehavior
            )
          )
        )
      )
    )
    .subscribe();
Enter fullscreen mode Exit fullscreen mode

But if i next the second or third Behavior, them trigger the mainBehavior.

So i implements this:

function getSecondBehavior() {
  return secondBehavior$.getValue();
}

function getThirdBehavior() {
  return thirdBehavior$.getValue();
}
Enter fullscreen mode Exit fullscreen mode
mainBehavior$
    .pipe(
      filter((mainBehavior) => !!mainBehavior),
      map((mainBehavior) => [
        mainBehavior,
        getSecondBehavior(),
        getThirdBehavior(),
      ]),
      tap(([mainBehavior, secondBehavior, thirdBehavior]) =>
        console.log('FIXED: ', mainBehavior, secondBehavior, thirdBehavior)
      )
    )
    .subscribe();
Enter fullscreen mode Exit fullscreen mode

Do you know how to edit my first function for not doing this workaround?

Link to Stackblitz for reproduce: https://stackblitz.com/edit/rxjs-8ay9ov?file=index.ts

Top comments (0)