Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rxjs unsubscribe on error?

Tags:

rxjs

rxjs5

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

like image 531
mgrinko Avatar asked Aug 03 '17 06:08

mgrinko


People also ask

How to unsubscribe from events in RxJS?

Fortunately, with RxJS you are offered a more convenient method: subscriptions. Whenever you subscribe to an observable, you ' ll get back a Subscription object. Once you no longer wish to receive events from the observable you can simply use this object to unsubscribe: Much cleaner.

What is retrywhen error handling in RxJS?

In RxJS, the retryWhen () error-handling operator provides an observable that is the same as the source observable except for the error. If the source observable calls an error, this method will emit the Throwable that caused the error to the observable received from the notifier.

Should I unsubscribe from subscriptions?

as a component you never know whether you're a child or not. therefore you should always unsubscribe from subscriptions as best practice. – SeriousM Oct 29 '16 at 17:57

Do we need to explicitly call the unsubscribe method?

In most cases we will not need to explicitly call the unsubscribe method unless we want to cancel early or our Observable has a longer lifespan than our subscription. The default behavior of Observable operators is to dispose of the subscription as soon as.complete () or.error () messages are published.


1 Answers

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

like image 161
Sergey Karavaev Avatar answered Sep 28 '22 18:09

Sergey Karavaev