adammyhre / unity-event-bus Goto Github PK
View Code? Open in Web Editor NEWLicense: The Unlicense
License: The Unlicense
Hey Adam!
Thank you for your great content!
I implemented your EventBus but think it is more complex than necessary.
Is there a specific reason why you work with the EventBinding class? In my eyes this only leads to more verbose code without any benefits. Do you extend the EventBinding class with additional functionality or why did you choose to go that way?
Example of registering a binding with your implementation:
private EventBinding<PlayerEvent> _playerEventBinding;
private void OnEnable()
{
_playerEventBinding = new EventBinding<PlayerEvent>(HandlePlayerEvent);
EventBus<PlayerEvent>.Register(_playerEventBinding);
}
With the implementation below it would be just one line:
private void OnEnable()
{
EventBus<PlayerEvent>.Register(HandlePlayerEvent);
}
My EventBus implementation:
public static class EventBus<T> where T : IEvent
{
//in this case we specifically _want_ a static member in a generic type. We want a different bindings-hashSet in each instance of different close constructed type (https://www.jetbrains.com/help/resharper/StaticMemberInGenericType.html)
// ReSharper disable once StaticMemberInGenericType
private static readonly HashSet<Action> BindingsWithoutArguments = new();
private static readonly HashSet<Action<T>> BindingsWithArguments = new();
public static void Register(Action<T> binding) => BindingsWithArguments.Add(binding);
public static void Register(Action binding) => BindingsWithoutArguments.Add(binding);
public static void DeRegister(Action<T> binding) => BindingsWithArguments.Remove(binding);
public static void DeRegister(Action binding) => BindingsWithoutArguments.Remove(binding);
public static void Raise(T @event)
{
foreach (Action<T> binding in BindingsWithArguments)
{
binding?.Invoke(@event);
}
foreach (Action binding in BindingsWithoutArguments)
{
binding?.Invoke();
}
}
/// <summary>Remove all listeners.</summary>
[UsedImplicitly] //used via reflection in EventBusUtilities.ClearAllBuses()
public static void Clear()
{
Debug.Log($"{EventBusUtilities.EventBusLogPrefix}Clearing {typeof(T).Name} bindings (removing all listeners)");
BindingsWithArguments.Clear();
BindingsWithoutArguments.Clear();
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.