Rx framework – IObservable<T>.Repeat

Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.

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:

image

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.


Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!


Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!


Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you