Skip to content

Instantly share code, notes, and snippets.

@letroll
Forked from DomGries/gist:3786466
Last active July 5, 2017 15:40
Show Gist options
  • Save letroll/a12b7067ed042be8f98c to your computer and use it in GitHub Desktop.
Save letroll/a12b7067ed042be8f98c to your computer and use it in GitHub Desktop.
[event bus csharp] event bus csharp #eventbus
using System;
using System.Collections.Generic;
public class GameEvent
{
}
public class EventMessenger
{
static EventMessenger _instance;
public static EventMessenger Instance
{
get
{
if (_instance == null)
{
_instance = new EventMessenger();
}
return _instance;
}
}
public delegate void EventDelegate<T>(T e) where T : GameEvent;
readonly Dictionary<Type, Delegate> _delegates = new Dictionary<Type, Delegate>();
public void AddListener<T>(EventDelegate<T> listener) where T : GameEvent
{
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
_delegates[typeof(T)] = Delegate.Combine(d, listener);
}
else
{
_delegates[typeof(T)] = listener;
}
}
public void RemoveListener<T>(EventDelegate<T> listener) where T : GameEvent
{
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
Delegate currentDel = Delegate.Remove(d, listener);
if (currentDel == null)
{
_delegates.Remove(typeof(T));
}
else
{
_delegates[typeof(T)] = currentDel;
}
}
}
public void Raise<T>(T e) where T : GameEvent
{
if (e == null)
{
throw new ArgumentNullException("e");
}
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
EventDelegate<T> callback = d as EventDelegate<T>;
if (callback != null)
{
callback(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment