问题
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:
- For each
DataSourceLoadedevent, - project throttled
DataSourceFieldChangedevents, - until a
DataSourceUnloadedevent.
来源:https://stackoverflow.com/questions/26640548/correlate-interdependent-event-streams-with-rx-net