使用Rx将多个事件源合并到一个IObservable中

时间:2011-11-19 13:55:48

标签: c# .net event-handling system.reactive

这是关于如何在特定事件相关方案中使用Reactive Extensions(Rx)的问题。

  • 目的是采取一些触发某些事件的课程
  • 并将它们聚集到一个IObservable中,任何客户都可以订阅它(不知道事件类)。
  • 请注意,感兴趣的事件使用子类EventArgs

一些自定义EventArgs

public class HappenedEventArgs : EventArgs
{
    internal bool IsBadNotGood;
}

发生事件的许多单独的类

public class EventSourceA : IEventSource {

    public event HappenedEventHandler Happened;
    private void OnHappened(HappenedEventArgs e)
    {
        if (Happened != null)
            Happened(this, e);
    }
    // And then this class calls OnHappened(e) whenever it decides to ...
}

public class EventSourceB : IEventSource {

    public event HappenedEventHandler Happened;
    private void OnHappened(HappenedEventArgs e)
    {
        if (Happened != null)
            Happened(this, e);
    }
    // And then this class also calls OnHappened(e) at times ...
}

public interface IEventSource
{
    event HappenedEventHandler Happened;
}

public delegate void HappenedEventHandler(object sender, HappenedEventArgs e);

如何聚集所有这些事件并揭露联合事件阵线

public class Pooler{

    private IObservable<X> _pool;

    public IObservable<X> Subscribe(){
        return _pool;        
    }

    public void Register(IEventSource item)
    {
        // How to take item.Happened and inject/bind it into _pool here?
    }        

    internal void Unregister(IEventSource item)
    {
        // Disconnect item.Happened from _pool
    }

    public Pooler(){
        // Instantiate _pool to whatever is best?
        // _pool = ...
    }

 }

直接了解EventSources的订阅者

 static void Try() {
     var pooler = new Pooler();
     pooler.Subscribe().Subscribe(e =>
            {
                 // Do something with events here, as they arrive
            }
     );
     // ....
     // Wherever whenever:
     AddEventSources(pooler);
 }

 static void AddEventSources(Pooler pooler){
     var eventSourceA = new EventSourceA();
     pooler.Register(eventSourceA);
     var eventSourceB = new EventSourceB();
     pooler.Register(eventSourceB);     
 }

2 个答案:

答案 0 :(得分:7)

Rx库试图提供的是处理这些情况的方法,而不必创建一堆手动传播可观察对象的类/方法。

假设您有一个有活动的课程:

public class EventedClass
{
    public event Action<EventArgs> Event;
}

这些实例IEnumerable<EventedClass> objects的数量, 您可以使用LINQ从这些类中投射可观察对象,将它们与Observable.Merge结合使用,这将为您提供这些事件的组合顺序输出。

Observable.Merge(
    objects.Select(
        o => Observable.FromEvent<EventArgs>(
            handler => o.Event += handler,
            handler => o.Event -= handler
        )
)).Subscribe(args => 
{ 
    //do stuff
});

答案 1 :(得分:3)

听起来你正在做类似this question的事情。基本上,您希望将主题用作_pool变量,并让它订阅和取消订阅注册和取消注册中的不同事件源。要取消注册源,您需要保留在注册调用中获得的一次性用品。另外,我会考虑直接Pooler实施IObservable,然后将Subscribe转发给_pool变量。

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; //already registered
        }
        else
        {
            _sourceSubs.Add(item,
                            Observable.FromEventPattern((EventHandler<HappenedEventArgs> h) => item.Happened += h,
                                                        h => item.Happened -= h)
                                      .Select(ep => ep.EventArgs)
                                      .Subscribe(_pool));
        }
    }

    internal void Unregister(IEventSource item)
    {
        IDisposable disp = null;
        if (_sourceSubs.TryGetValue(item, out disp))
        {
            _sourceSubs.Remove(item);
            disp.Dispose();
        }
    }
}

请注意,您需要实施IDisposable,以确保在完成Pooler后可以清理所有活动订阅。