Skip to content

Instantly share code, notes, and snippets.

@Yeah69
Created May 18, 2018 17:26
Show Gist options
  • Save Yeah69/41f6c3bcfcfde7cbe53ae178a58bfbbc to your computer and use it in GitHub Desktop.
Save Yeah69/41f6c3bcfcfde7cbe53ae178a58bfbbc to your computer and use it in GitHub Desktop.
MinimalSchedule for IScheduler from Rx
public static class SchedulerExtensions
{
/// <summary>
/// Schedules an action to be executed.
/// </summary>
/// <param name="scheduler">Scheduler to execute the action on.</param>
/// <param name="action">Action to execute.</param>
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
public static IDisposable MinimalSchedule(this IScheduler scheduler, Action action)
{
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
if (action == null)
throw new ArgumentNullException(nameof(action));
return scheduler.Schedule(Unit.Default, (_, __) =>
{
action();
return Disposable.Empty;
});
}
/// <summary>
/// Schedules a func returning a non-generic task to be executed. Can be used for async lambdas.
/// </summary>
/// <param name="scheduler">Scheduler to execute the func on.</param>
/// <param name="func">Func returning a non-generic task to be executed asynchronously.</param>
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="func"/> is <c>null</c>.</exception>
public static IDisposable MinimalScheduleAsync(this IScheduler scheduler, Func<Task> func)
{
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
if (func == null)
throw new ArgumentNullException(nameof(func));
return scheduler.ScheduleAsync(Unit.Default, async (_, __, ___) =>
{
await func();
return Disposable.Empty;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment