< Summary

Information
Class: TeleFlow.Telegram.ChatActionLease
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/ChatActionLease.cs
Line coverage
95%
Covered lines: 21
Uncovered lines: 1
Coverable lines: 22
Total lines: 66
Line coverage: 95.4%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
DisposeAsync()100%4490.9%
RepeatAsync()100%11100%

File(s)

/_/src/TeleFlow.Framework/ChatActionLease.cs

#LineLine coverage
 1namespace TeleFlow.Telegram;
 2
 3public sealed class ChatActionLease : IAsyncDisposable
 4{
 95    private readonly CancellationTokenSource _disposeCancellation = new();
 6    private readonly CancellationTokenSource _linkedCancellation;
 7    private readonly CancellationToken _linkedToken;
 8    private readonly Task _repeatTask;
 9    private int _disposed;
 10
 11    internal ChatActionLease(
 12        ITelegramClient bot,
 13        TelegramChatActionTarget target,
 14        ChatAction action,
 15        TimeProvider timeProvider,
 16        TimeSpan repeatDelay,
 17        CancellationToken cancellationToken)
 18    {
 919        _linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource(
 920            cancellationToken,
 921            _disposeCancellation.Token);
 922        _linkedToken = _linkedCancellation.Token;
 923        _repeatTask = RepeatAsync(bot, target, action, timeProvider, repeatDelay, _linkedToken);
 924    }
 25
 26    public async ValueTask DisposeAsync()
 27    {
 1028        var shouldDispose = Interlocked.Exchange(ref _disposed, 1) == 0;
 1029        if (shouldDispose)
 30        {
 931            await _disposeCancellation.CancelAsync().ConfigureAwait(false);
 32        }
 33
 34        try
 35        {
 1036            await _repeatTask.ConfigureAwait(false);
 037        }
 938        catch (OperationCanceledException) when (_linkedToken.IsCancellationRequested)
 39        {
 940        }
 41        finally
 42        {
 1043            if (shouldDispose)
 44            {
 945                _linkedCancellation.Dispose();
 946                _disposeCancellation.Dispose();
 47            }
 48        }
 949    }
 50
 51    private static async Task RepeatAsync(
 52        ITelegramClient bot,
 53        TelegramChatActionTarget target,
 54        ChatAction action,
 55        TimeProvider timeProvider,
 56        TimeSpan repeatDelay,
 57        CancellationToken cancellationToken)
 58    {
 159        while (true)
 60        {
 1061            await Task.Delay(repeatDelay, timeProvider, cancellationToken).ConfigureAwait(false);
 262            cancellationToken.ThrowIfCancellationRequested();
 263            await ChatActions.SendChatActionAsync(bot, target, action, cancellationToken).ConfigureAwait(false);
 64        }
 65    }
 66}