Caching singleton proxies

This commit is contained in:
2026-07-07 22:39:25 +03:00
parent a4c05af5c8
commit dce374035f
@@ -8,6 +8,7 @@ namespace mROA.Implementation
public class RemoteInstanceRepository : IInstanceRepository public class RemoteInstanceRepository : IInstanceRepository
{ {
private readonly List<RemoteObjectBase> _producedProxies = new(); private readonly List<RemoteObjectBase> _producedProxies = new();
private readonly List<RemoteObjectBase> _producedSingletonProxies = new();
private readonly ICallIndexProvider _callIndexProvider; private readonly ICallIndexProvider _callIndexProvider;
private readonly IRepresentationModuleProducer _representationProducer; private readonly IRepresentationModuleProducer _representationProducer;
@@ -32,9 +33,9 @@ namespace mROA.Implementation
public T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context) where T : class public T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context) where T : class
{ {
var index = _producedProxies.Find(i => i.Identifier.Equals(id)); var existing = _producedProxies.Find(i => i.Identifier.Equals(id));
if (index is not null) if (existing is not null)
return (T)(index as object); return (T)(existing as object);
if (!_callIndexProvider.Activators.TryGetValue(typeof(T), out var remoteType)) if (!_callIndexProvider.Activators.TryGetValue(typeof(T), out var remoteType))
throw new NotSupportedException(); throw new NotSupportedException();
@@ -55,6 +56,10 @@ namespace mROA.Implementation
public object GetSingletonObject(Type type, IEndPointContext context) public object GetSingletonObject(Type type, IEndPointContext context)
{ {
var existing = _producedSingletonProxies.Find(i => i.GetType() == type);
if (existing is not null)
return existing;
var representationModule = var representationModule =
_representationProducer.Produce(context.OwnerId); _representationProducer.Produce(context.OwnerId);
@@ -62,6 +67,7 @@ namespace mROA.Implementation
_callIndexProvider.GetIndices(type))!; _callIndexProvider.GetIndices(type))!;
_producedProxies.Add(instance); _producedProxies.Add(instance);
_producedSingletonProxies.Add(instance);
return _producedProxies.Last(); return _producedProxies.Last();
} }