JsStyle.MethodControl
1.0.0
dotnet add package JsStyle.MethodControl --version 1.0.0
NuGet\Install-Package JsStyle.MethodControl -Version 1.0.0
<PackageReference Include="JsStyle.MethodControl" Version="1.0.0" />
<PackageVersion Include="JsStyle.MethodControl" Version="1.0.0" />
<PackageReference Include="JsStyle.MethodControl" />
paket add JsStyle.MethodControl --version 1.0.0
#r "nuget: JsStyle.MethodControl, 1.0.0"
#:package JsStyle.MethodControl@1.0.0
#addin nuget:?package=JsStyle.MethodControl&version=1.0.0
#tool nuget:?package=JsStyle.MethodControl&version=1.0.0
JsStyle.MethodControl
Provides some common rate-limit actions and funcs in Js, such as throttle and debounce. It also encapsulates the set/clearTimeout/Interval series of methods into asynchronous and cancelable ExecuteAfter and ExecuteRepeatedly.
Usage
TryFunc
For the encapsulation of methods with return values, since the typical application scenario is high-frequency calls, a more logical design of throwing an exception when the execution is unsuccessful is not adopted. Instead, a bool value similar to the return value of TryXXX is returned to indicate the successful execution status.
Different overloaded versions with up to sixteen parameters and all defined in JsStyle.MethodControl.Global
delegate bool TryFunc<TResult>(out TResult result);
delegate bool TryFunc<T0, TResult>(T0 arg0, out TResult result);
// ...
Debouncer
The Debounce delays the action until the specified interval has passed without any new calls. This is useful for scenarios like user input where you want to wait until the user has finished typing before processing.
These are all defined in JsStyle.MethodControl.Debouncer
Debouncer API
The basic interface is similar to the following, like Action/Func, it provides different overloaded versions with up to sixteen parameters.
Action<T0> AsDebounce<T0>(this Action<T0> func, TimeSpan delay);
Action AsDebounce(this Action func, TimeSpan delay);
// ...
TryFunc<T0, TResult> AsDebounce<T0, TResult>(this Func<T0, TResult> func, TimeSpan delay);
TryFunc<TResult> AsDebounce<TResult>(this Func<TResult> func, TimeSpan delay);
// ...
Throttler
The Throttle ensures the action is only executed once within the specified interval, regardless of how many times it is called. This is useful for rate-limiting operations like API calls.
These are all defined in JsStyle.MethodControl.Throttler
Throttler API
Basically the same as Debouncer
Action AsThrottle(this Action func, TimeSpan delay);
Action<T0> AsThrottle<T0>(this Action<T0> func, TimeSpan delay);
// ...
TryFunc<TResult> AsThrottle<TResult>(this Func<TResult> func, TimeSpan delay);
TryFunc<T0, TResult> AsThrottle<T0, TResult>(this Func<T0, TResult> func,TimeSpan delay);
// ...
ExecuteExtension
ExecuteAfter and ExecuteRepeatedly correspond to set/clearTimeOut and set/clearInterval in Js respectively. According to their intentions, they are all defined as more logical asynchronous methods and support passing in CancellationToken to cancel tasks. In addition, ExecuteRepeatedly is given the function of specifying the maximum number of executions. When the parameter is a negative number, it means that it will be executed forever (there is no special consideration for using int, but it feels that there should be no demand that exceeds the int range...)
async Task ExecuteAfter(this Action func, TimeSpan delay, CancellationToken token = default);
async Task ExecuteAfter<T0>(this Action<T0> func, TimeSpan delay, T0 arg0, CancellationToken token = default);
// ...
async Task ExecuteRepeatedly(Action func, TimeSpan delay, int maxExecuteCount = -1, CancellationToken token = default);
async Task ExecuteRepeatedly<T0>(Action<T0> func, TimeSpan delay, T0 arg0, int maxExecuteCount = -1, CancellationToken token = default);
// ...
There is no Func related overload, just because I think there are not many such needs.
If you want to achieve a similar effect, you can use closure or parameter passing to pass in a List or similar object and store the return value in it
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 444 | 12/25/2024 |