Implementing Your Own Operators

While Rx provides a number of operators to manage even very complex programming situations, there will be times when you need to customize the behavior for your specific goal.

Since operators in both Rx and LINQ are simply extension methods on IObservable and IEnumerable, it's easy to create your own. The simplest way to create your own operators is by combining existing operators.

We'll use Aggregate in a clever way, since it already encapsulates “return a value when the input completes.” We'll use the First method to do the blocking for us.

public static class BlockingObservableMixins {     public static void BlockUntilCompleted<T>(this IObservable<T> input)     {         input.Aggregate(0, (acc, x) => acc) ...

Get Programming Reactive Extensions and LINQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.