Skip to content

Commit

Permalink
chore(shareReplay): ensure subscription is when source completes
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Jul 2, 2020
1 parent fd66c29 commit 229c58d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/internal/operators/shareReplay.ts
Expand Up @@ -152,7 +152,6 @@ function shareReplayOperator<T>({
let refCount = 0;
let subscription: Subscription | undefined;
let hasError = false;
let isComplete = false;

return function shareReplayOperation(this: Subscriber<T>, source: Observable<T>) {
refCount++;
Expand All @@ -168,19 +167,24 @@ function shareReplayOperator<T>({
subject!.error(err);
},
complete() {
isComplete = true;
subscription = undefined;
subject!.complete();
},
});
// The following condition is needed because source can complete synchronously
// upon subscription. When that happens `subscription` is first set to `undefined`
// and right after is set to the "closed subscription" returned by `subscribe`
if (subscription.closed && !hasError) {
subscription = undefined;
}
} else {
innerSub = subject.subscribe(this);
}

this.add(() => {
refCount--;
innerSub.unsubscribe();
if (subscription && !isComplete && useRefCount && refCount === 0) {
if (subscription && useRefCount && refCount === 0) {
subscription.unsubscribe();
subscription = undefined;
subject = undefined;
Expand Down

0 comments on commit 229c58d

Please sign in to comment.