Skip to content

Commit fd690a6

Browse files
MatthiasKunnenbenlesh
authored andcommittedApr 23, 2019
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

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed
 

‎spec/operators/throttleTime-spec.ts

+26-6
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
const e1 = hot('-a-xy-----b--x--cxxx--|');
146146
const e1subs = '^ !';
147-
const t = time( '----| ');
147+
const t = time( '----| ');
148148
const expected = '-a---y----b---x-c---x-|';
149149

150150
const result = 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+
const e1 = hot('-a--------------------|');
158+
const t = time('----| ');
159+
const expected = '-a--------------------|';
160+
161+
const result = 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
const e1 = hot('-a-xy-----b--x--cxxx--|');
160170
const e1subs = '^ !';
161-
const t = time( '----| ');
171+
const t = time( '----| ');
162172
const expected = '-----y--------x-----x-|';
163173

164174
const result = 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
const e1 = hot('-a-xy-----b--x--cxx|');
172182
const e1subs = '^ !';
173-
const t = time('----| ');
183+
const t = time('----| ');
174184
const expected = '-----y--------x----(x|)';
175185

176186
const result = 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+
const e1 = hot('-a--------------------|');
194+
const t = time('----| ');
195+
const expected = '-----a----------------|';
196+
197+
const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: false, trailing: true }));
198+
199+
expectObservable(result).toBe(expected);
200+
});
181201
});
182202
});

‎src/internal/operators/throttleTime.ts

+3
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+
} else if (this.trailing) {
137+
this._trailingValue = value;
138+
this._hasTrailingValue = true;
136139
}
137140
}
138141
}

0 commit comments

Comments
 (0)
Please sign in to comment.