// Copyright © WireMock.Net
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
namespace WireMock.Handlers;
///
/// Provides an in-memory implementation of the interface for managing scenario state objects by name.
///
public class InMemoryScenarioStateStore : IScenarioStateStore
{
private readonly ConcurrentDictionary _scenarios = new(StringComparer.OrdinalIgnoreCase);
///
public bool TryGet(string name, [NotNullWhen(true)] out ScenarioState? state)
{
return _scenarios.TryGetValue(name, out state);
}
///
public IReadOnlyList GetAll()
{
return _scenarios.Values.ToArray();
}
///
public bool ContainsKey(string name)
{
return _scenarios.ContainsKey(name);
}
///
public bool TryAdd(string name, ScenarioState scenarioState)
{
return _scenarios.TryAdd(name, scenarioState);
}
///
public ScenarioState AddOrUpdate(string name, Func addFactory, Func updateFactory)
{
return _scenarios.AddOrUpdate(name, addFactory, updateFactory);
}
///
public ScenarioState? Update(string name, Action updateAction)
{
if (_scenarios.TryGetValue(name, out var state))
{
updateAction(state);
return state;
}
return null;
}
///
public bool TryRemove(string name)
{
return _scenarios.TryRemove(name, out _);
}
///
public void Clear()
{
_scenarios.Clear();
}
}