Skip to content

Commit e1e7f98

Browse files
authored
Merge pull request #376 from iceljc/features/refine-state-node
refine state node
2 parents 14a0ea1 + fc7a82d commit e1e7f98

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,60 +43,54 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
4343
var preValue = string.Empty;
4444
var currentValue = value.ToString();
4545
var hooks = _services.GetServices<IConversationHook>();
46-
var preActiveRounds = -1;
47-
var curActiveRounds = activeRounds > 0 ? activeRounds : -1;
4846

4947
if (ContainsState(name) && _states.TryGetValue(name, out var pair))
5048
{
5149
var lastNode = pair?.Values.LastOrDefault();
52-
preActiveRounds = lastNode?.ActiveRounds ?? -1;
5350
preValue = lastNode?.Data ?? string.Empty;
5451
}
5552

56-
if (!ContainsState(name) || preValue != currentValue || (preValue == currentValue && preActiveRounds > 0))
57-
{
58-
_logger.LogInformation($"[STATE] {name} = {value}");
59-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
53+
_logger.LogInformation($"[STATE] {name} = {value}");
54+
var routingCtx = _services.GetRequiredService<IRoutingContext>();
6055

61-
if (!ContainsState(name) || preValue != currentValue)
56+
if (!ContainsState(name) || preValue != currentValue)
57+
{
58+
foreach (var hook in hooks)
6259
{
63-
foreach (var hook in hooks)
60+
hook.OnStateChanged(new StateChangeModel
6461
{
65-
hook.OnStateChanged(new StateChangeModel
66-
{
67-
ConversationId = _conversationId,
68-
MessageId = routingCtx.MessageId,
69-
Name = name,
70-
BeforeValue = preValue,
71-
AfterValue = currentValue
72-
}).Wait();
73-
}
62+
ConversationId = _conversationId,
63+
MessageId = routingCtx.MessageId,
64+
Name = name,
65+
BeforeValue = preValue,
66+
AfterValue = currentValue
67+
}).Wait();
7468
}
75-
76-
var newPair = new StateKeyValue
77-
{
78-
Key = name,
79-
Versioning = isNeedVersion
80-
};
69+
}
8170

82-
var newValue = new StateValue
83-
{
84-
Data = currentValue,
85-
MessageId = routingCtx.MessageId,
86-
Active = true,
87-
ActiveRounds = curActiveRounds,
88-
UpdateTime = DateTime.UtcNow,
89-
};
90-
91-
if (!isNeedVersion || !_states.ContainsKey(name))
92-
{
93-
newPair.Values = new List<StateValue> { newValue };
94-
_states[name] = newPair;
95-
}
96-
else
97-
{
98-
_states[name].Values.Add(newValue);
99-
}
71+
var newPair = new StateKeyValue
72+
{
73+
Key = name,
74+
Versioning = isNeedVersion
75+
};
76+
77+
var newValue = new StateValue
78+
{
79+
Data = currentValue,
80+
MessageId = routingCtx.MessageId,
81+
Active = true,
82+
ActiveRounds = activeRounds > 0 ? activeRounds : -1,
83+
UpdateTime = DateTime.UtcNow,
84+
};
85+
86+
if (!isNeedVersion || !_states.ContainsKey(name))
87+
{
88+
newPair.Values = new List<StateValue> { newValue };
89+
_states[name] = newPair;
90+
}
91+
else
92+
{
93+
_states[name].Values.Add(newValue);
10094
}
10195

10296
return this;

0 commit comments

Comments
 (0)