// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. namespace System.Reactive.Concurrency { // // NOTE: When adding interface-based optimizations here, ensure to add the type to the list of // interface-based optimizations used by DisableOptimizations and the RawScheduler type. // public static partial class Scheduler { internal static Type[] Optimizations = { typeof(ISchedulerLongRunning), typeof(IStopwatchProvider), typeof(ISchedulerPeriodic) /* update this list if new interface-based optimizations are added */ }; /// /// Returns the implementation of the specified scheduler, or null if no such implementation is available. /// /// Scheduler to get the implementation for. /// The scheduler's implementation if available; null otherwise. /// /// This helper method is made available for query operator authors in order to discover scheduler services by using the required /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. /// public static ISchedulerLongRunning? AsLongRunning(this IScheduler scheduler) => As(scheduler); /// /// Returns the implementation of the specified scheduler, or null if no such implementation is available. /// /// Scheduler to get the implementation for. /// The scheduler's implementation if available; null otherwise. /// /// /// This helper method is made available for query operator authors in order to discover scheduler services by using the required /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. /// /// /// Consider using in case a stopwatch is required, but use of emulation stopwatch based /// on the scheduler's clock is acceptable. Use of this method is recommended for best-effort use of the stopwatch provider /// scheduler service, where the caller falls back to not using stopwatches if this facility wasn't found. /// /// public static IStopwatchProvider? AsStopwatchProvider(this IScheduler scheduler) => As(scheduler); /// /// Returns the implementation of the specified scheduler, or null if no such implementation is available. /// /// Scheduler to get the implementation for. /// The scheduler's implementation if available; null otherwise. /// /// /// This helper method is made available for query operator authors in order to discover scheduler services by using the required /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. /// /// /// Consider using the extension methods for in case periodic scheduling /// is required and emulation of periodic behavior using other scheduler services is desirable. Use of this method is recommended /// for best-effort use of the periodic scheduling service, where the caller falls back to not using periodic scheduling if this /// facility wasn't found. /// /// public static ISchedulerPeriodic? AsPeriodic(this IScheduler scheduler) => As(scheduler); private static T? As(IScheduler scheduler) where T : class { if (scheduler is IServiceProvider svc) { return (T?)svc.GetService(typeof(T)); } return null; } } }