CatchScheduler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Disposables;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Reactive.Concurrency
  7. {
  8. internal sealed class CatchScheduler<TException> : SchedulerWrapper
  9. where TException : Exception
  10. {
  11. private readonly Func<TException, bool> _handler;
  12. public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler)
  13. : base(scheduler)
  14. {
  15. _handler = handler;
  16. }
  17. protected override Func<IScheduler, TState, IDisposable> Wrap<TState>(Func<IScheduler, TState, IDisposable> action)
  18. {
  19. return (self, state) =>
  20. {
  21. try
  22. {
  23. return action(GetRecursiveWrapper(self), state);
  24. }
  25. catch (TException exception) when (_handler(exception))
  26. {
  27. return Disposable.Empty;
  28. }
  29. };
  30. }
  31. public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler, ConditionalWeakTable<IScheduler, IScheduler> cache)
  32. : base(scheduler, cache)
  33. {
  34. _handler = handler;
  35. }
  36. protected override SchedulerWrapper Clone(IScheduler scheduler, ConditionalWeakTable<IScheduler, IScheduler> cache)
  37. {
  38. return new CatchScheduler<TException>(scheduler, _handler, cache);
  39. }
  40. protected override bool TryGetService(IServiceProvider provider, Type serviceType, out object service)
  41. {
  42. service = provider.GetService(serviceType);
  43. if (service != null)
  44. {
  45. if (serviceType == typeof(ISchedulerLongRunning))
  46. {
  47. service = new CatchSchedulerLongRunning((ISchedulerLongRunning)service, _handler);
  48. }
  49. else if (serviceType == typeof(ISchedulerPeriodic))
  50. {
  51. service = new CatchSchedulerPeriodic((ISchedulerPeriodic)service, _handler);
  52. }
  53. }
  54. return true;
  55. }
  56. private class CatchSchedulerLongRunning : ISchedulerLongRunning
  57. {
  58. private readonly ISchedulerLongRunning _scheduler;
  59. private readonly Func<TException, bool> _handler;
  60. public CatchSchedulerLongRunning(ISchedulerLongRunning scheduler, Func<TException, bool> handler)
  61. {
  62. _scheduler = scheduler;
  63. _handler = handler;
  64. }
  65. public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action)
  66. {
  67. return _scheduler.ScheduleLongRunning(state, (state_, cancel) =>
  68. {
  69. try
  70. {
  71. action(state_, cancel);
  72. }
  73. catch (TException exception) when (_handler(exception))
  74. {
  75. }
  76. });
  77. }
  78. }
  79. private sealed class CatchSchedulerPeriodic : ISchedulerPeriodic
  80. {
  81. private readonly ISchedulerPeriodic _scheduler;
  82. private readonly Func<TException, bool> _handler;
  83. public CatchSchedulerPeriodic(ISchedulerPeriodic scheduler, Func<TException, bool> handler)
  84. {
  85. _scheduler = scheduler;
  86. _handler = handler;
  87. }
  88. public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
  89. {
  90. var failed = false;
  91. var d = new SingleAssignmentDisposable();
  92. d.Disposable = _scheduler.SchedulePeriodic(state, period, state_ =>
  93. {
  94. //
  95. // Cancellation may not be granted immediately; prevent from running user
  96. // code in that case. Periodic schedulers are assumed to introduce some
  97. // degree of concurrency, so we should return from the SchedulePeriodic
  98. // call eventually, allowing the d.Dispose() call in the catch block to
  99. // take effect.
  100. //
  101. if (failed)
  102. return default(TState);
  103. try
  104. {
  105. return action(state_);
  106. }
  107. catch (TException exception)
  108. {
  109. failed = true;
  110. if (!_handler(exception))
  111. throw;
  112. d.Dispose();
  113. return default(TState);
  114. }
  115. });
  116. return d;
  117. }
  118. }
  119. }
  120. }