I would like to subscribe an async method to an observable. The idea would be similar to what is discussed here: Howto call back async function from rx subscribe?. But I was unable to change it so that I can have the value carried by the observable (x) to be passed to the method. Method D is working and looks like something I could accept, but looks awful. Is there any neater way?
Here is the base sketch (in LinqPad):
async Task Main()
{
var ox = Observable.Interval(TimeSpan.FromSeconds(1)).Timestamp();
var dA = ox.Subscribe(x => Console.WriteLine("A: {0}: {1}", x.Value, x.Timestamp));
var dB = ox.Subscribe( _ => AsyncMethod('B',_)); //works, but not awaited
var dC = ox.Subscribe( async _ => await AsyncMethod('C',_)); //works, but async void, that should be avoided
var dD = ox.SelectMany( _ => Observable.FromAsync( __ => AsyncMethodD(_))).Subscribe(); // really???
await Task.Delay(TimeSpan.FromSeconds(10));
dA.Dispose();
dB.Dispose();
dC.Dispose();
dD.Dispose();
}
async Task AsyncMethod(char mode, Timestamped<long> x)
{
Console.WriteLine($"{mode}: {x.Value}: {x.Timestamp}");
await Task.FromResult(true); // fake async
}
async Task AsyncMethodD(Timestamped<long> x)
{
Console.WriteLine($"D: {x.Value}: {x.Timestamp}");
await Task.FromResult(true); // fake async
}
Thank you.