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

前端 未结 6 2154
谎友^
谎友^ 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:16

    There's no built in operators to do what you're asking, but here's one that uses Publish to run two queries while only subscribing to the underlying observable once:

    // Emits matching values, but includes the value that failed the filter
    public static IObservable TakeWhileInclusive(
        this IObservable source, Func predicate)
    {
        return source.Publish(co => co.TakeWhile(predicate)
            .Merge(co.SkipWhile(predicate).Take(1)));
    }
    

    And then:

    var obs = listOfCommands.ToObservable()
        .TakeWhileInclusive(c.CurrentIndex != c.TotalCount);
    

提交回复
热议问题