From de0f076ef9ff546c9a90513259ad6c42cd2224b3 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sat, 29 Sep 2018 16:51:26 +0200 Subject: added firebase api --- FireBase/Extensions/ObservableExtensions.cs | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 FireBase/Extensions/ObservableExtensions.cs (limited to 'FireBase/Extensions/ObservableExtensions.cs') diff --git a/FireBase/Extensions/ObservableExtensions.cs b/FireBase/Extensions/ObservableExtensions.cs new file mode 100644 index 0000000..12cd5f3 --- /dev/null +++ b/FireBase/Extensions/ObservableExtensions.cs @@ -0,0 +1,40 @@ +namespace Firebase.Database.Extensions +{ + using System; + using System.Reactive.Linq; + + 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 + { + int 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)); + } + } +} -- cgit v1.2.3-70-g09d2