- [Instructor] In the last video,
we learned about observables and observers.
Observers contain three methods:
next, error, and complete.
Observables are the outer wrapper that
allow you to listen to an observer.
One important thing to note is that
each time you create an observable,
you get a single instance.
This means you cannot reuse or share observers.
Each new observable will contain its own observer
and these observers cannot interact with each other.
You can subscribe to the same observable more than once
but each subscription will be a separate instance
and they won't know about each other.
So for instance, in this code,
say we want to go outside and do observer dot next here,
as you can see from the red squiggly line,
already, it's not going to allow us to do that,
we can't access that outside of the original observable.
But what if we want to access it
outside of the original observable?
How do we handle that?
Well, we can write a wrapper function to do that for us,
but we don't need that because
we already have something for this
and it's called a subject.
A subject is both an observer and an observable.
Once you define a subject,
you have access to dot next, dot subscribe,
or anything else you might need from anywhere.
So, let's check this out.
First, we're going to import subject from RxJS.
Next, we'll create a new subject called mySubject.
And this, mySubject equals new subject.
Now, I like to leave the unsubscribe in here
so I don't forget to unsubscribe,
so we're going to change this to mySubject
and I'm going to leave this in here just so that
I don't forget to unsubscribe.
Now we're going to be able to subscribe to that subject
and call next and let's see how that works.
So, we'll do this dot mySubject,
dot next, one,
this dot mySubject, dot next.
So, now we have that console log that
looks pretty much like what we did before.
What we want to do here is let's do a this dot mySubject,
dot subscribe,
and in here, we'll get our payload
and we're going to console log it
and it's just going to say first subscribe
and it's going to give us our payload.
That's it.
Now before we go any further,
let's go look at that in the console log
to see what it looks like.
Okay, we have first subscribe one, first subscribe two.
Now, let's add another subscription.
This dot mySubject, dot subscribe,
this is going to look exactly the same.
The only difference is we're going to put second subscribe
and then we'll log our payload.
And let's do one more dot next with the number three.
Okay, so we have first subscribe one,
first subscribe two, first subscribe three,
and the second subscribe only gets the three.
So, our first subscriber got all three messages,
but our second subscriber only got the last one
because with this subject,
we only get notified of events after we've subscribed.
What happens if you want all the values,
even the ones that have already passed?
Don't worry, there's a subject for that too
and we'll cover that in just a minute.
First, just one more important thing to know about subjects:
subjects are shareable but they are not reusable.
Remember that once you call error or complete on a subject,
it's dead, you cannot use it again
and if you try, it will silently ignore you.
So, to show that,
we're going to put in here this dot mySubject, dot complete
and I want you to watch what's going to happen
to that second subscribe that's called after the complete.
Nothing,
it didn't even tell us that anything was wrong,
it just silently ignored us.
So, I really recommend that rather than using complete here,
we should use unsubscribe.
Now we're going to get an error,
so we recommend using unsubscribe rather than complete
when you're finished with a subject,
that way if you accidentally call that subject again,
at least you'll get an error message to let you know.