It looks like you are doing something similar to this question . Basically, you want to use the topic as your _pool variable and subscribe and unsubscribe to different sources of events in the register and unregister. To unregister the source, you will need to save the one-time items that you received when you called Register. In addition, I would like to make Pooler implement IObservable directly and just forward Subscribe to the _pool variable.
using System.Reactive.Subjects; using System.Reactive.Linq; public class Pooler : IObservable<HappenedEventArgs>, IDisposable { void Dispose() { if (_pool != null) _pool.Dispose(); if (_sourceSubs != null) { foreach (var d in _sourceSubs.Values) { d.Dispose(); } _sourceSubs.Clear(); } } private Subject<HappenedEventArgs> _pool = new Subject<HappenedEventArgs>(); private Dictionary<IEventSource, IDisposable> _sourceSubs = new Dictionary<IEventSource, IDisposable>(); public IDisposable Subscribe(IObserver<HappenedEventArgs> observer) { return _pool.Subscribe(observer); } public void Register(IEventSource item) { if (_sourceSubs.ContainsKey(item)) { return;
Please note that you will need to implement IDisposable so that you can clear all event subscriptions when you are done using Pooler .
Gideon engelberth
source share