Skip to content

Commit fd690a6

Browse files
MatthiasKunnenbenlesh
authored andcommitted
fix(throttleTime): emit single value with trailing enabled (#4564)
* test(throttleTime): test single value with trailing enabled Test if throttleTime emits when only a single value is emitted from the source with leading enabled/disabled and trailing enabled. * fix(throttleTime): fix single value with leading disabled, trailing enabled Emit single value with leading disabled, trailing enabled. Closes#2859 and #4491. * chore: improve test descriptions And remove an asDiagram call made with the same argument. Signed-off-by: Matthias Kunnen <matthias.kunnen@gmail.com>
1 parent e460eec commit fd690a6

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎spec/operators/throttleTime-spec.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,34 @@ describe('throttleTime operator', () => {
141141
});
142142

143143
describe('throttleTime(fn, { leading: true, trailing: true })',()=>{
144-
asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should immediately emit the first value in each time window',()=>{
144+
asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should immediately emit the first and last values in each time window',()=>{
145145
conste1=hot('-a-xy-----b--x--cxxx--|');
146146
conste1subs='^ !';
147-
constt=time('----| ');
147+
constt=time('----| ');
148148
constexpected='-a---y----b---x-c---x-|';
149149

150150
constresult=e1.pipe(throttleTime(t,rxTestScheduler,{leading: true,trailing: true}));
151151

152152
expectObservable(result).toBe(expected);
153153
expectSubscriptions(e1.subscriptions).toBe(e1subs);
154154
});
155+
156+
it('should emit the value if only a single one is given',()=>{
157+
conste1=hot('-a--------------------|');
158+
constt=time('----| ');
159+
constexpected='-a--------------------|';
160+
161+
constresult=e1.pipe(throttleTime(t,rxTestScheduler,{leading: true,trailing: true}));
162+
163+
expectObservable(result).toBe(expected);
164+
});
155165
});
156166

157167
describe('throttleTime(fn, { leading: false, trailing: true })',()=>{
158-
asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should immediately emit the first value in each time window',()=>{
168+
asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should immediately emit the last value in each time window',()=>{
159169
conste1=hot('-a-xy-----b--x--cxxx--|');
160170
conste1subs='^ !';
161-
constt=time('----| ');
171+
constt=time('----| ');
162172
constexpected='-----y--------x-----x-|';
163173

164174
constresult=e1.pipe(throttleTime(t,rxTestScheduler,{leading: false,trailing: true}));
@@ -167,16 +177,26 @@ describe('throttleTime operator', () => {
167177
expectSubscriptions(e1.subscriptions).toBe(e1subs);
168178
});
169179

170-
asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should emit the last throttled value when complete',()=>{
180+
it('should emit the last throttled value when complete',()=>{
171181
conste1=hot('-a-xy-----b--x--cxx|');
172182
conste1subs='^ !';
173-
constt=time('----| ');
183+
constt=time('----| ');
174184
constexpected='-----y--------x----(x|)';
175185

176186
constresult=e1.pipe(throttleTime(t,rxTestScheduler,{leading: false,trailing: true}));
177187

178188
expectObservable(result).toBe(expected);
179189
expectSubscriptions(e1.subscriptions).toBe(e1subs);
180190
});
191+
192+
it('should emit the value if only a single one is given',()=>{
193+
conste1=hot('-a--------------------|');
194+
constt=time('----| ');
195+
constexpected='-----a----------------|';
196+
197+
constresult=e1.pipe(throttleTime(t,rxTestScheduler,{leading: false,trailing: true}));
198+
199+
expectObservable(result).toBe(expected);
200+
});
181201
});
182202
});

‎src/internal/operators/throttleTime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> {
133133
this.add(this.throttled=this.scheduler.schedule<DispatchArg<T>>(dispatchNext,this.duration,{subscriber: this}));
134134
if(this.leading){
135135
this.destination.next(value);
136+
}elseif(this.trailing){
137+
this._trailingValue=value;
138+
this._hasTrailingValue=true;
136139
}
137140
}
138141
}

0 commit comments

Comments
 (0)