Under the subjects
package you have classes like PublishSubject
and BehaviorSubject
which I suppose can be described as some usable sample Observables
.
How can these subjects be unsubscribed from? There is no unsubscribe
method and calling onCompleted
ends the Observable altogether right?
A
Subject
is anObservable
and anObserver
at the same time, it can be unsubscribed from just like normal observables. What makes subject special is that it is sort of bridge between observables and observers. It can pass through the items it observes by reemitting them, and it can also emit new items. Subjects are to observables as promises are to futures.Here is a brief description of the subjects family:
AsyncSubject: only emits the last value of the source Observable
BehaviorSubject: emits the most recently emitted item and all the subsequent items of the source Observable when a observer subscribe to it.
PublishSubject: emits all the subsequent items of the source Observable at the time of the subscription.
ReplaySubject: emits all the items of the source Observable, regardless of when the subscriber subscribes.
the official doc comes with some nice marble diagrams which makes it more easier to understand