Skip to content

Commit

Permalink
Add: debounce and throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Oct 16, 2024
1 parent 2b17c0e commit fdbe905
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AspNet</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AutoDependencyInjection</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Common</PackageId>
<Title>TakasakiStudio.Lina.Common</Title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Database</PackageId>
<Title>TakasakiStudio.Lina.Database</Title>
Expand Down
30 changes: 30 additions & 0 deletions TakasakiStudio.Lina.Utils/Helpers/DebounceDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace TakasakiStudio.Lina.Utils.Helpers;

/// <summary>
/// Class for manipulate debounce
/// </summary>
public static class DebounceDispatcher
{
/// <summary>
/// Debounce
/// </summary>
/// <param name="action">Function call in debounce</param>
/// <param name="interval">Interval for debounce</param>
/// <typeparam name="T">Data type</typeparam>
/// <returns>Generate function with debounce</returns>
public static Action<T> Debounce<T>(Action<T> action, TimeSpan interval)
{
var last = 0;
return arg =>
{
var current = Interlocked.Increment(ref last);
Task.Delay(interval).ContinueWith(_ =>
{
if (current == last)
{
action(arg);
}
});
};
}
}
39 changes: 39 additions & 0 deletions TakasakiStudio.Lina.Utils/Helpers/ThrottleDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace TakasakiStudio.Lina.Utils.Helpers;

/// <summary>
/// Class for manipulate throttle
/// </summary>
public static class ThrottleDispatcher
{
/// <summary>
/// Throttle
/// </summary>
/// <param name="action">Function call in throttle</param>
/// <param name="interval">Interval for throttle</param>
/// <typeparam name="T">Data type</typeparam>
/// <returns>Generate function with throttle</returns>
public static Action<T> Throttle<T>(Action<T> action, TimeSpan interval)
{
Task? task = null;
var l = new object();
T args;
return arg =>
{
args = arg;
if (task is not null)
return;

lock (l)
{
if (task is not null)
return;

task = Task.Delay(interval).ContinueWith(_ =>
{
action(args);
task = null;
});
}
};
}
}
2 changes: 1 addition & 1 deletion TakasakiStudio.Lina.Utils/TakasakiStudio.Lina.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Utils</PackageId>
<Title>TakasakiStudio.Lina.Utils</Title>
Expand Down
2 changes: 1 addition & 1 deletion TakasakiStudio.Lina/TakasakiStudio.Lina.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina</PackageId>
<Title>TakasakiStudio.Lina</Title>
Expand Down

0 comments on commit fdbe905

Please sign in to comment.