是否有一个 Rx 框架函数可以创建一个可观察对象,该可观察对象在一段时间过去后结束?

发布于 2024-12-05 20:46:04 字数 461 浏览 0 评论 0原文

有些可观察量就是如此需要。他们希望你倾听、倾听、倾听他们所说的一切,也许是永远!如果我真的只能处理几秒钟的事件怎么办?我可以写一些像

mouseMoves.TakeFor(TimeSpan.FromSeconds(2))

概念上的东西吗,这将创建一个主题,它将传递事件直到经过一定时间,然后取消订阅底层可观察并将其自己的序列标记为完成。我怀疑你可以用手这样写,但似乎必须有一些现有的运算符可以做到这一点。我曾希望 Observable.TimeOut 能够执行我想要的操作,例如使用

mouseMoves.Timeout(TimeSpan.FromSeconds(2), Observable.Empty<T>())

但我认为它所做的是如果观察之间的时间跨度长于可观察对象的超时时间给定值。这也很有用,但不完全是我所追求的。

Some observables are just so needy. They want you to listen and listen and listen to all they have to say, maybe for all of time! What if I can really only handle a few seconds' of events? Could I write something like

mouseMoves.TakeFor(TimeSpan.FromSeconds(2))

Conceptually, this would create a subject which would pass along events until a certain time had elapsed, then unsubscribe to the underlying observable and mark its own sequence complete. I suspect you could write it that way by hand, but it seems like there must be some existing operators which do this. I had hoped that Observable.TimeOut would do what I want, e.g. using

mouseMoves.Timeout(TimeSpan.FromSeconds(2), Observable.Empty<T>())

But I think what it's doing is timing out the observable if the time span between observations is longer than the given value. This is useful, too, but not quite what I'm after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-12-12 20:46:04

听起来您需要 TakeUntil

var source = Observable.FromEventPattern(...);

source.TakeUntil(Observable.Timer(TimeSpan.FromSeconds(2))
   .Subscribe(_ => {});

Sounds like you need TakeUntil:

var source = Observable.FromEventPattern(...);

source.TakeUntil(Observable.Timer(TimeSpan.FromSeconds(2))
   .Subscribe(_ => {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文