简体   繁体   English

如何忽略可观察序列中除错误以外的所有通知?

[英]How to ignore all notifications from an observable sequence except errors?

I am looking to create an observable that subscribes to an input source but then only keeps the error message (ie ignores elements and completion messages until someone calls Dispose). 我正在寻找创建一个可观察的对象,该对象订阅一个输入源,但是仅保留错误消息(即忽略元素和完成消息,直到有人调用Dispose)。

Basically I am looking for something like Observable.IgnoreElements but that would work more like Observable.Never . 基本上,我正在寻找类似Observable.IgnoreElements的东西,但它将更像Observable.Never

The problem is that IgnoreElements leaves both termination messages (success and error). 问题是IgnoreElements留下了两个终止消息(成功和错误)。 I only want to leave the error notification and ignore when a sequence successfully terminates. 我只想留下错误通知,并忽略序列成功终止的时间。 The Never method does this, but only generates an observable sequence, you cannot create it from another sequence. Never方法可以执行此操作,但是只能生成一个可观察的序列,不能从另一个序列创建它。

If what you want is to ignore all updates except for a single exception, if it occurs, then you can do this: 如果您想要的是忽略除单个例外之外的所有更新(如果发生),则可以执行以下操作:

public static IObservable<T> OnlyError<T>(this IObservable<T> source)
{
    return Observable.Create<T>(
        observer => source.Subscribe(
            value => { },
            observer.OnError));
}

(The rest of the answer is no longer relevant because the question was changed.) (其余答案不再相关,因为问题已更改。)

If you want to have multiple exceptions in a single sequence, the solution is a little more involved. 如果要在一个序列中包含多个异常,则解决方案要多一些。

The implicit contract for IObserver<T> is that it will be called according to this pattern: IObserver<T>的隐式协定是将根据以下模式进行调用:

OnNext* [OnError|OnCompleted]

That is, OnError can be called at most once, and it ends the sequence. 也就是说, OnError最多可以调用一次,并结束序列。 See the following excerpt from Observer Design Pattern Best Practices : 请参见《 观察者设计模式最佳实践》的以下摘录:

Once the provider calls the OnError or IObserver<T>.OnCompleted method, there should be no further notifications, . 提供程序调用OnErrorIObserver<T>.OnCompleted方法后,就不应再有其他通知了。 . .

If you want to have a sequence that can have multiple "errors," define something like this: 如果要具有可以包含多个“错误”的序列,请定义以下内容:

interface ITry<T>
{
    bool HasValue { get; }
    T Value { get; }
    Exception Error { get; }
}

And then use it like this: 然后像这样使用它:

IObservable<ITry<T>>

Keep IObserver<T>.OnError(Exception) meaning something went wrong with the sequence itself , not with the items in the sequence. 保持IObserver<T>.OnError(Exception)含义是序列本身出了问题 ,而不是序列中的项目出了问题。

Turns out there is an obvious solution... simply concatenate to Never, like this: 事实证明,有一个明显的解决方案……只需将其连接到Never,就像这样:

IObservable<TSource> Never<TSource>(IObservable<TSource> source)
{
    return source.IgnoreElements().Concat(Observable.Never<TSource>());
}

If I understand your question then I think this will work. 如果我理解您的问题,那么我认为这会起作用。

given an observable named source : 给定一个可观察的命名source

source.Materialize().Where(x => x.Kind == NotificationKind.OnError);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从可观察序列计算出预计到达时间? - How to calculate an ETA from an Observable sequence? 除了的加载顺序 - loading sequence from except 如何取消可观察序列 - How to cancel an observable sequence 在可观察序列中删除除最后一个样本之外的任何样本,直到消费者准备好 - Drop any except last sample in observable sequence until consumer is ready 当 OnNext 正在运行时,我想删除所有传入的通知,除了最新的 - While the OnNext is running, I want to drop all incoming notifications except from the latest 绑定到用户控件中的Observable Collection并从中获取通知 - binding to an Observable Collection in a usercontrol and getting notifications from it 我的使用Slack的BotFramework机器人如何忽略除直接渠道以外的所有渠道上的消息? - How can my Slack-using BotFramework bot ignore messages on all channels except direct? 使用Rx,当我的Subscribe方法运行时,如何忽略除最新值以外的所有值 - With Rx, how do I ignore all-except-the-latest value when my Subscribe method is running 如何从Windows的子进程中忽略阻止系统错误 - How to ignore blocking system errors from a child process on windows 如何将可观察序列“延迟”一个值? - How to 'delay' observable sequence by one value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM