Search code examples
rxjsobservablecombinelatest

Combine Observables with latest of any one and previous value of the other observables


Let say I have observables A, B, C. I have to listen to change of these three and alter a caculation.

i.e., On value change on any one of the obseravable, I need to recalculate with the new value from the present and the old value for the rest of the observables.

I tried to use combineLatest, which was perfect except the first behavior that all the observables should have a latest/change in the value.


Solution

  • You can just prefix each source Observable with startWith operator before passing them into combineLatest:

    combineLatest(
      obsA$.pipe(startWith(null)),
      obsB$.pipe(startWith(null)),
      obsC$.pipe(startWith(null)),
    )
    

    Then you'll have to check manually what values are null.