< Summary

Information
Class: UIBlazor.Utils.Debouncer
Assembly: UIBlazor
File(s): /home/runner/work/InvAit/InvAit/UIBlazor/Utils/Debouncer.cs
Tag: 14_22728831704
Line coverage
61%
Covered lines: 11
Uncovered lines: 7
Coverable lines: 18
Total lines: 41
Line coverage: 61.1%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Trigger()100%11100%
RunLoopAsync()75%5457.14%
Dispose()100%210%

File(s)

/home/runner/work/InvAit/InvAit/UIBlazor/Utils/Debouncer.cs

#LineLine coverage
 1namespace UIBlazor.Utils;
 2
 3public sealed class Debouncer : IDisposable
 4{
 5    private readonly PeriodicTimer _timer;
 236    private readonly CancellationTokenSource _cts = new();
 7    private bool _pending;
 8    private readonly Func<Task> _action;
 9
 2310    public Debouncer(TimeSpan delay, Func<Task> action)
 11    {
 2312        _timer = new PeriodicTimer(delay);
 2313        _action = action;
 2314        _ = RunLoopAsync();
 2315    }
 16
 317    public void Trigger() => _pending = true;
 18
 19    private async Task RunLoopAsync()
 20    {
 21        try
 22        {
 3723            while (await _timer.WaitForNextTickAsync(_cts.Token))
 24            {
 1425                if (_pending)
 26                {
 327                    _pending = false;
 328                    await _action();
 29                }
 30            }
 031        }
 032        catch (OperationCanceledException) { }
 033    }
 34
 35    public void Dispose()
 36    {
 037        _cts.Cancel();
 038        _cts.Dispose();
 039        _timer.Dispose();
 040    }
 41}