mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:16:30 +00:00
16 lines
515 B
C#
16 lines
515 B
C#
namespace Azaion.Annotator.Extensions;
|
|
|
|
public static class FuncExtensions
|
|
{
|
|
private static CancellationTokenSource? _lastCToken;
|
|
|
|
public static Action Debounce(this Action func, TimeSpan? throttleTime = null) =>
|
|
() =>
|
|
{
|
|
_lastCToken?.Cancel();
|
|
|
|
var tokenSrc = _lastCToken = new CancellationTokenSource();
|
|
Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500), tokenSrc.Token)
|
|
.ContinueWith(_ => func(), tokenSrc.Token);
|
|
};
|
|
} |