The Subject
function in the rxjs
library is a type of Observable
that allows values to be multicasted to many observers which are subscribed to the Subject
. Here's an example of how to use it in JavaScript:
340 chars23 lines
In the example code above, we create a new Subject
object and subscribe an observer observerA
to it. We then emit values 1
and 2
using the next
method of the subject
object.
Afterwards, we subscribe another observer observerB
to the subject
object and emit the value 3
. Both observers receive the value 3
as Subject
is multicasting the emitted values.
Note that Subject
is a mutable object and it is recommended to keep a reference to it and call its complete
method when you are done with it, in order to avoid memory leaks.
gistlibby LogSnag