Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

rxjs5 - Why does Rxjs unsubscribe on error?

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:

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.

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

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.

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...