< Summary

Information
Class: TeleFlow.Storage.Memory.MemoryStateHistoryStore
Assembly: TeleFlow.Storage.Memory
File(s): /_/src/TeleFlow.Storage.Memory/MemoryStateHistoryStore.cs
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 119
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
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%
GetHistoryAsync(...)100%22100%
PushAsync(...)100%22100%
PopAsync(...)100%44100%
PeekAsync(...)100%22100%
ClearAsync(...)100%11100%
TryPeek(...)100%44100%

File(s)

/_/src/TeleFlow.Storage.Memory/MemoryStateHistoryStore.cs

#LineLine coverage
 1using TeleFlow.Framework.States;
 2
 3namespace TeleFlow.Storage.Memory;
 4
 5public sealed class MemoryStateHistoryStore : IStateHistoryStore
 6{
 317    private readonly object _gate = new();
 318    private readonly Dictionary<StateKey, List<string>> _history = [];
 9
 10    public ValueTask<IReadOnlyList<string>> GetHistoryAsync(
 11        StateKey key,
 12        CancellationToken cancellationToken = default)
 13    {
 1814        cancellationToken.ThrowIfCancellationRequested();
 15
 1716        lock (_gate)
 17        {
 1718            if (!_history.TryGetValue(key, out var history))
 19            {
 720                return ValueTask.FromResult<IReadOnlyList<string>>([]);
 21            }
 22
 1023            return ValueTask.FromResult<IReadOnlyList<string>>(history.ToArray());
 24        }
 1725    }
 26
 27    public ValueTask PushAsync(
 28        StateKey key,
 29        string stateId,
 30        CancellationToken cancellationToken = default)
 31    {
 1432        cancellationToken.ThrowIfCancellationRequested();
 1333        ArgumentException.ThrowIfNullOrWhiteSpace(stateId);
 34
 1235        lock (_gate)
 36        {
 1237            if (!_history.TryGetValue(key, out var history))
 38            {
 1039                history = [];
 1040                _history.Add(key, history);
 41            }
 42
 1243            history.Add(stateId);
 1244        }
 45
 1246        return ValueTask.CompletedTask;
 47    }
 48
 49    public ValueTask<string?> PopAsync(
 50        StateKey key,
 51        CancellationToken cancellationToken = default)
 52    {
 753        cancellationToken.ThrowIfCancellationRequested();
 54
 655        lock (_gate)
 56        {
 657            if (!TryPeek(key, out var history, out var state))
 58            {
 259                return ValueTask.FromResult<string?>(null);
 60            }
 61
 462            history.RemoveAt(history.Count - 1);
 63
 464            if (history.Count == 0)
 65            {
 366                _history.Remove(key);
 67            }
 68
 469            return ValueTask.FromResult<string?>(state);
 70        }
 671    }
 72
 73    public ValueTask<string?> PeekAsync(
 74        StateKey key,
 75        CancellationToken cancellationToken = default)
 76    {
 977        cancellationToken.ThrowIfCancellationRequested();
 78
 879        lock (_gate)
 80        {
 881            if (!TryPeek(key, out _, out var state))
 82            {
 383                return ValueTask.FromResult<string?>(null);
 84            }
 85
 586            return ValueTask.FromResult<string?>(state);
 87        }
 888    }
 89
 90    public ValueTask ClearAsync(
 91        StateKey key,
 92        CancellationToken cancellationToken = default)
 93    {
 594        cancellationToken.ThrowIfCancellationRequested();
 95
 496        lock (_gate)
 97        {
 498            _history.Remove(key);
 499        }
 100
 4101        return ValueTask.CompletedTask;
 102    }
 103
 104    private bool TryPeek(
 105        StateKey key,
 106        out List<string> history,
 107        out string? state)
 108    {
 14109        if (!_history.TryGetValue(key, out history!) ||
 14110            history.Count == 0)
 111        {
 5112            state = null;
 5113            return false;
 114        }
 115
 9116        state = history[^1];
 9117        return true;
 118    }
 119}