-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
For reference, the Rx Design Guidelines 5.10 suggest to use this operator.
The operator .publish()
returns an ConnectableObservable
, this means you first need to subscribe all your sources, .connect()
it, keep a reference to your connect to prevent leaks, and then you also end up with a hot observable, so downstream subscribers better be ready as well.
Most of the time you only want to duplicate a stream locally. RxJS 4 and many other Rx implementations allow you to write
xs.publish(ob => ob.zip(ob.skip(1), (x, y) => {...}))
Here we use publish to peek at the next item in the source. Another favorite of mine is
xs.publish(ob => ob.window(ob.debounce(100)))
Because often you want to use a source for its own window-boundary.
This operator is very similar to .let
, but avoid subscribing to the source twice. Useful on cold observables, but also to avoid the overhead of subscribing multiple times to a hot source. (imagine you use this trick 4x in a row with let
, you end up with 2^4th subscriptions.)