[RxJS] Subject Basic

Source: Internet
Author: User

A Subject is a type that implements both Observer and Observable types. As an Observer, it can subscribe to observables, and as an Observable it can produce values and has OBSERVERSW subscribe It.

First time I Read

Implements both Observer and Observable types

I was quite confused.

As a Observable:

varSubject =NewRx.subject ();varSubscription =Subject.subscribe (function OnNext (x) {Console.log ('OnNext:'+x); }, function OnError (e) {Console.log ('OnError:'+e.message); }, Function oncompleted () {Console.log ('oncompleted'); }); Subject.onnext ('Our message #1'); Subject.onnext ('Our message #2');/*"Onnext:our message #1" "Onnext:our message #2"*/

Every time we call OnNext () message to yield the value.

As a Observer, so we use ' source ' to sebscribe ' subject ', then subscribe ' subject ' again to get the side effect

varSubject =NewRx.subject ();varSource = Rx.Observable.interval ( -). Map (function (v) {return 'Interval Message #'+v;}) . Take (5); Source.subscribe (subject);varSubscription =Subject.subscribe (function OnNext (x) {Console.log ('OnNext:'+x); }, function OnError (e) {Console.log ('OnError:'+e.message); }, Function oncompleted () {Console.log ('oncompleted'); }); SetTimeout (function () {subject.oncompleted ();}, +);/*"Onnext:interval message #0" "Onnext:interval message #1" "Onnext:interval message #2 " "oncompleted"*/

[RxJS] Subject Basic

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.