开发者

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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜