RX Observable.TakeWhile checks condition BEFORE each element but I need to perform the check after

前端 未结 6 2138
谎友^
谎友^ 2020-12-30 11:50

Observable.TakeWhile allows you to run a sequence as long as a condition is true (using a delegate so we can perform computations on the actual sequence objects), but it\'s

6条回答
  •  执笔经年
    2020-12-30 12:12

    Final edit:

    I based my solution off of Sergey's TakeWhileInclusive implementation in this thread - How to complete a Rx Observable depending on a condition in a event

    public static IObservable TakeUntil(
            this IObservable source, Func predicate)
    {
        return Observable
            .Create(o => source.Subscribe(x =>
            {
                o.OnNext(x);
                if (predicate(x))
                    o.OnCompleted();
            },
            o.OnError,
            o.OnCompleted
        ));
    }
    

提交回复
热议问题