Created
December 2, 2012 03:16
-
-
Save ctolkien/4186791 to your computer and use it in GitHub Desktop.
RX Event Aggregator
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
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