Абстракция биндеров событий

This commit is contained in:
2025-03-10 12:34:04 +03:00
parent ce14258bf7
commit e3710ffe20
6 changed files with 30 additions and 15 deletions
+2 -2
View File
@@ -5,11 +5,11 @@ namespace mROA.Abstract
{ {
public interface IContextRepository : IInjectableModule public interface IContextRepository : IInjectableModule
{ {
int ResisterObject(object o, IEndPointContext context); int ResisterObject<T>(object o, IEndPointContext context);
void ClearObject(int id); void ClearObject(int id);
T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell); T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(int id); T? GetObject<T>(int id);
object GetSingleObject(Type type); object GetSingleObject(Type type);
int GetObjectIndex(object o, IEndPointContext context); int GetObjectIndex<T>(object o, IEndPointContext context);
} }
} }
+7
View File
@@ -0,0 +1,7 @@
namespace mROA.Abstract
{
public interface IEventBinder<T>
{
public void BindEvents(T source, IEndPointContext context);
}
}
@@ -10,8 +10,11 @@ namespace mROA.Implementation.Backend
{ {
public class ContextRepository : IContextRepository public class ContextRepository : IContextRepository
{ {
public static object[] EventBinders = new object[]{};
private int _debugId = -1; private int _debugId = -1;
private static int LastDebugId = -1; private static int LastDebugId = -1;
// [CanBeNull] // [CanBeNull]
private Dictionary<int, object?> _singletons; private Dictionary<int, object?> _singletons;
private object?[] _storage; private object?[] _storage;
@@ -39,13 +42,15 @@ namespace mROA.Implementation.Backend
Activator.CreateInstance); Activator.CreateInstance);
} }
public int ResisterObject(object o, IEndPointContext context) public int ResisterObject<T>(object o, IEndPointContext context)
{ {
if (!_lastIndexFinder.IsCompleted) if (!_lastIndexFinder.IsCompleted)
_lastIndexFinder.Wait(); _lastIndexFinder.Wait();
_storage[_lastIndexFinder.Result] = o; _storage[_lastIndexFinder.Result] = o;
EventBinders.OfType<IEventBinder<T>>().FirstOrDefault()?.BindEvents((T)o, context);
var last = _lastIndexFinder.Result; var last = _lastIndexFinder.Result;
_lastIndexFinder = Task.Run(FindLastIndex); _lastIndexFinder = Task.Run(FindLastIndex);
@@ -71,18 +76,21 @@ namespace mROA.Implementation.Backend
public T GetObject<T>(int id) public T GetObject<T>(int id)
{ {
return id == -1 || _storage.Length <= id ? throw new NullReferenceException("Cannot find that object. It is null"): (T)_storage[id]!; return id == -1 || _storage.Length <= id
? throw new NullReferenceException("Cannot find that object. It is null")
: (T)_storage[id]!;
} }
public object GetSingleObject(Type type) public object GetSingleObject(Type type)
{ {
return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); return _singletons.GetValueOrDefault(type.GetHashCode()) ??
throw new ArgumentException("Unregistered singleton type");
} }
public int GetObjectIndex(object o, IEndPointContext context) public int GetObjectIndex<T>(object o, IEndPointContext context)
{ {
var index = Array.IndexOf(_storage, o); var index = Array.IndexOf(_storage, o);
return index == -1 ? ResisterObject(o, context) : index; return index == -1 ? ResisterObject<T>(o, context) : index;
} }
private int FindLastIndex() private int FindLastIndex()
@@ -98,9 +106,9 @@ namespace mROA.Implementation.Backend
_storage = nextStorage; _storage = nextStorage;
return _storage.Length; return _storage.Length;
} }
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
{ {
} }
} }
} }
@@ -27,10 +27,10 @@ namespace mROA.Implementation.Backend
{ {
} }
public int ResisterObject(object o, IEndPointContext context) public int ResisterObject<T>(object o, IEndPointContext context)
{ {
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.ResisterObject(o, context); return repository.ResisterObject<T>(o, context);
} }
public void ClearObject(int id) public void ClearObject(int id)
@@ -63,10 +63,10 @@ namespace mROA.Implementation.Backend
return repository.GetSingleObject(type); return repository.GetSingleObject(type);
} }
public int GetObjectIndex(object o, IEndPointContext context) public int GetObjectIndex<T>(object o, IEndPointContext context)
{ {
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObjectIndex(o, context); return repository.GetObjectIndex<T>(o, context);
} }
public IContextRepository GetRepository(int clientId) public IContextRepository GetRepository(int clientId)
@@ -9,7 +9,7 @@ namespace mROA.Implementation
private IRepresentationModuleProducer? _representationProducer; private IRepresentationModuleProducer? _representationProducer;
public static Dictionary<Type, Type> RemoteTypes = new(); public static Dictionary<Type, Type> RemoteTypes = new();
public int ResisterObject(object o, IEndPointContext context) public int ResisterObject<T>(object o, IEndPointContext context)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@@ -58,7 +58,7 @@ namespace mROA.Implementation
representationModule)!; representationModule)!;
} }
public int GetObjectIndex(object o, IEndPointContext context) public int GetObjectIndex<T>(object o, IEndPointContext context)
{ {
if (o is RemoteObjectBase remote) if (o is RemoteObjectBase remote)
{ {
+1 -1
View File
@@ -49,7 +49,7 @@ namespace mROA.Implementation
else else
{ {
_identifier.OwnerId = EndPointContext.HostId; _identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value, EndPointContext); _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex<T>(Value, EndPointContext);
} }
} }
} }