Search code examples
c#unity-game-enginerx.netunirx

How to write a SelectManyLatest operator in Rx.NET and UniRx


SelectManyLatest is necessary operator. But All Internet don't answer how you can write it. So how?

SelectManyLatest is a FlatMapLatest in other Rx Frameworks. This operator like SelectMany but it complete previous subscription if new emit happens.


Solution

  • Add as an Extension

    public static IObservable<V> SelectManyLatest<T, V>(this IObservable<T> source, Func<T, IObservable<V>> selector)
    {
        return source
            .Select(selector)
            .Switch();
    }