3

我有以下代码:

Observable.FromEvent<ModelEventArgs>(
        h => ValuesController.ModelAdded += h,
        h => ValuesController.ModelAdded -= h)
    .Subscribe(m => context.Connection.Broadcast(m));

Observable.FromEvent<ModelEventArgs>(
        h => ValuesController.ModelDeleted += h,
        h => ValuesController.ModelDeleted -= h)
    .Subscribe(m => context.Connection.Broadcast(m));

更清洁的是:

Observable.[SOMETHING](
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelAdded += h,
                h => ValuesController.ModelAdded -= h),
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelDeleted += h,
                h => ValuesController.ModelDeleted -= h))
    .Subscribe(m => context.Connection.Broadcast(m));

我似乎无法弄清楚 [SOMETHING] 需要是什么,我不想等待 ModelAdded 完成,我只是希望它们都为每个事件发出 (m)。

放轻松,我是 Rx 新手

4

1 回答 1

7

我想你正在寻找Merge

Observable.Merge(
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelAdded += h,
                h => ValuesController.ModelAdded -= h),
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelDeleted += h,
                h => ValuesController.ModelDeleted -= h))
    .Subscribe(m => context.Connection.Broadcast(m));
于 2012-10-24T03:55:54.730 回答