Scheduler.Services.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 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 = new Type[] {
  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. var svc = scheduler as IServiceProvider;
  67. if (svc != null)
  68. {
  69. return (T)svc.GetService(typeof(T));
  70. }
  71. return null;
  72. }
  73. }
  74. }