“独家"和“默认" Rx中的订阅模式 [英] "Exclusive" and "Default" Subscription Modes in Rx
本文介绍了“独家"和“默认" Rx中的订阅模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可观察的事件对象序列,并且有许多观察者在处理特定类型的事件.我需要完成以下方案:
I have an observable sequence of event objects and a number of observers handling specific types of events. I need to accomplish the following scenarios:
- 某些事件类型需要由符合条件的第一位观察者处理(例如,observable.SubscribeExclusively(x => {}),而对于其他事件则变得不可观察".)
- 如果没有订阅,请设置一些默认处理程序(例如observable.SubscribeIfNoSubscriptions(x => {})),以使任何项目都不会丢失(例如,该处理程序可以将项目保存到数据库中,以便稍后进行处理) ).
有没有办法用Rx处理这些情况?
Is there a way to handle these cases with Rx?
推荐答案
我不确定我退出情况如何,但这对您有何影响:
I'm not sure I quite grok your scenario, but how does this strike you:
IObservable<Event> streamOfEvents.SelectMany(x => {
if (matchesExclusiveItem1(x)) {
x += exclusiveItem1Handler;
return Observable.Empty<Event>();
}
// Default case
return Observable.Return(x);
}).Subscribe(x => {
// Default case
x += defaultHandler;
});
我正在使用事件对象",因为这是您指定的,但是使用IObservable<IObservable<T>>
可能会更好-该选择器具有副作用(连接事件),效果不佳.
I'm using "Event objects" because that's what you specified, but it'd probably be better to use IObservable<IObservable<T>>
- this selector has side effects (connecting the event), which is less than good.
这篇关于“独家"和“默认" Rx中的订阅模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文