Skip to content

Commit

Permalink
refactor(catchError): simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Aug 23, 2020
1 parent dff9d4e commit 432488c
Showing 1 changed file with 6 additions and 36 deletions.
42 changes: 6 additions & 36 deletions src/internal/operators/catchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,45 +110,15 @@ export function catchError<T, O extends ObservableInput<any>>(
): OperatorFunction<T, T | ObservedValueOf<O>> {
return (source: Observable<T>) =>
lift(source, function (this: Subscriber<T>, source: Observable<T>) {
const subscriber = this;
const subscription = new Subscription();
let innerSub: Subscription | null = null;
let syncUnsub = false;
let handledResult: Observable<ObservedValueOf<O>>;

const handleError = (err: any) => {
try {
handledResult = from(selector(err, catchError(selector)(source)));
} catch (err) {
subscriber.error(err);
return;
}
};

innerSub = source.subscribe(
new CatchErrorSubscriber(subscriber, (err) => {
handleError(err);
if (handledResult) {
if (innerSub) {
innerSub.unsubscribe();
innerSub = null;
subscription.add(handledResult.subscribe(subscriber));
} else {
syncUnsub = true;
}
source.subscribe(
new CatchErrorSubscriber(this, (err) => {
try {
from(selector(err, catchError(selector)(source))).subscribe(this);
} catch (selectorErr) {
this.error(selectorErr);
}
})
);

if (syncUnsub) {
innerSub.unsubscribe();
innerSub = null;
subscription.add(handledResult!.subscribe(subscriber));
} else {
subscription.add(innerSub);
}

return subscription;
});
}

Expand Down

0 comments on commit 432488c

Please sign in to comment.