为什么 Rxjs 会在出错时取消订阅? [英] Why does Rxjs unsubscribe on error?

查看:64
本文介绍了为什么 Rxjs 会在出错时取消订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:如何在流中出现错误后继续监听而不在每个 .subscribe 之前放置 .catch?

In short: How to proceed listening after an error in stream without putting a .catch before every .subscribe?

如果您需要更多详细信息,请点击此处:

If you need more details they are here:

假设我有一个当前用户或空的主题.我有时从 API 获取数据并发送到主题.它相应地更新视图.但是在某些时候我的服务器上发生错误,我希望我的应用程序像以前一样继续工作,但通知某些地方有关错误并继续监听我的主题.

Lets assume I have a Subject of current user or null. I get the data from API sometimes and send to the Subject. It updates the view accordingly. But at some point error occurs on my server and I want my application to continue working as before but notify some places about the error and KEEP listening to my Subject.

最初我认为如果我只执行 userSubject.error(...) 它只会触发 .catch 回调和 error 处理程序on 订阅并跳过所有成功处理程序和链.如果在我调用 userSubject.next(...) 之后,我的所有链和订阅者都将像以前一样工作

Initially I thought that if I just do userSubject.error(...) it will only trigger .catch callback and error handlers on subscribes and skip all success handlers and chains. And if after I call userSubject.next(...) all my chains and subscribers will work as before

但不幸的是事实并非如此.在第一个未捕获的 .error 之后,它从流中取消订阅订阅者,他们不再操作.

BUT unluckily it is not the case. After the first uncaught .error it unsubscribes subscribers from the stream and they do not operate any more.

所以我的问题是:为什么???如果我想正常处理 null 值但只在某些地方处理错误,该怎么办?

So my question: Why??? And what to do instead if I want to handle null value normally but also handle errors only in some places?

这是 RxJs 源代码的链接,其中订阅者在出错时取消订阅https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L140

Here is the link to RxJs source code where Subscriber unsubscribes on error https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L140

推荐答案

Rx observables 遵循语法 next*(error|complete)?,意思是他们在errorcomplete后不能产生任何东西 通知已送达.

Rx observables follow the grammar next*(error|complete)?, meaning that they can produce nothing after error or complete notification has been delivered.

解释为什么这很重要可以从 Rx 设计指南:

An explanation of why this matters can be found from Rx design guidelines:

指示可观察序列已完成的单个消息确保可观察序列的使用者可以确定性地确定执行清理操作是安全的.

The single message indicating that an observable sequence has finished ensures that consumers of the observable sequence can deterministically establish that it is safe to perform cleanup operations.

单个故障进一步确保可以为处理多个可观察序列的运算符维护中止语义.

A single failure further ensures that abort semantics can be maintained for operators that work on multiple observable sequences.

简而言之,如果您希望您的观察者在发生服务器错误后继续收听主题,请不要将该错误传递给主题,而是以其他方式处理它(例如使用 catchretry 或将错误传递给专门的主题).

In short, if you want your observers to keep listening to the subject after a server error has occurred, do not deliver that error to the subject, but rather handle it in some other way (e.g. use catch, retry or deliver the error to a dedicated subject).

这篇关于为什么 Rxjs 会在出错时取消订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆