BehaviorSubject: representing a value over time

André Staltz
InstructorAndré Staltz

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 years ago

Whenever we have multiple observers sharing the same execution, some of those observers may arrive "too late". This lesson will teach you how and when to use a BehaviorSubject to mitigate that problem.

[00:00] Whenever we have multiple observers sharing the same execution, some of those observers may arrive too late. For instance, here we have a situation where observer B subscribes to the subject two seconds after these three values were emitted on the subject.

[00:16] If you run this and see what happens, observer A was subscribed in time to receive the values, but B was too late. Because B subscribing doesn't create a new execution, it just shares the execution that is ongoing, it only sees values that will come in the future, but not those that happened in the past.

[00:35] Let's try to draw a Marble diagram of this situation, so that we know exactly what we're dealing with, here. This will be the subject execution, or basically the values being emitted on the subject. It emits at some point here one, then emits two, and then emits three, but the observer A was subscribed before these three values were sent.

[00:58] If we draw here where A appeared, A appeared somewhere here, before. That's why it was able to see one, two, and three. It keeps on observing the future values, as well. But B, it was subscribed much after the value three was delivered. Maybe somewhere here, B subscribed. It is still observing what's happening on the subject, but it doesn't see any values in the future.

[01:27] How can we fix this? Because there are legitimate cases where we need a late observer like B to receive the current value from the subject. For instance, we would like B to see the value three. That's how we can use the so-called behavior subject.

[01:47] A behavior subject is a subject that always has a current value. That's why actually we need to initialize the behavior subject with the value, because it always has a value, then it also has to start with a value. That's because this type of subject is able to remember, what was the last value emitted?

[02:08] Then when late observers arrive, such as B, then the behavior subject will pass on to B, what was that value remembered inside the behavior subject? Now that we hit this run, we're going to see when A subscribes, A is actually going to receive, here at this spot, zero. Because that's the current value of the subject. Then when B subscribes, B will actually see here, three, like that.

[02:39] The behavior subject is quite useful to model a value over time compared to an event stream. A typical example of that would be your age versus your birthdays, because your birthdays are a stream of events. They're events that happen annually, but your age is a value. You always have an age. You always have that value.

[03:02] These are interrelated because, of course, your age changes on every birthday, but they are still fundamentally different ideas. You can converge from birthday to ages by remembering what was your latest birthday. That's essentially what we do here with behavior subject.

[03:21] The behavior subject is meant for values over time like an age is. But subject, the normal subject is meant for event streams like birthdays.