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