This commit is contained in:
ThomasCharester committed 2026-02-26 11:21:27 +03:00
1 parent a73338916a
commit fbc1cc8d3e
207 files changed
+10327 -2

No files matched your search

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace Code.Infrastructure.EventBus
{
public class EventBus
{
private Dictionary<Type, SubscribersList<IGlobalSubscriber>> s_Subscribers
= new();
public void Subscribe(IGlobalSubscriber subscriber)
{
List<Type> subscriberTypes = EventBusHelper.GetSubscriberTypes(subscriber);
foreach (Type t in subscriberTypes)
{
if (!s_Subscribers.ContainsKey(t))
{
s_Subscribers[t] = new SubscribersList<IGlobalSubscriber>();
}
s_Subscribers[t].Add(subscriber);
}
}
public void Unsubscribe(IGlobalSubscriber subscriber)
{
List<Type> subscriberTypes = EventBusHelper.GetSubscriberTypes(subscriber);
foreach (Type t in subscriberTypes)
{
if (s_Subscribers.ContainsKey(t))
s_Subscribers[t].Remove(subscriber);
}
}
public void RaiseEvent<TSubscriber>(Action<TSubscriber> action)
where TSubscriber : class, IGlobalSubscriber
{
if (!s_Subscribers.ContainsKey(typeof(TSubscriber))) return;
SubscribersList<IGlobalSubscriber> subscribers = s_Subscribers[typeof(TSubscriber)];
subscribers.Executing = true;
InvokeSubscribers(action, subscribers);
subscribers.Executing = false;
subscribers.Cleanup();
}
private void InvokeSubscribers<TSubscriber>(Action<TSubscriber> action, SubscribersList<IGlobalSubscriber> subscribers)
where TSubscriber : class, IGlobalSubscriber
{
for ( int i = 0; i < subscribers.List.Count; i++)
{
IGlobalSubscriber subscriber = subscribers.List[i];
try
{
action.Invoke(subscriber as TSubscriber);
}
catch (Exception e)
{
UnityEngine.Debug.LogError(e);
}
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b0547f2543ccf5e47b9ec486f35aa2df
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Code.Infrastructure.EventBus
{
internal static class EventBusHelper
{
private static Dictionary<Type, List<Type>> s_CashedSubscriberTypes =
new();
public static List<Type> GetSubscriberTypes(
IGlobalSubscriber globalSubscriber)
{
Type type = globalSubscriber.GetType();
if (s_CashedSubscriberTypes.ContainsKey(type))
{
return s_CashedSubscriberTypes[type];
}
List<Type> subscriberTypes = type
.GetInterfaces()
.Where(t => t.GetInterfaces()
.Contains(typeof(IGlobalSubscriber)))
.ToList();
s_CashedSubscriberTypes[type] = subscriberTypes;
return subscriberTypes;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ab2b1bcff76d5614dbda77a3c7b24158
@@ -0,0 +1,5 @@
namespace Code.Infrastructure.EventBus
{
public interface IGlobalSubscriber
{ }
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fe414b0de24841347b03eec77d1855c0
@@ -0,0 +1,46 @@
using System.Collections.Generic;
namespace Code.Infrastructure.EventBus
{
internal class SubscribersList<TSubscriber> where TSubscriber : class
{
private bool m_NeedsCleanUp;
public bool Executing;
public readonly List<TSubscriber> List = new(); // TODO Weakptr
public void Add(TSubscriber subscriber)
{
List.Add(subscriber);
}
public void Remove(TSubscriber subscriber)
{
if (Executing)
{
var i = List.IndexOf(subscriber);
if (i >= 0)
{
m_NeedsCleanUp = true;
List[i] = null;
}
}
else
{
List.Remove(subscriber);
}
}
public void Cleanup()
{
if (!m_NeedsCleanUp)
{
return;
}
List.RemoveAll(s => s == null);
m_NeedsCleanUp = false;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d364e9c8ac1c255428486d5892d23c1d