Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shareReplay): subscribe to subject before source subscription starts #5521

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/operators/shareReplay-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,15 @@ describe('shareReplay operator', () => {
done();
});
});

it('should not skip values on a sync source', () => {
const a = cold( '(abcd)');
const x = cold( 'x-------x');
const expected = '(abcd)--d';

const shared = a.pipe(shareReplay(1));
const result = x.pipe(mergeMapTo(shared));
expectObservable(result).toBe(expected);
});

});
5 changes: 4 additions & 1 deletion src/internal/operators/shareReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ function shareReplayOperator<T>({

return function shareReplayOperation(this: Subscriber<T>, source: Observable<T>) {
refCount++;
let innerSub: Subscription;
if (!subject || hasError) {
hasError = false;
subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler);
innerSub = subject.subscribe(this);
subscription = source.subscribe({
next(value) { subject!.next(value); },
error(err) {
Expand All @@ -169,9 +171,10 @@ function shareReplayOperator<T>({
subject!.complete();
},
});
} else {
innerSub = subject.subscribe(this);
}

const innerSub = subject.subscribe(this);
this.add(() => {
refCount--;
innerSub.unsubscribe();
Expand Down