Scheduler.Services.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Concurrency
  5. {
  6. //
  7. // NOTE: When adding interface-based optimizations here, ensure to add the type to the list of
  8. // interface-based optimizations used by DisableOptimizations and the RawScheduler type.
  9. //
  10. public static partial class Scheduler
  11. {
  12. internal static Type[] Optimizations = {
  13. typeof(ISchedulerLongRunning),
  14. typeof(IStopwatchProvider),
  15. typeof(ISchedulerPeriodic)
  16. /* update this list if new interface-based optimizations are added */
  17. };
  18. /// <summary>
  19. /// Returns the <see cref="ISchedulerLongRunning"/> implementation of the specified scheduler, or <c>null</c> if no such implementation is available.
  20. /// </summary>
  21. /// <param name="scheduler">Scheduler to get the <see cref="ISchedulerLongRunning"/> implementation for.</param>
  22. /// <returns>The scheduler's <see cref="ISchedulerLongRunning"/> implementation if available; <c>null</c> otherwise.</returns>
  23. /// <remarks>
  24. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  25. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  26. /// </remarks>
  27. public static ISchedulerLongRunning? AsLongRunning(this IScheduler scheduler) => As<ISchedulerLongRunning>(scheduler);
  28. /// <summary>
  29. /// Returns the <see cref="IStopwatchProvider"/> implementation of the specified scheduler, or <c>null</c> if no such implementation is available.
  30. /// </summary>
  31. /// <param name="scheduler">Scheduler to get the <see cref="IStopwatchProvider"/> implementation for.</param>
  32. /// <returns>The scheduler's <see cref="IStopwatchProvider"/> implementation if available; <c>null</c> otherwise.</returns>
  33. /// <remarks>
  34. /// <para>
  35. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  36. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  37. /// </para>
  38. /// <para>
  39. /// Consider using <see cref="StartStopwatch"/> in case a stopwatch is required, but use of emulation stopwatch based
  40. /// on the scheduler's clock is acceptable. Use of this method is recommended for best-effort use of the stopwatch provider
  41. /// scheduler service, where the caller falls back to not using stopwatches if this facility wasn't found.
  42. /// </para>
  43. /// </remarks>
  44. public static IStopwatchProvider? AsStopwatchProvider(this IScheduler scheduler) => As<IStopwatchProvider>(scheduler);
  45. /// <summary>
  46. /// Returns the <see cref="ISchedulerPeriodic"/> implementation of the specified scheduler, or <c>null</c> if no such implementation is available.
  47. /// </summary>
  48. /// <param name="scheduler">Scheduler to get the <see cref="ISchedulerPeriodic"/> implementation for.</param>
  49. /// <returns>The scheduler's <see cref="ISchedulerPeriodic"/> implementation if available; <c>null</c> otherwise.</returns>
  50. /// <remarks>
  51. /// <para>
  52. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  53. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  54. /// </para>
  55. /// <para>
  56. /// Consider using the <see cref="SchedulePeriodic"/> extension methods for <see cref="IScheduler"/> in case periodic scheduling
  57. /// is required and emulation of periodic behavior using other scheduler services is desirable. Use of this method is recommended
  58. /// for best-effort use of the periodic scheduling service, where the caller falls back to not using periodic scheduling if this
  59. /// facility wasn't found.
  60. /// </para>
  61. /// </remarks>
  62. public static ISchedulerPeriodic? AsPeriodic(this IScheduler scheduler) => As<ISchedulerPeriodic>(scheduler);
  63. private static T? As<T>(IScheduler scheduler)
  64. where T : class
  65. {
  66. if (scheduler is IServiceProvider svc)
  67. {
  68. return (T?)svc.GetService(typeof(T));
  69. }
  70. return null;
  71. }
  72. }
  73. }