using System;
using System.Threading;
using System.Threading.Tasks;
using VNLib.Utils.Logging;
namespace VNLib.Plugins.Extensions.Loading.Events
{
///
/// A deletage to form a method signature for shedulable interval callbacks
///
/// The plugin's default log provider
/// The plugin's exit token
/// A task the represents the asynchronous work
public delegate Task AsyncSchedulableCallback(ILogProvider log, CancellationToken pluginExitToken);
///
/// Provides event schedueling extensions for plugins
///
public static class EventManagment
{
///
/// Schedules an asynchronous event interval for the current plugin, that is active until canceled or until the plugin unloads
///
///
/// An asyncrhonous callback method.
/// The event interval
/// An that can manage the interval state
///
/// If exceptions are raised during callback execution, they are written to the plugin's default log provider
public static EventHandle ScheduleInterval(this PluginBase plugin, AsyncSchedulableCallback asyncCallback, TimeSpan interval)
{
plugin.ThrowIfUnloaded();
plugin.Log.Verbose("Interval for {t} scheduled", interval);
//Load new event handler
return new(asyncCallback, interval, plugin);
}
///
/// Registers an type's event handler for
/// raising timed interval events
///
///
/// The instance to schedule for timeouts
/// The timeout interval
/// An that can manage the interval state
///
/// If exceptions are raised during callback execution, they are written to the plugin's default log provider
public static EventHandle ScheduleInterval(this PluginBase plugin, IIntervalScheduleable scheduleable, TimeSpan interval) =>
ScheduleInterval(plugin, scheduleable.OnIntervalAsync, interval);
}
}