Search code examples
javascriptrxjsfrpsubject

RX Subject add new emit using last value


I have two subjects: a$ and b$.

var a$ = new Rx.Subject();
var b$ = new Rx.Subject();

When b$ emits something, need to take this value and add with last emit from a$. Then it needs to send this result to a$.

a$ ---4----6------------->
b$ -------------3-------->

result

a$ ---4----6----9-------->
b$ -------------3-------->

I can not find a solution to this task


Solution

  • You probably can use the withLatestFrom operator. For example :

    b$.withLatestFrom(a$, function (b,a){return a+b})
      .subscribe(function(x){a.onNext(x)})
    

    or more succintly :

    b$.withLatestFrom(a$, function (b,a){return a+b})
      .subscribe(a)