using System.Collections.Concurrent; using DotNetty.Transport.Channels; using Shiki.Common.Identity; using Shiki.Common.Util; using UniScan.UserInterface; using UniScan.UserInterface.Nodes.Core.Node; namespace UniScan.Server.Core.UserInterface; public interface IUISSRManagerMutationProxy { void Subscribe(IChannel channel, IUISSRContextSession session); void Unsubscribe(IChannel channel, Identifier id); void UnsubscribeAll(IChannel channel); void RegisterFactory(IUISSRContextSessionFactory factory); void UnregisterFactory(Identifier id); void RegisterNodeProvider(IUINodeProvider provider); void UnregisterNodeProvider(IUINodeProvider provider); IUINode? ResolveNode(object context); } public interface IUISSRManager : IUISSRManagerMutationProxy { public IReadOnlyDictionary> ChannelSessions { get; } public IReadOnlyDictionary SessionFactories { get; } } public class UISSRManager : IUISSRManager { private ConcurrentDictionary> _channelSessions = []; public IReadOnlyDictionary> ChannelSessions => _channelSessions; private readonly ConcurrentDictionary _sessionFactories = []; public IReadOnlyDictionary SessionFactories => _sessionFactories; private readonly List _nodeProviders = []; public UISSRManager(IEnumerable factories, IEnumerable nodeProviders) { foreach (IUISSRContextSessionFactory factory in factories) { _sessionFactories.TryAdd(factory.ContextIdentifier, factory); } _nodeProviders.AddRange(nodeProviders); } void IUISSRManagerMutationProxy.Subscribe(IChannel channel, IUISSRContextSession session) { var sessions = _channelSessions.GetOrAdd(channel, _ => new ConcurrentDictionary()); sessions[session.ContextIdentifier] = session; } void IUISSRManagerMutationProxy.Unsubscribe(IChannel channel, Identifier id) { if (!_channelSessions.TryGetValue(channel, out var sessions)) return; sessions.TryRemove(id, out IUISSRContextSession? session); session?.Dispose(); } void IUISSRManagerMutationProxy.UnsubscribeAll(IChannel channel) { if (_channelSessions.TryRemove(channel, out var sessions)) { foreach (var session in sessions) { session.Value.Dispose(); } } } void IUISSRManagerMutationProxy.RegisterFactory(IUISSRContextSessionFactory factory) { _sessionFactories.TryAdd(factory.ContextIdentifier, factory); } void IUISSRManagerMutationProxy.UnregisterFactory(Identifier id) { _sessionFactories.TryRemove(id, out _); } public void RegisterNodeProvider(IUINodeProvider provider) { _nodeProviders.Add(provider); } public void UnregisterNodeProvider(IUINodeProvider provider) { _nodeProviders.Remove(provider); } public IUINode? ResolveNode(object context) => _nodeProviders.Where(p => p.CanProvide(context)) .OrderByDescending(p => p.Priority) .FirstOrDefault()?.Provide(context); } /// /// Creates SSR context sessions /// /// Supposed to be registered to DI for easy construction /// public interface IUISSRContextSessionFactory { Identifier ContextIdentifier { get; } /// /// Returns true if the provided args are supported /// /// The args /// Whether the args are supported public bool SupportsArgs(object? args); /// /// Creates a session /// /// /// /// /// IUISSRContextSession Create(IChannel channel, Identifier contextIdentifier, object? args); } /// /// Creates SSR context sessions /// /// Supposed to be registered to DI for easy construction /// public interface IUISSRContextSessionFactory : IUISSRContextSessionFactory where TArgs : class { /// /// Creates a session /// /// /// /// /// IUISSRContextSession Create(IChannel channel, Identifier contextIdentifier, TArgs args); /// bool IUISSRContextSessionFactory.SupportsArgs(object? args) => args is TArgs; /// IUISSRContextSession IUISSRContextSessionFactory. Create(IChannel channel, Identifier contextIdentifier, object? args) => Create(channel, contextIdentifier, args as TArgs ?? throw new InvalidCastException()); } /// /// Ongoing session for subscribed context /// public interface IUISSRContextSession : IDisposable { Identifier ContextIdentifier { get; } /// /// Initializes the session /// /// Supposed to set up observing/periodic updates when needed /// Task InitializeAsync(); } /// /// Constructs IUINodes based on a given type /// public interface IUINodeProvider : IProvider { /// /// Type this Provider applies to (null for globally usable) /// public Type? Type { get; } } /// /// Constructs IUINodes based on a given type /// public interface IUINodeProvider : IProvider, IUINodeProvider where T : class { /// Type IUINodeProvider.Type => typeof(T); /// bool IProvider.CanProvide(object? o) => o is T t && CanProvide(t); /// IUINode IProvider.Provide(object? o) => Provide((T)o!); } /// /// For providing updates to IUINodes/slots /// public interface IUINetworkNodeUpdater where TArgs : class { public Task UpdateAsync(IChannel channel, TArgs args); }