Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

javascript - When should I use `publishReplay` vs `shareReplay`?

I already know that

  • publish shares a single subscription and also returns a ConnectableObservable ( so we have to Connect())

  • Share() is publish().refcount()

The Replay postfix is pretty obvious, it returns its last emission/s.

Let's take for example an Angular HTTP request with present AND future subscription :

<p>{{ (person | async)?.id   }}</p> //present markup

<p *ngIf=”show”>{{ (person | async)?.userId }}</p> <!-- future markup -->

If I don't want multiple http requests I can use :

publishReplay().Connect()

But I can also use: shareReplay(), but I'm sure that there is one here that is more correct to use than the other.

Question :

When should I use publishReplay vs shareReplay? What will be the difference in terms of that HTTP present & future request?

NB Why there's no documentation about shareReplay?

question from:https://stackoverflow.com/questions/47926240/when-should-i-use-publishreplay-vs-sharereplay

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

publishReplay allows you to controls when the subscription starts. shareReplay will start automatically upon the first subscription.

Generally, if the observable is to be used in a template (html file) use shareReplay. The advantage being you won't have to worry about unsubscribing etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...