Is the an extension methods in .net Reactive extensions for Action<T>
I have two asynchronouse methods which take Action callbacks开发者_Go百科. I was wondering if there is an extension in Rx for actions?
My goal is to wait until both callback are called and then do some processing?
This is from Jesse and my upcoming book, but here you go, it's a freebie:
public Func<T1, IObservable<TRet>> FromCallbackPattern<T1, TRet>(Action<T1, Action<TRet>> originalMethod)
{
return new Func<T1, IObservable<TRet>>((param1) => {
var subject = new AsyncSubject<TRet>();
try {
return originalMethod(param1, (result) => {
subject.OnNext(result);
subject.OnCompleted();
});
} catch (Exception ex) {
subject.OnError(ex);
}
return subject;
});
}
Here's how you use it:
// Here's a sample method that follows the callback pattern
public void DownloadPageTextAsync(string url, Action<string> callback);
var dlPageRx = FromCallbackPattern(DownloadPageTextAsync);
dlPageRx("http://www.jesseliberty.com")
.Subscribe(pageText => Console.WriteLine(pageText));
精彩评论