[RxJS] Subject:an Observable and Observer hybrid

Source: Internet
Author: User

This lesson teaches what a Subject is simply a hybrid of Observable and Observer which can act as a bridge between the Source Observable and multiple observers, effectively making it possible for multiple observers to share the same OBSERVAB Le execution.

varObservable = Rx.Observable.interval ( +). Take (5);varObservera ={next:function (x) {Console.log ('A Next'+x); }, Error:function (err) {Console.log ('A Error'+err); }, Complete:function () {Console.log ('A Done'); },};varObserverb ={next:function (x) {Console.log ('B Next'+x); }, Error:function (err) {Console.log ('B Error'+err); }, Complete:function () {Console.log ('B Done'); },};observable.subscribe (Observera); SetTimeout (()={observable.subscribe (observerb); }, -)

In the code above, we have both ' observers ', because we call subscribe twice:

Observable.scbscribe (Observera); Observable.scbscribe (Observerb);

If we want to has one observer, so we need to call subscribe only once.

For that we can build a bridgeobservers, which would loop though the observers:

ConstObservable = Rx.Observable.interval ( +). Take (5);ConstObservera ={next:function (x) {Console.log ("A Next"+x)}, Error:function (x) {Console.error ("A Error"+x)}, Complete:function () {Console.log ("A Done")  },};ConstObserverb ={next:function (x) {Console.log ("B Next"+x)}, Error:function (x) {Console.error ("B Error"+x)}, Complete:function () {Console.log ("B Done")  },};Const bridgeobservers={next:function (x) { This. Observers.foreach (o=O.next (x))}, Error:function (x) { This. Observers.foreach (o=o.error (x))}, Complete:function () { This. Observers.foreach (o=o.complete ())}, observers: [], ADDOBSERVER:FUNCTION (Observer) { This. OBSERVERS.PUSH (Observer)}};observable.subscribe (Bridgeobservers); Bridgeobservers.addobserver (Observera); SetTimeout (function () {bridgeobservers.addobserver (observerb);}, -)

And this partten:

Observable.subscribe (bridgeobservers); Bridgeobservers.addobserver (Observera); Birdegeobservers.subscribe (Observera)

is actually ' subject ' partten, works both as Observer and Observable.

Subject:

ConstObservable = Rx.Observable.interval ( +). Take (5);ConstObservera ={next:function (x) {Console.log ("A Next"+x)}, Error:function (x) {Console.error ("A Error"+x)}, Complete:function () {Console.log ("A Done")  },};ConstObserverb ={next:function (x) {Console.log ("B Next"+x)}, Error:function (x) {Console.error ("B Error"+x)}, Complete:function () {Console.log ("B Done")  },}; const subject = new  rx.subject (); /*Const Bridgeobservers = {next:function (x) {This.observers.forEach (o = O.next (x))}, Error:funct      Ion (x) {This.observers.forEach (o = o.error (x))}, Complete:function () {This.observers.forEach ( o = = O.complete ())}, observers: [], SUBSCRIBE:FUNCTION (Observer) {THIS.OBSERVERS.PUSH (Observer)}};*/ observable.subscribe (subject); Subject.subscribe (Observera); //Bridgeobservers.subscribe (Observera);setTimeout (function () { subject.subscribe (observerb); //Bridgeobservers.subscribe (observerb);}, -)

In the end, Observera and OBSERVERB share one single observer.

[RxJS] Subject:an Observable and Observer hybrid

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.