Skip to content
Permalink
Browse files

fix(pairwise): make it recursion-proof (#4743)

  • Loading branch information...
thorn0 authored and benlesh committed May 9, 2019
1 parent 0a102fd commit 21ab2615d9b06868825a235b347da09590bf468d
Showing with 31 additions and 2 deletions.
  1. +24 −1 spec/operators/pairwise-spec.ts
  2. +7 −1 src/internal/operators/pairwise.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;

@@ -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']]);
});
});
@@ -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);
}
}
}

0 comments on commit 21ab261

Please sign in to comment.
You can’t perform that action at this time.