Skip to content

Instantly share code, notes, and snippets.

@ctolkien
Created December 2, 2012 03:16
Show Gist options
  • Save ctolkien/4186791 to your computer and use it in GitHub Desktop.
Save ctolkien/4186791 to your computer and use it in GitHub Desktop.
RX Event Aggregator
public class EventPublisher : IEventPublisher
{
private readonly ConcurrentDictionary<Type, object> subjects
= new ConcurrentDictionary<Type, object>();
public IObservable<TEvent> GetEvent<TEvent>()
{
var subject =
(ISubject<TEvent>) subjects.GetOrAdd(typeof (TEvent),
t => new Subject<TEvent>());
return subject.AsObservable();
}
public void Publish<TEvent>(TEvent sampleEvent)
{
object subject;
if (subjects.TryGetValue(typeof(TEvent), out subject))
{
((ISubject<TEvent>)subject)
.OnNext(sampleEvent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment