Skip to content

Commit 21ab261

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

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎spec/operators/pairwise-spec.ts‎

Lines changed: 24 additions & 1 deletion
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
declarefunctionasDiagram(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+
constresults=newArray<[string,string]>();
111+
112+
constsubject=newSubject<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‎

Lines changed: 7 additions & 1 deletion
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+
letpair: [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)