Using Reactive Extensions, I want to ignore messages coming from my event stream that occur while my Subscribe
method is running. I.e. it sometimes takes me lon
Yet another solution.
This is not pretty, because it mixes Task
and Observable
, so it's not really testable using ReactiveTest
(though to be honest, I'm not sure how I'd implement a 'slow' subscriber with ReactiveTest
either).
public static IObservable ShedLoad(this IObservable source)
{
return Observable.Create(observer =>
{
Task task = Task.FromResult(0);
return source.Subscribe(t =>
{
if(task.IsCompleted)
task = Task.Run(() => observer.OnNext(t));
else
Debug.WriteLine("Skip, task not finished");
}, observer.OnError, observer.OnCompleted);
});
}
I'm guessing there might be a race condition in there, but to my mind, if we're at the stage where we're ditching stuff because it's going too fast, I don't mind ditching one too many or too few. Oh, and each OnNext
is called (potentially) on a different thread (I guess I could put a Synchronize
on the back of the Create
).
I admit I couldn't get the Materialize extension to work properly (I hooked it up to a FromEventPattern(MouseMove)
and then subscribed with a deliberately slow Subscribe, and weirdly it would let bursts of events through, rather than one at at time)