Warm tip: This article is reproduced from serverfault.com, please click

Convert IEnumerable<Task<T>> to IObservable<T> with exceptions handling

发布于 2018-10-07 21:04:29

I want to convert IEnumerable<Task<T>> to IObservable<T>. I found solution to this here:

IObservable<T> ToObservable<T>(IEnumerable<Task<T>> source)
{
    return source.Select(t => t.ToObservable()).Merge();
}

It is perfectly ok for usual cases, but I need to handle exceptions, that could raise in that Tasks... So IObservable<T> should not be dead after first exception.

What I read, recommendation for this use case is to use some wrapper, that will carry actual value or error. So my attempt was

IObservable<Either<T, Exception>> ToObservable<T>(IEnumerable<Task<T>> source)
{
    var subject = new Subject<Either<T, Exception>>();

    foreach (var observable in GetIntsIEnumerable().Select(t => t.ToObservable()))
    {
        observable.Subscribe(i => subject.OnNext(i), e => subject.OnNext(e));
    }

    return subject;
}

With Either<T, Exception> borrowed from this article.

But this is not ok either, because OnCompleted() is not called. How should I solve it? I'm pretty new with Rx concept.

Here is full code for testing...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static Task Main()
        {
            SemaphoreSlim signal = new SemaphoreSlim(0, 1);

            //GetInts1().Subscribe(
            //    i => Console.WriteLine($"OK: {i}"),
            //    e => Console.WriteLine($"ERROR: {e.Message}"),
            //    () => signal.Release());

            GetInts2().Subscribe(r => Console.WriteLine(r.Match(
                    i => $"OK: {i}",
                    e => $"ERROR: {e.Message}")),
                () => signal.Release());

            return signal.WaitAsync();
        }

        static IObservable<int> GetInts1()
        {
            return GetIntsIEnumerable().Select(t => t.ToObservable()).Merge();
        }

        static IObservable<Either<int, Exception>> GetInts2()
        {
            var subject = new Subject<Either<int, Exception>>();

            foreach (var observable in GetIntsIEnumerable().Select(t => t.ToObservable()))
            {
                observable.Subscribe(i => subject.OnNext(i), e => subject.OnNext(e));
            }

            return subject;
        }

        static IEnumerable<Task<int>> GetIntsIEnumerable()
        {
            Random rnd = new Random();

            foreach (int i in Enumerable.Range(1, 10))
            {
                yield return Task.Run(async () =>
                {
                    await Task.Delay(rnd.Next(0, 5000));

                    if (i == 6)
                        throw new ArgumentException();

                    return i;
                });
            }
        }
    }

    /// <summary>
    /// Functional data data to represent a discriminated
    /// union of two possible types.
    /// </summary>
    /// <typeparam name="TL">Type of "Left" item.</typeparam>
    /// <typeparam name="TR">Type of "Right" item.</typeparam>
    public class Either<TL, TR>
    {
        private readonly TL left;
        private readonly TR right;
        private readonly bool isLeft;

        public Either(TL left)
        {
            this.left = left;
            this.isLeft = true;
        }

        public Either(TR right)
        {
            this.right = right;
            this.isLeft = false;
        }

        public T Match<T>(Func<TL, T> leftFunc, Func<TR, T> rightFunc)
        {
            if (leftFunc == null)
            {
                throw new ArgumentNullException(nameof(leftFunc));
            }

            if (rightFunc == null)
            {
                throw new ArgumentNullException(nameof(rightFunc));
            }

            return this.isLeft ? leftFunc(this.left) : rightFunc(this.right);
        }

        /// <summary>
        /// If right value is assigned, execute an action on it.
        /// </summary>
        /// <param name="rightAction">Action to execute.</param>
        public void DoRight(Action<TR> rightAction)
        {
            if (rightAction == null)
            {
                throw new ArgumentNullException(nameof(rightAction));
            }

            if (!this.isLeft)
            {                
                rightAction(this.right);
            }
        }

        public TL LeftOrDefault() => this.Match(l => l, r => default);

        public TR RightOrDefault() => this.Match(l => default, r => r);

        public static implicit operator Either<TL, TR>(TL left) => new Either<TL, TR>(left);

        public static implicit operator Either<TL, TR>(TR right) => new Either<TL, TR>(right);
    }
}
Questioner
Matěj Pokorný
Viewed
11
Enigmativity 2018-10-08 08:15:15

There's a built-in mechanism for handling errors like this. Simply use the .Materialize() operator which changes an IObservable<T> to an IObservable<Notification<T>> and allows errors and completions to be viewed as normal values.

So, as an example, Observable.Return<int>(42) produces a value 42 and a completion, but Observable.Return<int>(42).Materialize() produces a value Notification.CreateOnNext<int>(42), followed by a value Notification.CreateOnCompleted<int>(), followed by a normal completion.

If you have a sequence that produces an error then you effectively get a value Notification.CreateOnError<T>(exception) followed by a normal completion.

This all means that you can change your code like this:

IObservable<Notification<T>> ToObservable<T>(IEnumerable<Task<T>> source)
{
    return source.Select(t => t.ToObservable().Materialize()).Merge();
}

Your test code is a little bit complicated for my liking. You should never need to use a SemaphoreSlim nor a Subject in the way that you're using them.

I've written my own test code.

void Main()
{
    var r = new Random();

    IEnumerable<Task<int>> source =
        Enumerable
            .Range(0, 10).Select(x => Task.Factory.StartNew(() =>
    {
        Thread.Sleep(r.Next(10000));
        if (x % 3 == 0) throw new NotSupportedException($"Failed on {x}");
        return x;
    }));

    IObservable<Notification<int>> query = source.ToObservable();

    query
        .Do(x =>
        {
            if (x.Kind == NotificationKind.OnError)
            {
                Console.WriteLine(x.Exception.Message);
            }
        })
        .Where(x => x.Kind == NotificationKind.OnNext) // Only care about vales
        .Select(x => x.Value)
        .Subscribe(x => Console.WriteLine(x), () => Console.WriteLine("Done."));
}

public static class Ex
{
    public static IObservable<Notification<T>> ToObservable<T>(this IEnumerable<Task<T>> source)
    {
        return source.Select(t => t.ToObservable().Materialize()).Merge();
    }
}

A typical run of that code produces:

Failed on 3
2
5
4
Failed on 0
Failed on 9
Failed on 6
7
1
8
Done.