combineLatest Trap for RxJs newbies

I have been enjoying some of the features of RxJs on the refactor of a current project that needs to combine and react to changes in a number of different data streams.

One function I have made heavy use of is combineLatest, which combines the latest output from two observable sequences using a selector function.

I was initially under the impression that the selector function would only run when either of the combined streams emitted. However, while tracking down an obscure bug I discovered that in fact the selector function is run each time an observer subscribes, eg.

var combined  = Rx.Observable.combineLatest(obsOne, obsTwo, function(){

    console.log('I am combining streams');

});
    
var sub1 = combined.subscribe();
var sub2 = combined.subscribe();

//output
I am combining streams
I am combining streams

If the selector function is resource intensive, or has side effects that should only happen if the parent stream outputs change, then this is obviously not the most desirable outcome.

Luckily this is easily fixed by adding .shareReplay(1). This will cause the combineLatest to replay the last output from the selector function, unless the parent streams change., eg.

var combined  = Rx.Observable.combineLatest(obsOne, obsTwo, function(){{

    console.log('I am combining streams');

}).shareReplay(1);
    
var sub1 = combined.subscribe();
var sub2 = combined.subscribe();

//output
I am combining streams

Let me know if there is a better way to do this.

Comments

Post a Comment