-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b17c0e
commit fdbe905
Showing
8 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters