Using Rx Framework for async calls using the void AsyncMethod(Action callback) pattern

前端 未结 3 1148
深忆病人
深忆病人 2020-12-20 00:26

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 st

3条回答
  •  一整个雨季
    2020-12-20 01:08

    Func> FromListCallbackPattern(Action>> 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();
    
            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 => /* ... */);
    

提交回复
热议问题