Action的 .net Reactive 扩展中的扩展方法是?

发布于 2024-11-27 13:11:11 字数 80 浏览 2 评论 0原文

我有两个采用 Action 回调的异步方法。我想知道 Rx 中是否有针对操作的扩展?

我的目标是等到两个回调被调用然后做一些处理?

I have two asynchronouse methods which take Action callbacks. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-12-04 13:11:11

这是来自 Jesse 和我即将出版的书,但是给你,这是免费赠品:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文