// 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.
using System.Reactive.Disposables;
namespace System.Reactive.Concurrency
{
///
/// Provides a set of extension methods for virtual time scheduling.
///
public static class VirtualTimeSchedulerExtensions
{
///
/// Schedules an action to be executed at .
///
/// Absolute time representation type.
/// Relative time representation type.
/// Scheduler to execute the action on.
/// Relative time after which to execute the action.
/// Action to be executed.
/// The disposable object used to cancel the scheduled action (best effort).
/// or is null.
public static IDisposable ScheduleRelative(this VirtualTimeSchedulerBase scheduler, TRelative dueTime, Action action)
where TAbsolute : IComparable
{
if (scheduler == null)
{
throw new ArgumentNullException(nameof(scheduler));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
// As stated in Scheduler.Simple.cs,
// an anonymous delegate will allow delegate caching.
// Watch https://github.com/dotnet/roslyn/issues/5835 for compiler
// support for caching delegates from method groups.
return scheduler.ScheduleRelative(action, dueTime, static (_, a) => Invoke(a));
}
///
/// Schedules an action to be executed at .
///
/// Absolute time representation type.
/// Relative time representation type.
/// Scheduler to execute the action on.
/// Absolute time at which to execute the action.
/// Action to be executed.
/// The disposable object used to cancel the scheduled action (best effort).
/// or is null.
public static IDisposable ScheduleAbsolute(this VirtualTimeSchedulerBase scheduler, TAbsolute dueTime, Action action)
where TAbsolute : IComparable
{
if (scheduler == null)
{
throw new ArgumentNullException(nameof(scheduler));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
return scheduler.ScheduleAbsolute(action, dueTime, static (_, a) => Invoke(a));
}
private static IDisposable Invoke(Action action)
{
action();
return Disposable.Empty;
}
}
}