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 1531152 commit 364567a
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions src/internal/operators/catchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,44 +111,24 @@ export function catchError<T, O extends ObservableInput<any>>(
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(
let subscription: Subscription | null = null;
let sourceSubscription: Subscription | null = source.subscribe(
new CatchErrorSubscriber(subscriber, (err) => {
handleError(err);
if (handledResult) {
if (innerSub) {
innerSub.unsubscribe();
innerSub = null;
subscription.add(handledResult.subscribe(subscriber));
} else {
syncUnsub = true;
}
try {
const nextSource = from(selector(err, catchError(selector)(source)));
subscription = nextSource.subscribe(subscriber);
} catch (selectorErr) {
subscriber.error(selectorErr);
}
})
);
subscription = subscription || sourceSubscription;

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

return subscription;
return () => {
subscription!.unsubscribe();
subscription = sourceSubscription = null;
};
});
}

Expand Down

0 comments on commit 364567a

Please sign in to comment.