In my system I have a source, two "steps" that map the source to a new value, and then a sum that combines those two steps to create a final value. The initial run through of this system works as I hoped, generating a single sum of 3.
var source = new Rx.BehaviorSubject(0);
var stepOne = source.map(function (value) {
return value + 1;
});
var stepTwo = source.map(function (value) {
return value + 2;
});
var sum = Rx.Observable.combineLatest(
stepOne,
stepTwo,
function (s1, s2) {
console.log('calc sum: ' + (s1 + s2));
return s1 + s2;
}).subscribe(function (sum) {
});
Outputs:
> calc sum: 3
But if I then put in a new value for source I get two results like this:
source.onNext(1);
> calc sum: 4
> calc sum: 5
The first is an intermediate result… as the new source value passes through one part of the system, and then I get the final result when all values have finished propagating.
So my questions is, what's the recommended way to configure things so that a new value pushed into source will pass through the system atomically and only generate one sum result?
Thanks!