Last active
January 4, 2016 19:09
-
-
Save asizikov/8665517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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