Created
October 22, 2012 12:29
-
-
Save kevinobee/3931297 to your computer and use it in GitHub Desktop.
SimpleEventAggregator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
namespace Mvc.Infrastructure.Sitecore.Events | |
{ | |
public class SimpleEventAggregator : IEventAggregator | |
{ | |
private readonly Dictionary<Type, List<WeakReference>> _eventSubscriberLists = | |
new Dictionary<Type, List<WeakReference>>(); | |
private readonly object _lock = new object(); | |
public void Subscribe(object subscriber) | |
{ | |
lock (_lock) | |
{ | |
var subscriberTypes = | |
subscriber.GetType().GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISubscriber<>)); | |
var weakReference = new WeakReference(subscriber); | |
foreach (var subscriberType in subscriberTypes) | |
{ | |
var subscribers = GetSubscribers(subscriberType); | |
subscribers.Add(weakReference); | |
} | |
} | |
} | |
public void Publish<TEvent>(TEvent eventToPublish) | |
{ | |
var subscriberType = typeof(ISubscriber<>).MakeGenericType(typeof(TEvent)); | |
var subscribers = GetSubscribers(subscriberType); | |
var subscribersToRemove = new List<WeakReference>(); | |
foreach (var weakSubscriber in subscribers) | |
{ | |
if (weakSubscriber.IsAlive) | |
{ | |
var subscriber = (ISubscriber<TEvent>)weakSubscriber.Target; | |
var syncContext = SynchronizationContext.Current; | |
if (syncContext == null) | |
syncContext = new SynchronizationContext(); | |
syncContext.Post(s => subscriber.OnEvent(eventToPublish), null); | |
} | |
else | |
{ | |
subscribersToRemove.Add(weakSubscriber); | |
} | |
} | |
if (subscribersToRemove.Any()) | |
{ | |
lock (_lock) | |
{ | |
foreach (var remove in subscribersToRemove) | |
subscribers.Remove(remove); | |
} | |
} | |
} | |
private List<WeakReference> GetSubscribers(Type subscriberType) | |
{ | |
List<WeakReference> subscribers; | |
lock (_lock) | |
{ | |
var found = _eventSubscriberLists.TryGetValue(subscriberType, out subscribers); | |
if (!found) | |
{ | |
subscribers = new List<WeakReference>(); | |
_eventSubscriberLists.Add(subscriberType, subscribers); | |
} | |
} | |
return subscribers; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment