I need to bring some data into an IObservable stream. I want to use an extension method on IObservable to do this, but i am not sure how to do it.
The data is produced by a simple class that has this signature:
interface Process<TIn, TOut> {
void Do(TIn data);
event Action<TOut> Result;
}
i.e. to start the process I have to call Do(...) and the result is sent to the event Result(...). This can’t be changed!
I want to bring this data into an Rx process, that handles user input in this way:
subject.AsObservable<string> // this produces the observable of user inputs
.Throttle(TimeSpan.FromMilliseconds(typingDelay))
.DistinctUntilChanged()
.Process(myProcess) // this is what I need help for
.Switch()
.Subscribe(myConsumer)
This is from adopted from the standard example of delayed user input triggering a web service (or something else that is long-running and has to be async) in the Hands-on Labs.
Whenever the user continues to enter, all observables that are still “underway” must be cancelled (therefore the Switch()). So my Process() has to return an IObservable<IObservable<TOut>> to make the switch work correctly.
I’m really stuck here. Does anyone has a hint for me how to write this Process(...) extension method?
I’ve modified your
Processinterface to use generics properly and provided a dummy implementation of the interface like so:Now you can write the extension method like this:
Does this work for you?