// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 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 dueTime.
///
/// 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));
return scheduler.ScheduleRelative(action, dueTime, Invoke);
}
///
/// Schedules an action to be executed at dueTime.
///
/// 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, Invoke);
}
static IDisposable Invoke(IScheduler scheduler, Action action)
{
action();
return Disposable.Empty;
}
}
}