Between values with Rx

主宰稳场 提交于 2019-12-06 05:15:13

Here's what you need as an extension method:

public static IObservable<T> TakeBetween<T>(
    this IObservable<T> source,
    Func<T, bool> entry,
    Func<T, bool> exit)
{
    return source
        .Publish(xs =>
        {
            var entries = xs.Where(entry);
            var exits = xs.Where(exit);
            return xs.Window(entries, x => exits);
        })
        .Switch();
}

The key thing I've included in this is the used of the Publish extension. In this particular case it is important as your source observable may be "hot" and this enables the source values to be shared without creating multiple subscriptions to the source.

I think you can use Window:

source.Window(entrySignal, _ => exitSignal).Switch();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!