< Summary

Information
Class: UIBlazor.Utils.Throttler
Assembly: UIBlazor
File(s): /home/runner/work/InvAit/InvAit/UIBlazor/Utils/Throttler.cs
Tag: 71_26091983037
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 49
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ShouldRender(...)0%7280%
Dispose()100%210%

File(s)

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

#LineLine coverage
 1namespace UIBlazor.Utils;
 2
 03public class Throttler(int intervalMs) : IDisposable
 4{
 05    private DateTime _lastExecution = DateTime.MinValue;
 6    private bool _hasPendingWork;
 7    private bool _isWaiting;
 08    private readonly CancellationTokenSource _cts = new();
 9
 10    public bool ShouldRender(Action onTailUpdate)
 11    {
 012        var elapsed = (DateTime.Now - _lastExecution).TotalMilliseconds;
 13
 014        if (elapsed >= intervalMs)
 15        {
 016            _lastExecution = DateTime.Now;
 017            _hasPendingWork = false;
 018            return true;
 19        }
 20
 021        _hasPendingWork = true;
 22
 023        if (!_isWaiting)
 24        {
 025            _isWaiting = true;
 026            var token = _cts.Token;
 27
 028            _ = Task.Delay(intervalMs, token).ContinueWith(t =>
 029            {
 030                if (t.IsCanceled) return;
 031
 032                _isWaiting = false;
 033                if (_hasPendingWork)
 034                {
 035                    _hasPendingWork = false;
 036                    onTailUpdate();
 037                }
 038            }, token);
 39        }
 40
 041        return false;
 42    }
 43
 44    public void Dispose()
 45    {
 046        _cts.Cancel();
 047        _cts.Dispose();
 048    }
 49}