update/rename notifyafterdelay to ExecuteAfterDelay

This commit is contained in:
RaidMax 2023-04-09 14:07:50 -05:00
parent 22af762a9d
commit 5d53c2559b

View File

@ -25,6 +25,7 @@ using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Localization; using SharedLibraryCore.Localization;
using SharedLibraryCore.RCon; using SharedLibraryCore.RCon;
using static System.Threading.Tasks.Task;
using static SharedLibraryCore.Server; using static SharedLibraryCore.Server;
using static Data.Models.Client.EFClient; using static Data.Models.Client.EFClient;
using static Data.Models.EFPenalty; using static Data.Models.EFPenalty;
@ -886,7 +887,7 @@ namespace SharedLibraryCore
{ {
if (delay != null) if (delay != null)
{ {
await Task.Delay(delay.Value); await Delay(delay.Value);
} }
var response = await server.RemoteConnection.SendQueryAsync(StaticHelpers.QueryType.GET_INFO); var response = await server.RemoteConnection.SendQueryAsync(StaticHelpers.QueryType.GET_INFO);
@ -1053,7 +1054,7 @@ namespace SharedLibraryCore
public static async Task WithWaitCancellation(this Task task, public static async Task WithWaitCancellation(this Task task,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var completedTask = await Task.WhenAny(task, Task.Delay(Timeout.Infinite, cancellationToken)); var completedTask = await WhenAny(task, Delay(Timeout.Infinite, cancellationToken));
if (completedTask == task) if (completedTask == task)
{ {
await task; await task;
@ -1068,7 +1069,7 @@ namespace SharedLibraryCore
public static async Task<T> WithWaitCancellation<T>(this Task<T> task, public static async Task<T> WithWaitCancellation<T>(this Task<T> task,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var completedTask = await Task.WhenAny(task, Task.Delay(Timeout.Infinite, cancellationToken)); var completedTask = await WhenAny(task, Delay(Timeout.Infinite, cancellationToken));
if (completedTask == task) if (completedTask == task)
{ {
return await task; return await task;
@ -1080,13 +1081,13 @@ namespace SharedLibraryCore
public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout) public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout)
{ {
await Task.WhenAny(task, Task.Delay(timeout)); await WhenAny(task, Delay(timeout));
return await task; return await task;
} }
public static async Task WithTimeout(this Task task, TimeSpan timeout) public static async Task WithTimeout(this Task task, TimeSpan timeout)
{ {
await Task.WhenAny(task, Task.Delay(timeout)); await WhenAny(task, Delay(timeout));
} }
public static bool ShouldHideLevel(this Permission perm) public static bool ShouldHideLevel(this Permission perm)
@ -1303,7 +1304,7 @@ namespace SharedLibraryCore
serviceProvider.GetRequiredService<IConfigurationHandlerV2<TConfigurationType>>(); serviceProvider.GetRequiredService<IConfigurationHandlerV2<TConfigurationType>>();
var configuration = var configuration =
Task.Run(() => configurationHandler.Get(fileName ?? typeof(TConfigurationType).Name, defaultConfig)) Run(() => configurationHandler.Get(fileName ?? typeof(TConfigurationType).Name, defaultConfig))
.GetAwaiter().GetResult(); .GetAwaiter().GetResult();
if (typeof(TConfigurationType).GetInterface(nameof(IBaseConfiguration)) is not null && if (typeof(TConfigurationType).GetInterface(nameof(IBaseConfiguration)) is not null &&
@ -1316,7 +1317,7 @@ namespace SharedLibraryCore
if (defaultConfig is not null && configuration is null) if (defaultConfig is not null && configuration is null)
{ {
Task.Run(() => configurationHandler.Set(defaultConfig)).GetAwaiter().GetResult(); Run(() => configurationHandler.Set(defaultConfig)).GetAwaiter().GetResult();
configuration = defaultConfig; configuration = defaultConfig;
} }
@ -1332,17 +1333,20 @@ namespace SharedLibraryCore
return serviceCollection; return serviceCollection;
} }
public static void NotifyAfterDelay(TimeSpan duration, Func<Task> action) => public static void ExecuteAfterDelay(TimeSpan duration, Func<CancellationToken, Task> action, CancellationToken token = default) =>
NotifyAfterDelay((int)duration.TotalMilliseconds, action); ExecuteAfterDelay((int)duration.TotalMilliseconds, action, token);
public static void NotifyAfterDelay(int delayMs, Func<Task> action) public static void ExecuteAfterDelay(int delayMs, Func<CancellationToken, Task> action, CancellationToken token = default)
{ {
Task.Run(async () => // ReSharper disable once MethodSupportsCancellation
#pragma warning disable CA2016
_ = Run(async () =>
#pragma warning restore CA2016
{ {
try try
{ {
await Task.Delay(delayMs); await Delay(delayMs, token);
await action(); await action(token);
} }
catch catch
{ {
@ -1350,5 +1354,8 @@ namespace SharedLibraryCore
} }
}); });
} }
public static void ExecuteAfterDelay(this Func<CancellationToken, Task> action, int delayMs,
CancellationToken token = default) => ExecuteAfterDelay(delayMs, action, token);
} }
} }