【发布时间】:2020-07-02 10:19:21
【问题描述】:
我有一个可观察的发射项目,我想在发射 n 个项目或经过一定时间间隔时收到通知。我正在寻找可以让我这样做的 Reactive 运算符。
此运算符可以具有与 Buffer(timeSpan, count) 相同的签名。我什至可以使用 Buffer,只是我不想缓冲任何东西,我只需要显示以下内容的事件:
n 个项目已发出或间隔已过
.
谢谢。
【问题讨论】:
标签: c# system.reactive
我有一个可观察的发射项目,我想在发射 n 个项目或经过一定时间间隔时收到通知。我正在寻找可以让我这样做的 Reactive 运算符。
此运算符可以具有与 Buffer(timeSpan, count) 相同的签名。我什至可以使用 Buffer,只是我不想缓冲任何东西,我只需要显示以下内容的事件:
n 个项目已发出或间隔已过
.
谢谢。
【问题讨论】:
标签: c# system.reactive
这是我对这个问题的看法:
/// <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
}
【讨论】: