反应性油门返回时间跨度内添加的所有项目 [英] Reactive Throttle returning all items added within the TimeSpan

查看:27
本文介绍了反应性油门返回时间跨度内添加的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 IObservable 有没有一种方法可以使用 Throttle 行为(在添加项目时重置计时器,但让它返回所有在那段时间内添加的项目?

Given an IObservable<T> is there a way to use Throttle behaviour (reset a timer when an item is added, but have it return a collection of all the items added within that time?

Buffer 提供了类似的功能,它在每个时间跨度或计数上将数据分块到 IList 中.但是每次添加项目时我都需要时间来重置.

Buffer provides a similar functionality it that it chunks the data up into IList<T> on every time span or count. But I need that time to reset each time an item is added.

我在这里看到了一个类似的问题,反应式扩展是否支持滚动缓冲区?,但答案似乎并不理想,而且它有点旧,所以我想知道 Rx-Main 的发布版本现在是否支持此功能.

I've seen a similar question here, Does reactive extensions support rolling buffers?, but the answers don't seem ideal and it's a little old so I wondered if the release version of Rx-Main now supports this functionality out the box.

推荐答案

正如我在其他帖子中回答,是的你可以!使用ObservableThrottleWindow方法:

As I answered in the other post, yes you can! Using the Throttle and Window methods of Observable:

public static IObservable<IList<T>> BufferUntilInactive<T>(this IObservable<T> stream, TimeSpan delay)
{
    var closes = stream.Throttle(delay);
    return stream.Window(() => closes).SelectMany(window => window.ToList());
}

这篇关于反应性油门返回时间跨度内添加的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆