[RxJS] Connection Operator:multicast and connect

Source: Internet
Author: User

We have seen how subjects is useful for sharing a execution of an RxJS observable to multiple observers. However, this technique requires some laborious setting up. In this lesson we'll learn about the multicast () operator which helps solve the same problem with less code, and with a Neater API.

Let's go back and remember why do we need subjects in the first place? Originally, we had one typical observable, but we wanted both observers A B and, to see the same execution of that Observable.

Does that mean this every time that we want to has multiple observers we need to set up a subject , and to the subscribe Observa Bles, to the subscribe subjects?

This system was not so ergonomic to set up. That's why there exists an operator or a method, that simplifies all of the for us. That's would be multicast . multicast is an operator on a normal observable. It takes here's argument, which is a subject .

//var Source = rx.observable//. Interval (+)//. Take (5);//var subject = new Rx.subject ();//Source.subscribe (subject);var connectableobservable=rx.observable. Interval (100). Take (5). Multicast (Newrx.subject ());  connectableobservable. Connect (); varObservera ={Next:function(x) {console.log (' A Next ' +x); }, Error:function(ERR) {console.log (' A error ' +err); }, Complete:function() {Console.log (' A done ')); },}; connectableobservable. Subscribe (Observera); Console.log (' Observera subscribed ');varObserverb ={Next:function(x) {console.log (' B Next ' +x); }, Error:function(ERR) {console.log (' B error ' +err); }, Complete:function() {Console.log (' B done ')); },};settimeout (function() { connectableobservable. Subscribe (OBSERVERB); Console.log (' Observerb subscribed ');}, 300);

Now if we this observable, the this, it would use the connect connectableObservable A to the this ReplaySubject subscribe observable. That's means that's when the late observer arrives here, it'll see the last values replayed to it. If We run this B arrives late, but B sees the latest values, zero and one, for instance.

//var Source = rx.observable//. Interval (+)//. Take (5);//var subject = new Rx.subject ();//Source.subscribe (subject);varConnectableobservable =rx.observable. Interval (100). Take (5). Multicast (NewRx.Replaysubject(100) ; Connectableobservable.connect ();varObservera ={Next:function(x) {console.log (' A Next ' +x); }, Error:function(ERR) {console.log (' A error ' +err); }, Complete:function() {Console.log (' A done ')); },};connectableobservable.subscribe (Observera); Console.log (' Observera subscribed ');varObserverb ={Next:function(x) {console.log (' B Next ' +x); }, Error:function(ERR) {console.log (' B error ' +err); }, Complete:function() {Console.log (' B done ')); },};settimeout (function() {Console.log (' Observerb subscribed '); Connectableobservable.subscribe (Observerb);},300);
/* "Observera subscribed" "a Next 0" "a Next 1" "A Next 2" "Observerb subscribed" "B Next 0" "B Next 1" "B Next 2" "a Next 3" "B N Ext 3 "A next 4" "B Next 4" "A Done" "B Done"* /

[RxJS] Connection Operator:multicast and connect

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.