Subject
A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.
Because a Subject subscribes to an Observable, it will trigger that Observable to begin emitting items (if that Observable is “cold” — that is, if it waits for a subscription before it begins to emit items). This can have the effect of making the resulting Subject a “hot” Observable variant of the original “cold” Observable.
See Also
- To Use or Not to Use Subject from Dave Sexton’s blog
- Introduction to Rx: Subject
- 101 Rx Samples: ISubject<T> and ISubject<T1,T2>
- Advanced RxJava: Subject by Dávid Karnok
- Using Subjects by Dennis Stoyanov
Varieties of Subject
There are four varieties of Subject
that are designed for particular use cases. Not all of these are available in all implementations, and some implementations use other naming conventions (for example, in RxScala, what is called a “PublishSubject” here is known simply as a “Subject”):
AsyncSubject
See Also
BehaviorSubject
See Also
PublishSubject
ReplaySubject
See Also
Language-Specific Information
RxGroovy
If you have a Subject
and you want to pass it along to some other agent without exposing its Subscriber
interface, you can mask it by calling its asObservable
method, which will return the Subject as a pure Observable
.
See Also
- Javadoc:
AsyncSubject
- Javadoc:
BehaviorSubject
- Javadoc:
PublishSubject
- Javadoc:
ReplaySubject
RxJava 1․x
If you have a Subject
and you want to pass it along to some other agent without exposing its Subscriber
interface, you can mask it by calling its asObservable
method, which will return the Subject as a pure Observable
.
See Also
- Javadoc:
AsyncSubject
- Javadoc:
BehaviorSubject
- Javadoc:
PublishSubject
- Javadoc:
ReplaySubject
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/subject.html