Skip to content

Instantly share code, notes, and snippets.

@asizikov
Last active January 4, 2016 19:09
Show Gist options
  • Save asizikov/8665517 to your computer and use it in GitHub Desktop.
Save asizikov/8665517 to your computer and use it in GitHub Desktop.
protected IObservable<T> GetDataAsync<T>(RestfullRequest<T> request) where T : new()
{
if (!_cache.IsCached<T>(request.Url))
{
Debug.WriteLine("Getting data:" + request.Url);
return Observable.Create<T>(
observer =>
Scheduler.Default.Schedule(() => ExecuteRequest(request, observer))
);
}
Debug.WriteLine("Getting cached data:" + request.Url);
return Observable.Create<T>(
observer =>
Scheduler.Default.Schedule(() =>
{
var item = _cache.Fetch<T>(request.Url);
observer.OnNext(item);
Debug.WriteLine("UpdateData:" + request.Url);
ExecuteRequest(request, observer, true);
})
);
}
private void ExecuteRequest<T>(RestfullRequest<T> request, IObserver<T> observer, bool ignoreErrors = false)
where T : new()
{
request.Execute()
.Subscribe(result =>
{
_cache.Put(result, request.Url);
observer.OnNext(result);
},
ex =>
{
if (!ignoreErrors)
{
observer.OnError(ex);
}
},
observer.OnCompleted);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment