Documentation for the topic is sparse and it's hard to discover an "entry-point" there.
But rxjs offers different types of Subjects, namely: BehaviorSubject, ReplaySubject and AsyncSubject.
Subject – No initial value or replay available. BehaviouralSubject – requires an initial value and emits current values to new subscribers. AsyncSubject – Emits latest values to subscribers on completion of the async task. ReplaySubject – replays a specified number of last values to new subscribers.
The EventEmitter class is essentially an RxJS observable stream which powers component and directive outputs – extends the RxJS subject class. The subject then, in turn, extends both the RxJS observer and observable class. Thus we are able to operate on it and subscribe to it just like any other RxJS stream.
A BehaviorSubject holds one value (so we actually need to initialize a default value). When it is subscribed it emits that value immediately. A Subject on the other hand, does not hold a value.
Semantics differ according to the type of subjects. I will divide them in two kinds : vanilla (Rx.Subject
), and special-purpose subjects (the other three). The special-purpose subjects share part of the semantics of the vanilla subject with a few caveats due to their specialization (for instance, completion/reconnection behaviour).
Vanilla Rx.Subject semantics
Key features
dispose
handler on their prototype). That means, among other things, they have: onNext
, onError
, onComplete
methodsubscribe
method.asObserver()
, and .asObservable()
) if need bedispose
ing a subject will unsubscribe all observers and release resources. I quote a key aspect of Rxjs contract and grammar :
This grammar allows observable sequences to send any amount (0 or more) of onNext messages to the subscribed observer instance, optionally followed by a single success (onCompleted) or failure (onError) message.
a vanilla subject (created with new Rx.Subject()
) implements that grammar : when onCompleted
has been called once, all subsequent calls to onNext
are ignored. Second call of onCompleted
on the same observer is also ignored. If an observer subscribes to the observable side of the subject, its onComplete
callback will immediately be called (http://jsfiddle.net/cLf6Lqsn/1/).
Creation
new Rx.Subject()
Returns a subject which connects its observer to its observable (jsfiddle). This example is taken from the official documentation and portrays how to use subjects as proxies. The subject is subscribed to a source (observer side), and is also listened on by observers (observable side). Any call to onNext
(observer side) results in the observable side calling onNext
with the same value for each of its observers.
Rx.Subject.create(observer, observable)
Creates a subject from the specified observer and observable. Those two are not necessarily connected. A good example can be seen in the implementation of Rx.Dom.fromWebSocket
which returns a subject used to send and receive data from a socket. The observer side of the subject sends data to the socket. The observable side is used to listen on incoming data from the socket. Also, a subject created this way does NOT have a dispose
method.
Specialized Rx.Subject semantics
reactivex.io
documentation covers pretty well most of the semantics of the specialized subjects.Hopefully I did not get too much wrong. I'll be happy to be corrected. Last note, this should be valid for RxJS v4.
For a detailed explanation of the behaviour of cold/hot observables, one can refer to : Hot and Cold observables : are there 'hot' and 'cold' operators?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With