[RxJS] Reactive programming-sharing Network requests with Sharereplay ()

Source: Internet
Author: User

Currently we show three users in the list, it actually does three time network request, we can verfiy this by console out EA CH Network Request:

var responsestream = startuprequeststream.merge (requestonrefreshstream)  = = {    Console.log (' Do network request ');     return Rx.Observable.fromPromise (Jquery.getjson (requesturl)   );

We actually can use the same network request by Sharereplay (1):

var responsestream = startuprequeststream.merge (requestonrefreshstream)  = = {    Console.log (' Do network request ');     return Rx.Observable.fromPromise (Jquery.getjson (Requesturl));  })  . Sharereplay (  1);

Why replay one? Well, that's because if there happens to be a really late subscriber...let ' s say someone does a setTimeout and doesn ' t sub scribe to the responsestream after a long while. Let ' s say, three seconds or even seconds. Then, the this subscribe would get a replayed response JSON. It won't do-a new network request, but it'll simply replay that same JSON that happened before.

[RxJS] reactive programming-sharing network requests with Sharereplay ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.