using System.Collections.Concurrent;
using ZB.MOM.WW.OtOpcUa.Commons.Browsing;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Browsing;
///
/// Singleton in-process directory of live instances,
/// keyed by . Concurrency is provided by the
/// underlying alone — there are no
/// additional locks; callers must dispose evicted sessions outside the dictionary.
///
public sealed class BrowseSessionRegistry
{
private readonly ConcurrentDictionary _sessions = new();
/// Adds (or replaces) a session in the registry keyed by its token.
/// The session to register.
public void Register(IBrowseSession session) => _sessions[session.Token] = session;
/// Looks up a session by token without removing it.
/// The token identifying the session to look up.
/// The found session, if any.
/// true if a session with the given token was found.
public bool TryGet(Guid token, out IBrowseSession session) =>
_sessions.TryGetValue(token, out session!);
/// Atomically removes a session from the registry, returning it for disposal.
/// The token identifying the session to remove.
/// The removed session, if found.
/// true if a session with the given token was found and removed.
public bool TryRemove(Guid token, out IBrowseSession session) =>
_sessions.TryRemove(token, out session!);
/// Returns a point-in-time snapshot of all currently registered sessions.
/// The registered sessions and their tokens at the time of the call.
public IReadOnlyList<(Guid Token, IBrowseSession Session)> Snapshot() =>
_sessions.Select(kv => (kv.Key, kv.Value)).ToList();
}