Rx framework – IObservable<T>.Repeat

Yan Cui

I help clients go faster for less using serverless technologies.

Having looked at a number of extension methods in Rx which allows you to combine two observable collections in some way, namely:

There are also extension methods which allow you to repeatedly subscribe to the same observable collection, the suitably name IObservable<T>.Repeat method.

If the repeated observable collection is pre-determined, i.e. regardless of when you subscribe to the observable it’ll always return the same set of values (such as Observable.Return(1)), then those same values will be repeated in the resulting observable collection. This type of observable collections are referred to as Cold Observables, the enumeration equivalent of these will be an array whose values have been defined upfront.

Conversely, if the observable collection’s values are determined at the point of subscription, then when the observable collection is repeated it might yield a different set of values (e.g. when you use the Observable.Defer method to generate an observable collection). This type of observable collections are referred to as Hot Observables, the enumeration equivalent of these will be an Iterator which uses yield return to return whatever value it has at the point of execution.

You can call Repeat either as an extension method:

var zs = xs.Repeat(3);

or as a static method:

var zs = Observable.Repeat(xs, 3);

There are a few things you should keep in mind when you’re using the Repeat method though.

1. If you call Repeat method with no argument, it’ll repeatedly subscribe to the same observable collection indefinitely

2. When you supply the Repeat method with a repeat count, the count actually means how many times in total the observable collection will be subscribed. I.e. xs.Repeat(1) subscribes to xs once, and xs.Repeat(0) will not subscribe to xs at all.

3. If an exception is thrown during any repetition of the observable collection, the resulting observable collection excepts too as illustrated below:



Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.


3 thoughts on “Rx framework – IObservable<T>.Repeat”

  1. Pingback: Rx framework — IObservable<T>.Retry | theburningmonk.com

  2. You’ve reversed the definition of cold and hot observable.
    I quote the RX hands on labs:

    “Most of the sequences we’re looking at in this exercise are so-called cold observables which means
    they start running upon subscription. This is different from hot observables such as mouse move events which
    are flowing even before a subscription is active (there’s no way to keep the mouse from moving after all…).”

    and you can also have a look at http://jesseliberty.com/2011/05/09/hot-and-cold-observables-in-rx/

  3. @liviu trifoi – yes you’re right, that was a typo on my part, meant to say that the other way round! Corrected accordingly, thanks for pointing it out.

Leave a Comment

Your email address will not be published. Required fields are marked *