I have an rxjs BehaviorSubject
I subscribe to using an async
pipe from Angular 2 and I have a catch
to handle eventual errors it throws.
Problem is, every time I get an error it starts an infinite loop because my catch returns the Observable derived from the BehaviorSubject
and I think the async
pipe resubscribes to the observable when I return the catch
.
The code looks roughly like this:
ListService - is a @Injectable
where I have the BehaviorSubject and the property with the Observable
.
private listSubject: BehaviorSubject<IItem[]>;
public get listObservable() {
return this.listSubject.asObservable()
}
private error(error: IError) {
this.listSubject.error(error);
}
ListComponent - is a @Component
that shows the list observable.
// Template
<items-view [items]="list | async"></items-view>
// Code
public get list() {
return this.listService.listObservable
.catch((error) => {
this.handleError(error);
return this.listService.listObservable;
});
}
As you can see, my catch returns the current observable, as it MUST return an observable. So, what happens is, when I send the this.listSubject.error(error)
the code enters an infinite loop calling the catch
indefinitely because, like I said before, I think that the BehaviourSubject
re-throws the error because the async
pipe re-subscribes to the observable when the catch
returns it.
I tried to return my previous cached array in the error to return an Observable.of(error.cached)
, but I got a whole new set of problems because think the async wasn't subscribed to the BehaviorSubject
anymore.
Like I said before, this is a rough representation of my real code, but the logic is basically that.
I have been trying various different approaches to this but I couldn't manage to get this infinite loop to stop.
Thanks in advance for the help.