简体   繁体   English

如何结束可观察的热点?

[英]How do I end a hot observable?

I'd like to end the observable based on an event if a value is not received within a specific time-frame. 如果在特定时间范围内未收到值,我想根据事件结束可观察的事件。

// This will go on forever because it is a hot
// observable based on an event
// If a value has not been produced in, say, 2 seconds
// I would like to stop it and raise an OnCompleted
var observable = Observable.FromEvent(...);

I could do a TakeUntil like so but that would be an absolute time period from the start of the subscription. 我可以像这样执行TakeUntil ,但这将是从订阅开始的绝对时间段。 What I want is, if a value has not been returned in, say, 2 seconds from the time the previous value was generated, then stop and raise an OnCompleted event. 我想要的是,如果从生成上一个值的时间起2秒钟内未返回任何值,则停止并引发OnCompleted事件。

If no value was ever generated by the observable for 2 seconds, then also stop and raise an OnCompleted event. 如果可观察对象在2秒钟内未生成任何值,则也请停止并引发OnCompleted事件。

How would I do that without the client code having to maintain this state about what time the last value was generated? 在没有客户端代码必须维护有关最后一次生成时间的状态时,该怎么办?

Try this: 尝试这个:

var query = observable.Timeout(TimeSpan.FromSeconds(2.0), Observable.Empty<int>());

Just replace int with whatever type your source observable is. 只需将int替换为您的源可观察到的任何类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM