Skip to content
Open
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
22 changes: 22 additions & 0 deletions packages/rxjs/spec/operators/takeLast-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { takeLast, mergeMap } from 'rxjs/operators';
import { expect } from 'chai';
import { of } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand Down Expand Up @@ -205,4 +206,25 @@ describe('takeLast operator', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should not throw when count is Infinity and should emit all values', () => {
const results: number[] = [];
expect(() => {
of(1, 2, 3)
.pipe(takeLast(Infinity))
.subscribe((x) => results.push(x));
}).not.to.throw();
expect(results).to.deep.equal([1, 2, 3]);
});

it('should emit all values when count exceeds source length (Infinity case)', () => {
rxTest.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('(abc|)');
const expected = ' (abc|)';
const subs = ' (^!)';

expectObservable(source.pipe(takeLast(Infinity))).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
});
7 changes: 5 additions & 2 deletions packages/rxjs/src/internal/operators/takeLast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> {
? () => EMPTY
: (source) =>
new Observable((destination) => {
// This is a ring buffer that will hold our values
let ring = new Array<T>(count);
// This is a ring buffer that will hold our values.
// For non-finite counts (e.g. Infinity), use a growable array because
// `new Array(Infinity)` throws a RangeError. The ring-buffer math still
// works correctly for Infinity since `n % Infinity === n` for all finite n.
let ring: T[] = isFinite(count) ? new Array<T>(count) : [];
// This counter is how we track where we are at in the ring buffer.
let counter = 0;
source.subscribe(
Expand Down