using System; using System.Collections; using System.Collections.Generic; using UniRx.Operators; namespace UniRx { public static partial class Observable { public static IObservable Finally(this IObservable source, Action finallyAction) { return new FinallyObservable(source, finallyAction); } public static IObservable Catch(this IObservable source, Func> errorHandler) where TException : Exception { return new CatchObservable(source, errorHandler); } public static IObservable Catch(this IEnumerable> sources) { return new CatchObservable(sources); } /// Catch exception and return Observable.Empty. public static IObservable CatchIgnore(this IObservable source) { return source.Catch(Stubs.CatchIgnore); } /// Catch exception and return Observable.Empty. public static IObservable CatchIgnore(this IObservable source, Action errorAction) where TException : Exception { var result = source.Catch((TException ex) => { errorAction(ex); return Observable.Empty(); }); return result; } public static IObservable Retry(this IObservable source) { return RepeatInfinite(source).Catch(); } public static IObservable Retry(this IObservable source, int retryCount) { return System.Linq.Enumerable.Repeat(source, retryCount).Catch(); } /// /// Repeats the source observable sequence until it successfully terminates. /// This is same as Retry(). /// public static IObservable OnErrorRetry( this IObservable source) { var result = source.Retry(); return result; } /// /// When catched exception, do onError action and repeat observable sequence. /// public static IObservable OnErrorRetry( this IObservable source, Action onError) where TException : Exception { return source.OnErrorRetry(onError, TimeSpan.Zero); } /// /// When catched exception, do onError action and repeat observable sequence after delay time. /// public static IObservable OnErrorRetry( this IObservable source, Action onError, TimeSpan delay) where TException : Exception { return source.OnErrorRetry(onError, int.MaxValue, delay); } /// /// When catched exception, do onError action and repeat observable sequence during within retryCount. /// public static IObservable OnErrorRetry( this IObservable source, Action onError, int retryCount) where TException : Exception { return source.OnErrorRetry(onError, retryCount, TimeSpan.Zero); } /// /// When catched exception, do onError action and repeat observable sequence after delay time during within retryCount. /// public static IObservable OnErrorRetry( this IObservable source, Action onError, int retryCount, TimeSpan delay) where TException : Exception { return source.OnErrorRetry(onError, retryCount, delay, Scheduler.DefaultSchedulers.TimeBasedOperations); } /// /// When catched exception, do onError action and repeat observable sequence after delay time(work on delayScheduler) during within retryCount. /// public static IObservable OnErrorRetry( this IObservable source, Action onError, int retryCount, TimeSpan delay, IScheduler delayScheduler) where TException : Exception { var result = Observable.Defer(() => { var dueTime = (delay.Ticks < 0) ? TimeSpan.Zero : delay; var count = 0; IObservable self = null; self = source.Catch((TException ex) => { onError(ex); return (++count < retryCount) ? (dueTime == TimeSpan.Zero) ? self.SubscribeOn(Scheduler.CurrentThread) : self.DelaySubscription(dueTime, delayScheduler).SubscribeOn(Scheduler.CurrentThread) : Observable.Throw(ex); }); return self; }); return result; } } }