Correlate interdependent Event Streams with RX.Net

删除回忆录丶 提交于 2020-01-06 13:58:32

问题


I have a class that has, among others, the three following events:

  • DataSourceLoaded
  • DataSourceUnloaded
  • DataSourceFieldChanged

Right now I am using RX.Net in combination with its .Throttle()'ing functionality to slow down handling of burst / frequent incoming events as I only need to know IF something changed recently, not every single occurrence is relevant to me.

The part that I have a bit of problems with is that the underlying datasource may be added/removed at any time and the handler of the DataSourceFieldChanged event streams uses the datasource.

So basically the DataSourceFieldChanged event stream should only start once the DataSourceLoaded event occurred, stop immediately once a DataSourceUnloaded event occurred and re-start whenever the DataSourceLoaded reoccurred.. and so on.

After that DataSourceUnloaded event occurred, all 'throttled' DataSourceFieldChanged streams should however not trigger again.. as in: if a DataSourceFieldChanged event occurred and within the 500ms .Throttle() timespan a DataSourceUnloaded event occurred, the DataSourceFieldChanged event stream should not call the handler.

If a(nother) DataSourceLoaded event happens within or outside that previous 500ms DataSourceFieldChanged .Throttle() window also happens, they should also not trigger the DataSourceFieldChanged handler.

Is there any way to bring these three event streams in one combined RX.Net statement?

Moreover & ideally, IF the handler of DataSourceFieldChanged is already running while the DataSourceUnloaded event occurs, is it possible to pass along a CancellationToken into the DataSourceFieldChanged handler that got triggered beforehand and lets me cancel ongoing activities (so that I do not attempt to access the now gone datasource)?


回答1:


Assuming that DataSourceLoaded and DataSourceUnloaded events are always raised in pairs:

from _ in DataSourceLoaded
from changed in DataSourceFieldChanged.Throttle(x).TakeUntil(DataSourceUnloaded)
select changed;

Read it as:

  1. For each DataSourceLoaded event,
  2. project throttled DataSourceFieldChanged events,
  3. until a DataSourceUnloaded event.


来源:https://stackoverflow.com/questions/26640548/correlate-interdependent-event-streams-with-rx-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!