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 all commits
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
21 changes: 20 additions & 1 deletion spec/operators/shareReplay-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as sinon from 'sinon';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { shareReplay, mergeMapTo, retry } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { Observable, Operator, Observer, of } from 'rxjs';
import { Observable, Operator, Observer, of, from } from 'rxjs';

declare const rxTestScheduler: TestScheduler;

Expand Down Expand Up @@ -265,4 +265,23 @@ describe('shareReplay operator', () => {
done();
});
});

it('should not skip values on a sync source', () => {
const a = from(['a', 'b', 'c', 'd']);
// We would like for the previous line to read like this:
//
// const a = cold('(abcd|)');
//
// However, that would synchronously emit multiple values at frame 0,
// but it's not synchronous upon-subscription.
// TODO: revisit once https://github.com/ReactiveX/rxjs/issues/5523 is fixed

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