I have two merged observables with a scan after the merge. The first one is a simple range and the other is a Subject. Whenever the Subject emits a new value with onNext
I concatenate that value in the scan and return the new array as the accumulator. If I dispose of my subscription, and then subscribe again it replays the values from the range but I have lost the ones from the Subject. In the code below I want my second subscription to have a final value of [1, 2, 3, 4, 5]
What would be the best way to do this? Right now I have another Subject where I store that final value and subscribe to that, but it feels wrong.
Here's a simple version that demonstrates what is happening:
var Rx = require('rx');
var source = Rx.Observable.range(1, 3);
var adder = new Rx.Subject();
var merged = source.merge(adder)
.scan([], function(accum, x) {
return accum.concat(x);
});
var subscription1 = merged.subscribe(function(x) {console.log(x)});
adder.onNext(4);
adder.onNext(5);
subscription1.dispose();
console.log('After Disposal');
var subscription2 = merged.subscribe(function(x) {console.log(x)});
This outputs:
[ 1 ]
[ 1, 2 ]
[ 1, 2, 3 ]
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 4, 5 ]
After Disposal
[ 1 ]
[ 1, 2 ]
[ 1, 2, 3 ]