ReactiveX Operator 在发出 n 个项目或间隔时间结束时收到通知,以先到者为准

ReactiveX Operator to be notified when n items have been emitted or interval elapsed whichever comes first

我有一个可观察的发射项目,我想在发射 n 个项目或经过特定时间间隔时收到通知。我正在寻找可以让我这样做的 Reactive 运算符。

此运算符可以具有与 Buffer(timeSpan, count) 相同的签名。我什至可以使用 Buffer 除了我不想缓冲任何东西,我只需要这样的事件:

n items emitted or interval elapsed

.

谢谢。

这是我对这个问题的看法:

/// <summary>Returns true if the specified number of elements have been emitted
/// before the timeout has elapsed; otherwise, false.</summary>
public static IObservable<bool> EmittedCountOrTimeout<T>(
    this IObservable<T> source, int count, TimeSpan timeout)
{
    return source
        .Take(count)     // Take the first 'count' elements
        .Count()         // Count the number of emitted elements
        .Contains(count) // Confirm that 'count' elements have been emitted (could be less)
        .Timeout(timeout, Observable.Return(false)); // On timeout return false
}