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(buffer): subscribe to the closing notifier before the source #7185

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/operators/buffer-spec.ts
Expand Up @@ -317,11 +317,11 @@ describe('Observable.prototype.buffer', () => {
});

subject.next(1);
expect(results).to.deep.equal([[1]]);
expect(results).to.deep.equal([[]]);
subject.next(2);
expect(results).to.deep.equal([[1], [2]]);
expect(results).to.deep.equal([[], [1]]);
subject.complete();
expect(results).to.deep.equal([[1], [2], [], 'complete']);
expect(results).to.deep.equal([[], [1], [2], 'complete']);
});

it('should buffer when Promise resolves', (done) => {
Expand Down
26 changes: 13 additions & 13 deletions src/internal/operators/buffer.ts
Expand Up @@ -47,19 +47,7 @@ export function buffer<T>(closingNotifier: ObservableInput<any>): OperatorFuncti
// The current buffered values.
let currentBuffer: T[] = [];

// Subscribe to our source.
source.subscribe(
createOperatorSubscriber(
subscriber,
(value) => currentBuffer.push(value),
() => {
subscriber.next(currentBuffer);
subscriber.complete();
}
)
);

// Subscribe to the closing notifier.
// Subscribe to the closing notifier first.
from(closingNotifier).subscribe(
createOperatorSubscriber(
subscriber,
Expand All @@ -73,6 +61,18 @@ export function buffer<T>(closingNotifier: ObservableInput<any>): OperatorFuncti
)
);

// Subscribe to our source.
source.subscribe(
createOperatorSubscriber(
subscriber,
(value) => currentBuffer.push(value),
() => {
subscriber.next(currentBuffer);
subscriber.complete();
}
)
);

return () => {
// Ensure buffered values are released on finalization.
currentBuffer = null!;
Expand Down