使用接收框架使用无效AsyncMethod异步调用(动作< T>回调)模式 [英] Using Rx Framework for async calls using the void AsyncMethod(Action<T> callback) pattern

查看:111
本文介绍了使用接收框架使用无效AsyncMethod异步调用(动作< T>回调)模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过吨关于如何使用Observable.FromAsyncPattern()在接收框架,简化异步调用的例子,但我使用不使用的IAsyncResult的BeginXXX / EndXXX标准异步模式的接口(IAsyncResult的),所以这不会为我工作。

我工作的库自曝异步函数与回调拍打:

 无效GetAllObjects(动作<名单,LT;对象>>回调)

在一个理想的世界,我想关闭此:

  VAR isLoadingUsers = TRUE;
VAR isLoadingSystems = TRUE;
VAR isLoadingCustomers = TRUE;
VAR isLoadingRules = TRUE;mClient.GetAllUsers(UsersCallback);
mClient.GetAllCustomers(CustomersCallback);
mClient.GetAllRules(RulesCallback);//设置IsLoadingXXX变量为false回调
//一旦所有都是假的mClient.GetAllSystems(SystemsCallback);

弄成这个样子:

 变种O = Observable.ForkJoin(
                     Observable.Start(GetAllUsers()),
                     Observable.Start(GetAllCustomers()),
                     Observable.Start(GetAllRules())
                    )。最后(()=> GetAllSystems);

人会如何去开启这个模式到的东西,返回的IObservable?


解决方案

  Func键<&的IObservable LT; TRET>> FromListCallbackPattern(动作<作用<名单,LT; TRET>>>功能)
{
    回报()= GT; {
        //我们使用ReplaySubject因此,如果人们以后*的订阅
        //真正的方法完成,他们仍然会得到所有的项目
        RET =新ReplaySubject<&TRET GT;();        功能((名单)=> {
            //我们要重播列表上的主体
            //这是不这样做的最Rx'iest的方式,但它是最
            // COM prehensible :)
            的foreach(在列表VAR V){
                ret.OnNext(五);
            }
            ret.OnCompleted();
        });        返回RET;
    };
}

现在,你可以这样做:

  VAR getAllUsers = FromListCallbackPattern(mClient.GetAllUsers);
。getAllUsers()订阅(X => / * ... * /);

I've seen tons of examples on how to use the Observable.FromAsyncPattern() in the Rx Framework to simplify Async calls, but I'm using an interface that doesn't use the standard Async pattern of IAsyncResult BeginXXX/EndXXX(IAsyncResult), so this doesn't work for me.

The library I'm working with exposes async functions with a callback patter:

void GetAllObjects(Action<List<Object>> callback)

In an ideal world I'd like to turn this:

var isLoadingUsers = true;
var isLoadingSystems = true;
var isLoadingCustomers = true;
var isLoadingRules = true;

mClient.GetAllUsers(UsersCallback);
mClient.GetAllCustomers(CustomersCallback);
mClient.GetAllRules(RulesCallback);

// set the IsLoadingXXX variables to false in callbacks
// once all are false

mClient.GetAllSystems(SystemsCallback);

into something like this:

var o = Observable.ForkJoin(
                     Observable.Start(GetAllUsers()),
                     Observable.Start(GetAllCustomers()),
                     Observable.Start(GetAllRules())
                    ).Finally(() => GetAllSystems);

How would one go about turning that pattern into something that returns an IObservable?

解决方案

Func<IObservable<TRet>> FromListCallbackPattern(Action<Action<List<TRet>>> function)
{
    return () => {
        // We use a ReplaySubject so that if people subscribe *after* the
        // real method finishes, they'll still get all the items
        ret = new ReplaySubject<TRet>();

        function((list) => {
            // We're going to "rebroadcast" the list onto the Subject
            // This isn't the most Rx'iest way to do this, but it is the most
            // comprehensible :)
            foreach(var v in list) {
                ret.OnNext(v);
            }
            ret.OnCompleted();
        });

        return ret;
    };
}

Now, you can do something like:

var getAllUsers = FromListCallbackPattern(mClient.GetAllUsers);
getAllUsers().Subscribe(x => /* ... */);

这篇关于使用接收框架使用无效AsyncMethod异步调用(动作&LT; T&GT;回调)模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆