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(pairwise): make it recursion-proof #4743

Merged
merged 1 commit into from May 9, 2019
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
25 changes: 24 additions & 1 deletion spec/operators/pairwise-spec.ts
@@ -1,5 +1,7 @@
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { pairwise } from 'rxjs/operators';
import { pairwise, take } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { expect } from 'chai';

declare function asDiagram(arg: string): Function;

Expand Down Expand Up @@ -103,4 +105,25 @@ describe('pairwise operator', () => {
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should be recursively re-enterable', () => {
const results = new Array<[string, string]>();

const subject = new Subject<string>();

subject
.pipe(
pairwise(),
take(3)
)
.subscribe(pair => {
results.push(pair);
subject.next('c');
});

subject.next('a');
subject.next('b');

expect(results).to.deep.equal([['a', 'b'], ['b', 'c'], ['c', 'c']]);
});
});
8 changes: 7 additions & 1 deletion src/internal/operators/pairwise.ts
Expand Up @@ -70,12 +70,18 @@ class PairwiseSubscriber<T> extends Subscriber<T> {
}

_next(value: T): void {
let pair: [T, T] | undefined;

if (this.hasPrev) {
this.destination.next([this.prev, value]);
pair = [this.prev, value];
} else {
this.hasPrev = true;
}

this.prev = value;

if (pair) {
this.destination.next(pair);
}
}
}