diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 61f489be7..cf7b9e7ee 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -43,60 +43,54 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe var preValue = string.Empty; var currentValue = value.ToString(); var hooks = _services.GetServices(); - var preActiveRounds = -1; - var curActiveRounds = activeRounds > 0 ? activeRounds : -1; if (ContainsState(name) && _states.TryGetValue(name, out var pair)) { var lastNode = pair?.Values.LastOrDefault(); - preActiveRounds = lastNode?.ActiveRounds ?? -1; preValue = lastNode?.Data ?? string.Empty; } - if (!ContainsState(name) || preValue != currentValue || (preValue == currentValue && preActiveRounds > 0)) - { - _logger.LogInformation($"[STATE] {name} = {value}"); - var routingCtx = _services.GetRequiredService(); + _logger.LogInformation($"[STATE] {name} = {value}"); + var routingCtx = _services.GetRequiredService(); - if (!ContainsState(name) || preValue != currentValue) + if (!ContainsState(name) || preValue != currentValue) + { + foreach (var hook in hooks) { - foreach (var hook in hooks) + hook.OnStateChanged(new StateChangeModel { - hook.OnStateChanged(new StateChangeModel - { - ConversationId = _conversationId, - MessageId = routingCtx.MessageId, - Name = name, - BeforeValue = preValue, - AfterValue = currentValue - }).Wait(); - } + ConversationId = _conversationId, + MessageId = routingCtx.MessageId, + Name = name, + BeforeValue = preValue, + AfterValue = currentValue + }).Wait(); } - - var newPair = new StateKeyValue - { - Key = name, - Versioning = isNeedVersion - }; + } - var newValue = new StateValue - { - Data = currentValue, - MessageId = routingCtx.MessageId, - Active = true, - ActiveRounds = curActiveRounds, - UpdateTime = DateTime.UtcNow, - }; - - if (!isNeedVersion || !_states.ContainsKey(name)) - { - newPair.Values = new List { newValue }; - _states[name] = newPair; - } - else - { - _states[name].Values.Add(newValue); - } + var newPair = new StateKeyValue + { + Key = name, + Versioning = isNeedVersion + }; + + var newValue = new StateValue + { + Data = currentValue, + MessageId = routingCtx.MessageId, + Active = true, + ActiveRounds = activeRounds > 0 ? activeRounds : -1, + UpdateTime = DateTime.UtcNow, + }; + + if (!isNeedVersion || !_states.ContainsKey(name)) + { + newPair.Values = new List { newValue }; + _states[name] = newPair; + } + else + { + _states[name].Values.Add(newValue); } return this;