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(takeLast): fix takeLast behavior to emit correct order #1412

Merged
merged 1 commit into from
Mar 3, 2016
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
34 changes: 34 additions & 0 deletions spec/operators/takeLast-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ describe('Observable.prototype.takeLast()', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should take last three values', () => {
const e1 = cold('--a-----b----c---d--| ');
const e1subs = '^ ! ';
const expected = '--------------------(bcd|)';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer


expectObservable(e1.takeLast(3)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should take all element when try to take larger then source', () => {
const e1 = cold('--a-----b----c---d--| ');
const e1subs = '^ ! ';
const expected = '--------------------(abcd|)';

expectObservable(e1.takeLast(5)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should take all element when try to take exact', () => {
const e1 = cold('--a-----b----c---d--| ');
const e1subs = '^ ! ';
const expected = '--------------------(abcd|)';

expectObservable(e1.takeLast(4)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the above tests to use marble diagrams and virtual scheduling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, let me do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind keeping one synchronous test, of course, but the marble tests are more analogous to the issue that was reported (which was using an interval).


it('should not take any values', () => {
const e1 = cold('--a-----b----c---d--|');
const expected = '|';

expectObservable(e1.takeLast(0)).toBe(expected);
});

it('should work with empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
Expand Down
44 changes: 16 additions & 28 deletions src/operator/takeLast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,40 @@ class TakeLastOperator<T> implements Operator<T, T> {
}

class TakeLastSubscriber<T> extends Subscriber<T> {
private ring: T[];
private ring: Array<T> = new Array();
private count: number = 0;
private index: number = 0;

constructor(destination: Subscriber<T>, private total: number) {
super(destination);
this.ring = new Array(total);
}

protected _next(value: T): void {

let index = this.index;
const ring = this.ring;
const total = this.total;
const count = this.count;
const count = this.count++;

if (total > 1) {
if (count < total) {
this.count = count + 1;
this.index = index + 1;
} else if (index === 0) {
this.index = ++index;
} else if (index < total) {
this.index = index + 1;
} else {
this.index = index = 0;
}
} else if (count < total) {
this.count = total;
if (ring.length < total) {
ring.push(value);
} else {
const index = count % total;
ring[index] = value;
}

ring[index] = value;
}

protected _complete(): void {
const destination = this.destination;
let count = this.count;

let iter = -1;
const { ring, count, total, destination } = this;
let index = (total === 1 || count < total) ? 0 : this.index - 1;
if (count > 0) {
const total = this.count >= this.total ? this.total : this.count;
const ring = this.ring;

while (++iter < count) {
if (iter + index === total) {
index = total - iter;
for (let i = 0; i < total; i++) {
const idx = (count++) % total;
destination.next(ring[idx]);
}
destination.next(ring[iter + index]);
}

destination.complete();
}
}