Image for post
Image for post
Photo by Tim Mossholder on Unsplash

multicast has a secret. And so does publish — which wraps multicast. And it’s sometimes really useful.

The documentation for multicast and publish mentions a ConnectableObservable. A connectable observable is type of observable that waits until its connect method is called before it begins emitting notifications to subscribers. However, the multicast and publish operators don’t always return a connectable observable.

Let’s start by looking at the source code for publish:

It’s clear that publish is just a thin wrapper around multicast. It creates a subject, which is passed to multicast along with the optional selector function. The interesting bit is inside the multicast implementation, which contains the following code:

multicast will only return a connectable observable if a selector function is not specified. If a function is specified, the lift mechanism will be used to allow the source observable to create an observable of the appropriate type. There will be no need to call connect on the returned observable and the source observable will be shared within the scope of the selector function.

That means that multicast (and publish) can be used to easily implement the local sharing of a source observable.

Let’s look at an example that uses publish.

RxJS includes a defaultIfEmpty operator which takes a value that’s to be emitted if the source observable is empty. Sometimes, it’s useful be be able to specify a default observable — rather than a single value — so let’s implement a defaultObservableIfEmpty function that can be used with the let operator.

A marble diagram, showing its behaviour with an empty source, looks like this:

Image for post
Image for post

RxJS includes an isEmpty operator which will emit a boolean value when the source observable completes — indicating whether or not the source was empty. However, for it to be used it in the implementation of defaultObservableIfEmpty, the source observable needs to be shared — as the value notifications need to be emitted, too, and isEmpty does not do that. publish makes sharing the source observable easy and the implementation looks like this:

publish is passed a selector function that receives the shared source observable. The selector returns an observable composed from the shared source, merged with either the default observable, if the source is empty, or with an empty observable, if the source is not empty.

The sharing of the source observable is managed entirely by publish. Within the selector, it’s possible to subscribe to the shared observable as many times as is necessary, without effecting further subscriptions to the source.

Let’s look at another example, this time using multicast.

RxJS includes a takeWhile operator which returns an observable that emits values received from the source until a received value fails the predicate, at which point the observable completes. The value that fails the predicate is not emitted. Let’s implement a takeWhileInclusive function that can be used with the let operator.

A marble diagram, showing its behaviour with a value that fails the predicate, looks like this:

Image for post
Image for post

The implementation can use the takeWhile operator as its basis; it just needs to concatenate the last value, if it fails the predicate. To obtain the last value — after the observable returned by takeWhile completes — a ReplaySubject can be used:

The source observable is shared using a ReplaySubject with a buffer size of one. When the observable returned by the takeWhile operator completes, the shared observable is concatenated, with take(1) ensuring that only the replayed value is considered and filter ensuring that it’s only appended if it fails the predicate.

RxJS version 5 is a relatively new library and its documentation is a work in progress, so the behaviour is not-yet-documented, rather than internal. The public TypeScript signatures indicate that a ConnectableObservable is not always returned and there are unit tests for the behaviour.

There are other ways to implement these functions — which is often the case with RxJS — but the above examples show that when the local sharing of a source observable is necessary, publish and multicast are easy to use and are worth considering.

This post is also published on my personal blog:

RxJS core team member; front-end developer; mentor; speaker; open-source contributor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store