From 21ab2615d9b06868825a235b347da09590bf468d Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Fri, 10 May 2019 02:52:39 +0300 Subject: [PATCH] fix(pairwise): make it recursion-proof (#4743) --- spec/operators/pairwise-spec.ts | 25 ++++++++++++++++++++++++- src/internal/operators/pairwise.ts | 8 +++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/spec/operators/pairwise-spec.ts b/spec/operators/pairwise-spec.ts index 48e2fb1b1f..2d9fb18a06 100644 --- a/spec/operators/pairwise-spec.ts +++ b/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; @@ -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(); + + 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']]); + }); }); diff --git a/src/internal/operators/pairwise.ts b/src/internal/operators/pairwise.ts index 209f90fad2..8c2eb26a3f 100644 --- a/src/internal/operators/pairwise.ts +++ b/src/internal/operators/pairwise.ts @@ -70,12 +70,18 @@ class PairwiseSubscriber extends Subscriber { } _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); + } } }