我有以下2个使用Action回调的异步方法
public interface IGeoCoordinateService
{
void Start(Action<GeoPosition<GeoCoordinate>> positionCallback, Action<Exception> exceptionCallback);
}
public interface IGooglePlacesService
{
void FindLocations(GeoCoordinate geoCoordinate, SearchFilter searchFilter, Action<PlaceSearchResponse> searchCallback);
}
使用Rx执行此操作的最简单方法是什么? (我极端的菜鸟所以请温柔地使用Rx术语)
答案 0 :(得分:2)
最简单的方法是首先将接口转换为IObservables。为此,我将对您的API做出一些假设:
然后,您可以编写这些函数:
public IObservable<GeoPosition<GeoCoordinate>> ToObservable(this IGeoCoordinateService service)
{
return Observable.Create((IObserver<GeoPosition<GeoCoordinate>> observer) =>
{
service.Start(observer.OnNext, observer.OnError);
//nothing to do on unsubscribe, cannot cancel run
return (() => {});
})
}
public Func<GeoCoordinate, SearchFilter, IObservable<PlaceSearchResponse>>
ToObservable(IGooglePlacesService service)
{
return (coord, filter) =>
{
return Observable.Create((IObserver<PlaceSearchResponse> observer) =>
{
service.FindLocations(coord, filter,
(value) =>
{
observer.OnNext(value);
observer.OnCompleted();
});
//nothing to do on unsubscribe, cannot cancel run
return (() => {});
})
}
}
现在您可以将服务转换为IObservables,您可以将它们链接起来(希望您可以提供更好的函数名称):
public IObservable<PlaceSearchResponse> ChainServices(IGeoCoordinateService geo, IGooglePlacesService place, SearchFilter filter)
{
return from pos in geo.ToObservable()
where pos != null && pos.Coordinate != null
from placeResponsen place.ToObservable()(pos.Coordinate, filter)
select placeResponse;
}
请注意,按照IObservables的惯例,调用ChainServices什么都不做,你必须在返回的IObservable上调用Subscribe来实际进行异步调用。
如果您有任何控制权,API可以使用的一些内容: