mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:46:30 +00:00
961d2499de
fix small issues
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
namespace Azaion.Common.Extensions;
|
|
|
|
public static class ThrottleExt
|
|
{
|
|
private static bool _throttleRunFirstOn;
|
|
public static async Task ThrottleRunFirst(this Func<Task> func, TimeSpan? throttleTime = null, CancellationToken cancellationToken = default)
|
|
{
|
|
if (_throttleRunFirstOn)
|
|
return;
|
|
|
|
_throttleRunFirstOn = true;
|
|
await func();
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500), cancellationToken);
|
|
_throttleRunFirstOn = false;
|
|
}, cancellationToken);
|
|
}
|
|
|
|
private static bool _throttleRunAfter;
|
|
public static async Task ThrottleRunAfter(this Func<Task> func, TimeSpan? throttleTime = null, CancellationToken cancellationToken = default)
|
|
{
|
|
if (_throttleRunAfter)
|
|
return;
|
|
|
|
_throttleRunAfter = true;
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500), cancellationToken);
|
|
await func();
|
|
_throttleRunAfter = false;
|
|
}, cancellationToken);
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
} |