【问题标题】:ReactiveX Operator to be notified when n items have been emitted or interval elapsed whichever comes first当 n 个项目已发出或间隔已过时(以先到者为准)通知 ReactiveX 操作员
【发布时间】:2020-07-02 10:19:21
【问题描述】:

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

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

n 个项目已发出或间隔已过

.

谢谢。

【问题讨论】:

    标签: c# system.reactive


    【解决方案1】:

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

    /// <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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      相关资源
      最近更新 更多