To use the connectable
function from the rxjs
library in TypeScript, you'll need to import it along with other necessary modules:
167 chars4 lines
The above code imports the Observable
and ConnectableObservable
classes, as well as the publish
operator, which is often used together with connectable
.
Once you have your observable, you can create a ConnectableObservable
using the connectable
function:
364 chars12 lines
In the above example, we created a simple Observable that emits two values. We then called the publish
operator to convert it to a ConnectableObservable
. Finally, we subscribed to the ConnectableObservable
, and called connect
to start emitting values.
By doing this, both subscribers receive every emitted value, as opposed to each having their own separate subscription and receiving separate emissions. This can be useful in a variety of scenarios where you want to share a single source of emissions across multiple subscribers.
gistlibby LogSnag