What is Subject
?
Subject
is a special type of Observable
that allows emitting value between all subscribers.
Based on the implementation you can see that Subject
defines methods like:
next
: emit value to all subscriberserror
: emit error to all subscribers and unsubscribe all subscriberscomplete
: close the source and unsubscribe all subscribers
When to use Subject
?
Subject
is useful if you want to emit events (ex: click event, page changed event, modal closed event) and you are not interested in previous events.
What is BehaviorSubject
?
BehaviorSubject
is a special type of Subject that allows emitting value between all subscribers (like Subject
)
and storing the last emitted value. BehaviorSubject
require to define the initial value.
BehaviorSubject
is useful if you know the initial state otherwise you should try to use ReplaySubject
.
When a new subscriber arrives BehaviorSubject
emit stored value to him (look at _subscribe
method).
A new subscriber will not receive a value if BehaviorSubject is completed.
Getting stored value
You can get the current state without calling subscribe
method. To do it use getValue()
.
When to use BehaviorSubject
?
BehaviorSubject
is useful if you want to store the current state of the application
and you know the initial state or you can emulate it by marking the state as pristine.
Passing null
as an initial state is not a good idea if null
does not represent a real state.
What is ReplaySubject
?
ReplaySubject
is a special type of Subject
that allows emitting values between all subscribers (like Subject
)
and storing emitted values based on time and/or buffer size.
ReplaySubject
does not require to define an initial value, so is useful if the initial value is unknown (in contrast to BehaviorSubject
).
When a new subscriber arrives ReplaySubject
emits all
values from the buffer (look at _subscribe method).
If the buffer is empty no events will be emitted.
Thanks to _bufferSize
and _windowTime
params you can define how many values and how long you want to store them.
ReplaySubject
does not have a method to get the current buffer size so checking if the buffer is empty/full is not possible.
Buffer is trimmed only if a new subscriber has been added or a new value has been emitted. That means ReplaySubject it will not emit trimmed values even after window time has arrived.
By default buffer size is set as Infinity
.
If you do not need historical values do not forget to set the buffer size as one.
If you subscribe after calling complete
method you will also receive the latest value (unlike BehaviorSubject
).
When to use ReplaySubject
?
ReplaySubject
is useful if you want to store the state of part of an application but you do not know the initial state.
What is AsyncSubject
?
AsyncSubject
is a special type of Subject
that allows emitting value between all subscribers (like Subject
) but only the latest value on complete.
Note that the AsyncSubject
will only emit a value when the complete function is called, and it will only emit the last value that was passed to the next function.
If the complete function is not called, the AsyncSubject will not emit any values.
If you subscribe after the calling complete
method you will also receive the latest value.
When to use AsyncSubject
?
AsyncSubject
is useful if you want to cache a value that never changed.