blob: 1afac229374386cb89571dbabf5394984c337ce9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
using System;
using System.Threading;
using System.Threading.Tasks;
using VNLib.Utils.Logging;
namespace VNLib.Plugins.Extensions.Loading.Events
{
/// <summary>
/// A deletage to form a method signature for shedulable interval callbacks
/// </summary>
/// <param name="log">The plugin's default log provider</param>
/// <param name="pluginExitToken">The plugin's exit token</param>
/// <returns>A task the represents the asynchronous work</returns>
public delegate Task AsyncSchedulableCallback(ILogProvider log, CancellationToken pluginExitToken);
/// <summary>
/// Provides event schedueling extensions for plugins
/// </summary>
public static class EventManagment
{
/// <summary>
/// Schedules an asynchronous event interval for the current plugin, that is active until canceled or until the plugin unloads
/// </summary>
/// <param name="plugin"></param>
/// <param name="asyncCallback">An asyncrhonous callback method.</param>
/// <param name="interval">The event interval</param>
/// <returns>An <see cref="EventHandle"/> that can manage the interval state</returns>
/// <exception cref="ObjectDisposedException"></exception>
/// <remarks>If exceptions are raised during callback execution, they are written to the plugin's default log provider</remarks>
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);
}
/// <summary>
/// Registers an <see cref="IIntervalScheduleable"/> type's event handler for
/// raising timed interval events
/// </summary>
/// <param name="plugin"></param>
/// <param name="scheduleable">The instance to schedule for timeouts</param>
/// <param name="interval">The timeout interval</param>
/// <returns>An <see cref="EventHandle"/> that can manage the interval state</returns>
/// <exception cref="ObjectDisposedException"></exception>
/// <remarks>If exceptions are raised during callback execution, they are written to the plugin's default log provider</remarks>
public static EventHandle ScheduleInterval(this PluginBase plugin, IIntervalScheduleable scheduleable, TimeSpan interval) =>
ScheduleInterval(plugin, scheduleable.OnIntervalAsync, interval);
}
}
|