Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shareReplay in RxJS 5

According to the RxJS 5 MIGRATION.md it looks like shareReplay() been removed.

  1. Why?
  2. Does .publishReplay(1).refCount() faithfully replicate the behaviour? Basically I need to replay the single most recent data set to any new subscribers.
like image 769
Philip Bulley Avatar asked Feb 06 '16 21:02

Philip Bulley


People also ask

What is shareReplay in angular RXJS?

The shareReplay prevents additional subscribers to the returned observable triggering a new response, but you're creating a new one each time.

What is the key difference between share and shareReplay?

In contrast to share , shareReplay exposes a ReplaySubject to subscribers. ReplaySubject(1) is very similar to a BehaviorSubject .

What is pipe operator in RXJS?

Operatorslink Pipes let you combine multiple functions into a single function. The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.


2 Answers

Happy news, shareReplay() is back in RxJS 5.4.0: https://github.com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09

like image 181
Wayne Maurer Avatar answered Oct 27 '22 23:10

Wayne Maurer


The short answer : Quoting Sir Blesh :

The problem RxJS 5 is trying to solve is that ConnectableObservables should be "reconnectable", and refCount should return an observable that is cold until subscribed to, then hot until all subscriptions have ended, then cold again.

The issue is that of the behaviour of subjects after completion which prevents a connectable observable to be reconnected when completed.

The long answer : https://github.com/ReactiveX/RxJS/issues/453

The current API appears to be the result of a compromise between two visions of the reconnection issue. It would be great if those involved could do a summary of the issues at hand. My understanding is that .publishReplay(1).refCount() should keep the old behaviour, that is that when your number of subscribers reaches 0, the source is disconnected, and you can't replay it. But don't take my word for it, test it, I got lost following the discussion.

like image 43
user3743222 Avatar answered Oct 27 '22 23:10

user3743222