Scheduler.Wrappers.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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;
  5. namespace System.Reactive.Concurrency
  6. {
  7. public static partial class Scheduler
  8. {
  9. /// <summary>
  10. /// Returns a scheduler that represents the original scheduler, without any of its interface-based optimizations (e.g. long running scheduling).
  11. /// </summary>
  12. /// <param name="scheduler">Scheduler to disable all optimizations for.</param>
  13. /// <returns>Proxy to the original scheduler but without any optimizations enabled.</returns>
  14. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  15. public static IScheduler DisableOptimizations(this IScheduler scheduler)
  16. {
  17. if (scheduler == null)
  18. throw new ArgumentNullException(nameof(scheduler));
  19. return new DisableOptimizationsScheduler(scheduler);
  20. }
  21. /// <summary>
  22. /// Returns a scheduler that represents the original scheduler, without the specified set of interface-based optimizations (e.g. long running scheduling).
  23. /// </summary>
  24. /// <param name="scheduler">Scheduler to disable the specified optimizations for.</param>
  25. /// <param name="optimizationInterfaces">Types of the optimization interfaces that have to be disabled.</param>
  26. /// <returns>Proxy to the original scheduler but without the specified optimizations enabled.</returns>
  27. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="optimizationInterfaces"/> is null.</exception>
  28. public static IScheduler DisableOptimizations(this IScheduler scheduler, params Type[] optimizationInterfaces)
  29. {
  30. if (scheduler == null)
  31. throw new ArgumentNullException(nameof(scheduler));
  32. if (optimizationInterfaces == null)
  33. throw new ArgumentNullException(nameof(optimizationInterfaces));
  34. return new DisableOptimizationsScheduler(scheduler, optimizationInterfaces);
  35. }
  36. /// <summary>
  37. /// Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
  38. /// </summary>
  39. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  40. /// <param name="scheduler">Scheduler to apply an exception filter for.</param>
  41. /// <param name="handler">Handler that's run if an exception is caught. The exception will be rethrown if the handler returns false.</param>
  42. /// <returns>Wrapper around the original scheduler, enforcing exception handling.</returns>
  43. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="handler"/> is null.</exception>
  44. public static IScheduler Catch<TException>(this IScheduler scheduler, Func<TException, bool> handler)
  45. where TException : Exception
  46. {
  47. if (scheduler == null)
  48. throw new ArgumentNullException(nameof(scheduler));
  49. if (handler == null)
  50. throw new ArgumentNullException(nameof(handler));
  51. return new CatchScheduler<TException>(scheduler, handler);
  52. }
  53. }
  54. }