Skip to content

Commit 21ab261

Browse files
thorn0benlesh
authored andcommittedMay 9, 2019
fix(pairwise): make it recursion-proof (#4743)
1 parent 0a102fd commit 21ab261

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎spec/operators/pairwise-spec.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
2-
import { pairwise } from 'rxjs/operators';
2+
import { pairwise, take } from 'rxjs/operators';
3+
import { Subject } from 'rxjs';
4+
import { expect } from 'chai';
35

46
declare function asDiagram(arg: string): Function;
57

@@ -103,4 +105,25 @@ describe('pairwise operator', () => {
103105
expectObservable(source).toBe(expected);
104106
expectSubscriptions(e1.subscriptions).toBe(e1subs);
105107
});
108+
109+
it('should be recursively re-enterable', () => {
110+
const results = new Array<[string, string]>();
111+
112+
const subject = new Subject<string>();
113+
114+
subject
115+
.pipe(
116+
pairwise(),
117+
take(3)
118+
)
119+
.subscribe(pair => {
120+
results.push(pair);
121+
subject.next('c');
122+
});
123+
124+
subject.next('a');
125+
subject.next('b');
126+
127+
expect(results).to.deep.equal([['a', 'b'], ['b', 'c'], ['c', 'c']]);
128+
});
106129
});

‎src/internal/operators/pairwise.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,18 @@ class PairwiseSubscriber<T> extends Subscriber<T> {
7070
}
7171

7272
_next(value: T): void {
73+
let pair: [T, T] | undefined;
74+
7375
if (this.hasPrev) {
74-
this.destination.next([this.prev, value]);
76+
pair = [this.prev, value];
7577
} else {
7678
this.hasPrev = true;
7779
}
7880

7981
this.prev = value;
82+
83+
if (pair) {
84+
this.destination.next(pair);
85+
}
8086
}
8187
}

0 commit comments

Comments
 (0)
Please sign in to comment.