From e6181c24124d97f2fbc932b8a68311e625463156 Mon Sep 17 00:00:00 2001 From: uzvkl Date: Tue, 11 Jun 2019 23:05:52 +0200 Subject: Move dsa related stuff to subfolder --- dsa/FireBase/Extensions/ObservableExtensions.cs | 41 +++++++++++++++++++++++++ dsa/FireBase/Extensions/TaskExtensions.cs | 23 ++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 dsa/FireBase/Extensions/ObservableExtensions.cs create mode 100644 dsa/FireBase/Extensions/TaskExtensions.cs (limited to 'dsa/FireBase/Extensions') diff --git a/dsa/FireBase/Extensions/ObservableExtensions.cs b/dsa/FireBase/Extensions/ObservableExtensions.cs new file mode 100644 index 0000000..0a672d7 --- /dev/null +++ b/dsa/FireBase/Extensions/ObservableExtensions.cs @@ -0,0 +1,41 @@ +using System; +using System.Reactive.Linq; + +namespace Firebase.Database.Extensions +{ + public static class ObservableExtensions + { + /// + /// Returns a cold observable which retries (re-subscribes to) the source observable on error until it successfully + /// terminates. + /// + /// The source observable. + /// How long to wait between attempts. + /// A predicate determining for which exceptions to retry. Defaults to all + /// + /// A cold observable which retries (re-subscribes to) the source observable on error up to the + /// specified number of times or until it successfully terminates. + /// + public static IObservable RetryAfterDelay( + this IObservable source, + TimeSpan dueTime, + Func retryOnError) + where TException : Exception + { + var attempt = 0; + + return Observable.Defer(() => + { + return (++attempt == 1 ? source : source.DelaySubscription(dueTime)) + .Select(item => new Tuple(true, item, null)) + .Catch, TException>(e => retryOnError(e) + ? Observable.Throw>(e) + : Observable.Return(new Tuple(false, default(T), e))); + }) + .Retry() + .SelectMany(t => t.Item1 + ? Observable.Return(t.Item2) + : Observable.Throw(t.Item3)); + } + } +} \ No newline at end of file diff --git a/dsa/FireBase/Extensions/TaskExtensions.cs b/dsa/FireBase/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..c955b3a --- /dev/null +++ b/dsa/FireBase/Extensions/TaskExtensions.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; + +namespace Firebase.Database.Extensions +{ + public static class TaskExtensions + { + /// + /// Instead of unwrapping it throws it as it is. + /// + public static async Task WithAggregateException(this Task source) + { + try + { + await source.ConfigureAwait(false); + } + catch (Exception ex) + { + throw source.Exception ?? ex; + } + } + } +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf