diff --git a/global.json b/global.json index 67d23d48..c3152d8c 100644 --- a/global.json +++ b/global.json @@ -3,7 +3,7 @@ "Uno.Sdk": "6.5.31" }, "sdk": { - "version": "10.0.202", + "version": "10.0.103", "rollForward": "latestPatch", "allowPrerelease": false } diff --git a/src/SalmonEgg.Presentation.Core/Services/Chat/AcpConnectionSessionRegistry.cs b/src/SalmonEgg.Presentation.Core/Services/Chat/AcpConnectionSessionRegistry.cs index a1d27587..38791a06 100644 --- a/src/SalmonEgg.Presentation.Core/Services/Chat/AcpConnectionSessionRegistry.cs +++ b/src/SalmonEgg.Presentation.Core/Services/Chat/AcpConnectionSessionRegistry.cs @@ -53,6 +53,7 @@ public sealed class InMemoryAcpConnectionSessionRegistry : IAcpConnectionSession { private readonly object _gate = new(); private readonly Dictionary _sessionsByProfile = new(StringComparer.Ordinal); + private readonly Dictionary _profileIdByService = new(); /// public event Action? ProfileConnectionChanged; @@ -70,13 +71,10 @@ public bool TryGetProfileId(IChatService service, out string profileId) ArgumentNullException.ThrowIfNull(service); lock (_gate) { - foreach (var pair in _sessionsByProfile) + if (_profileIdByService.TryGetValue(service, out var foundProfileId)) { - if (ReferenceEquals(pair.Value.Service, service)) - { - profileId = pair.Key; - return true; - } + profileId = foundProfileId; + return true; } } @@ -89,10 +87,16 @@ public void Upsert(AcpConnectionSession session) ArgumentNullException.ThrowIfNull(session); lock (_gate) { + if (_sessionsByProfile.TryGetValue(session.ProfileId, out var existingSession) && !ReferenceEquals(existingSession.Service, session.Service)) + { + _profileIdByService.Remove(existingSession.Service); + } + _sessionsByProfile[session.ProfileId] = session with { LastUsedUtc = session.LastUsedUtc == default ? DateTime.UtcNow : session.LastUsedUtc }; + _profileIdByService[session.Service] = session.ProfileId; } // Raise outside the lock to avoid potential deadlocks from re-entrant subscribers. @@ -104,7 +108,15 @@ public bool RemoveByProfile(string profileId) bool removed; lock (_gate) { - removed = _sessionsByProfile.Remove(profileId); + if (_sessionsByProfile.TryGetValue(profileId, out var session)) + { + _profileIdByService.Remove(session.Service); + removed = _sessionsByProfile.Remove(profileId); + } + else + { + removed = false; + } } if (removed) @@ -123,15 +135,12 @@ public bool RemoveByService(IChatService service, out string profileId) lock (_gate) { - foreach (var pair in _sessionsByProfile) + if (_profileIdByService.TryGetValue(service, out var foundProfileId)) { - if (ReferenceEquals(pair.Value.Service, service)) - { - profileId = pair.Key; - _sessionsByProfile.Remove(pair.Key); - removed = true; - break; - } + profileId = foundProfileId; + _profileIdByService.Remove(service); + _sessionsByProfile.Remove(profileId); + removed = true; } } @@ -164,7 +173,11 @@ public IReadOnlyList RemoveWhere(Func