* minor changes to state keepers

This commit is contained in:
2025-08-13 00:58:31 +04:00
parent ccfefa578d
commit cbfb1d4e8d
4 changed files with 38 additions and 20 deletions
+1
View File
@@ -362,3 +362,4 @@ MigrationBackup/
# Fody - auto-generated XML schema # Fody - auto-generated XML schema
FodyWeavers.xsd FodyWeavers.xsd
/GETTING_STARTED.md /GETTING_STARTED.md
/ANNOTATION_OVERVIEW.md
+1 -1
View File
@@ -110,7 +110,7 @@ namespace Telegrator.Handlers
/// <param name="url">A URL that will be opened by the client.</param> /// <param name="url">A URL that will be opened by the client.</param>
/// <param name="cacheTime">The maximum amount of time in seconds that the result of the callback query may be cached client-side.</param> /// <param name="cacheTime">The maximum amount of time in seconds that the result of the callback query may be cached client-side.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
protected async Task AnswerCallbackQuery( protected async Task Answer(
string? text = null, string? text = null,
bool showAlert = false, bool showAlert = false,
string? url = null, string? url = null,
@@ -26,7 +26,7 @@ namespace Telegrator.StateKeeping.Components
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
/// <param name="newState">The new state value.</param> /// <param name="newState">The new state value.</param>
public void SetState(Update keySource, TState newState) public virtual void SetState(Update keySource, TState newState)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
States.Set(key, newState, DefaultState); States.Set(key, newState, DefaultState);
@@ -37,7 +37,7 @@ namespace Telegrator.StateKeeping.Components
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
/// <returns>The state value.</returns> /// <returns>The state value.</returns>
public TState GetState(Update keySource) public virtual TState GetState(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
return States[key]; return States[key];
@@ -49,7 +49,7 @@ namespace Telegrator.StateKeeping.Components
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
/// <param name="state">When this method returns, contains the state value if found; otherwise, the default value.</param> /// <param name="state">When this method returns, contains the state value if found; otherwise, the default value.</param>
/// <returns>True if the state was found; otherwise, false.</returns> /// <returns>True if the state was found; otherwise, false.</returns>
public bool TryGetState(Update keySource, out TState? state) public virtual bool TryGetState(Update keySource, out TState? state)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
return States.TryGetValue(key, out state); return States.TryGetValue(key, out state);
@@ -60,7 +60,7 @@ namespace Telegrator.StateKeeping.Components
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
/// <returns>True if the state exists; otherwise, false.</returns> /// <returns>True if the state exists; otherwise, false.</returns>
public bool HasState(Update keySource) public virtual bool HasState(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
return States.ContainsKey(key); return States.ContainsKey(key);
@@ -70,7 +70,7 @@ namespace Telegrator.StateKeeping.Components
/// Creates a state for the specified update using the default state value. /// Creates a state for the specified update using the default state value.
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
public void CreateState(Update keySource) public virtual void CreateState(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
States.Set(key, DefaultState); States.Set(key, DefaultState);
@@ -80,7 +80,7 @@ namespace Telegrator.StateKeeping.Components
/// Deletes the state for the specified update. /// Deletes the state for the specified update.
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
public void DeleteState(Update keySource) public virtual void DeleteState(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
States.Remove(key); States.Remove(key);
@@ -90,7 +90,7 @@ namespace Telegrator.StateKeeping.Components
/// Moves the state forward for the specified update. /// Moves the state forward for the specified update.
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
public void MoveForward(Update keySource) public virtual void MoveForward(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
if (!States.TryGetValue(key, out TState currentState)) if (!States.TryGetValue(key, out TState currentState))
@@ -107,22 +107,29 @@ namespace Telegrator.StateKeeping.Components
/// Moves the state backward for the specified update. /// Moves the state backward for the specified update.
/// </summary> /// </summary>
/// <param name="keySource">The update to use as a key source.</param> /// <param name="keySource">The update to use as a key source.</param>
public void MoveBackward(Update keySource) public virtual void MoveBackward(Update keySource)
{ {
TKey key = KeyResolver.ResolveKey(keySource); TKey key = KeyResolver.ResolveKey(keySource);
TState currentState = States[key]; if (!States.TryGetValue(key, out TState currentState))
{
States.Set(key, DefaultState);
return;
}
TState newState = MoveBackward(currentState, key); TState newState = MoveBackward(currentState, key);
States[key] = newState; States[key] = newState;
} }
/*
/// <summary> /// <summary>
/// Gets the state keeper for the specified key. /// Gets the state keeper for the specified key.
/// </summary> /// </summary>
/// <typeparam name="TStateKeeper">The type of the state keeper.</typeparam> /// <typeparam name="TStateKeeper">The type of the state keeper.</typeparam>
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <returns>The state keeper instance.</returns> /// <returns>The state keeper instance.</returns>
protected TStateKeeper GetKeeper<TStateKeeper>(TKey key) where TStateKeeper : StateKeeperBase<TKey, TState> protected virtual TStateKeeper GetKeeper<TStateKeeper>(TKey key) where TStateKeeper : StateKeeperBase<TKey, TState>
=> States[key] as TStateKeeper ?? throw new InvalidCastException(); => States[key] as TStateKeeper ?? throw new InvalidCastException();
*/
/// <summary> /// <summary>
/// Moves the state forward for the specified current state and key. /// Moves the state forward for the specified current state and key.
+19 -9
View File
@@ -1,25 +1,30 @@
using Telegrator.Annotations.StateKeeping; using Telegrator.Annotations.StateKeeping;
using Telegrator.Handlers.Components; using Telegrator.Handlers.Components;
using Telegrator.StateKeeping.Components;
namespace Telegrator.StateKeeping namespace Telegrator.StateKeeping
{ {
/// <summary> /// <summary>
/// State keeper that manages string-based states for chat sessions. /// State keeper that manages string-based states for chat sessions.
/// Inherits from <see cref="ArrayStateKeeper{TKey, TState}"/> with long keys and string states.
/// </summary> /// </summary>
/// <param name="states">Initial array of string states to manage</param> public class StringStateKeeper() : StateKeeperBase<string, string>()
public class StringStateKeeper(params string[] states) : ArrayStateKeeper<long, string>(states)
{ {
/// <summary>
/// Initializes a new instance of <see cref="StringStateKeeper"/> with an empty state array.
/// </summary>
public StringStateKeeper()
: this([]) { }
/// <summary> /// <summary>
/// Gets the default state value, which is an empty string. /// Gets the default state value, which is an empty string.
/// </summary> /// </summary>
public override string DefaultState => string.Empty; public override string DefaultState => string.Empty;
/// <inheritdoc/>
protected override string MoveBackward(string currentState, string currentKey)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
protected override string MoveForward(string currentState, string currentKey)
{
throw new NotImplementedException();
}
} }
/// <summary> /// <summary>
@@ -58,6 +63,10 @@ namespace Telegrator.StateKeeping
public static void SetStringState(this IHandlerContainer container, string? newState) public static void SetStringState(this IHandlerContainer container, string? newState)
=> container.StringStateKeeper().SetState(container.HandlingUpdate, newState ?? StringStateAttribute.DefaultState); => container.StringStateKeeper().SetState(container.HandlingUpdate, newState ?? StringStateAttribute.DefaultState);
public static string GetStringState(this IHandlerContainer container, string key)
=> container.StringStateKeeper().GetState()
/*
/// <summary> /// <summary>
/// Moves the string state forward to the next state in the sequence. /// Moves the string state forward to the next state in the sequence.
/// </summary> /// </summary>
@@ -71,5 +80,6 @@ namespace Telegrator.StateKeeping
/// <param name="container">The handler container instance</param> /// <param name="container">The handler container instance</param>
public static void BackwardStringState(this IHandlerContainer container) public static void BackwardStringState(this IHandlerContainer container)
=> container.StringStateKeeper().MoveBackward(container.HandlingUpdate); => container.StringStateKeeper().MoveBackward(container.HandlingUpdate);
*/
} }
} }