Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using System;
  2. using System.Reactive.Linq;
  3.  
  4. namespace RxNET.Extensions
  5. {
  6. public static class RetryWhenExtension
  7. {
  8. public static IObservable<T> RetryWhen<T>(
  9. this IObservable<T> source,
  10. Func<IObservable<Exception>, IObservable<T>> predicate)
  11. {
  12. return RetryWhenRecursive(source, predicate);
  13. }
  14.  
  15. private static IObservable<T> RetryWhenRecursive<T>(
  16. IObservable<T> source,
  17. Func<IObservable<Exception>, IObservable<T>> predicate)
  18. {
  19. return source.Catch((Exception e) =>
  20. {
  21. //
  22. // シーケンスから例外が Throw された場合、
  23. // predicate でリトライするかどうかを判定する。
  24. //
  25. return
  26. predicate(Observable.Return(e))
  27. .Catch((Exception ee) =>
  28. {
  29. //
  30. // predicate から例外が Throw された場合、
  31. // 後続のシーケンスに例外を流す(=リトライせずオブザーバーに例外を捕捉させる)。
  32. //
  33. return Observable.Throw<T>(e);
  34. })
  35. .SelectMany(_ =>
  36. {
  37. //
  38. // predicate から例外が Throw されなかった場合、
  39. // 再度、起点となったシーケンスに繋げる(=リトライ)。
  40. //
  41. return RetryWhenRecursive(source, predicate);
  42. });
  43. });
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement