Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: evenly distribute values on throttleTime(fn, {leading: true, trailing: true}) #4864

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions spec/operators/throttleTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,27 @@ describe('throttleTime operator', () => {
});

describe('throttleTime(fn, { leading: true, trailing: true })', () => {
asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should immediately emit the first and last values in each time window', () => {
const e1 = hot('-a-xy-----b--x--cxxx--|');
const e1subs = '^ !';
asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should emit only one value in given time window', () => {
const e1 = hot('-a-xy-----b--x--cxxx---|');
const e1subs = '^ !';
const t = time( '----| ');
const expected = '-a---y----b---x-c---x-|';
const expected = '-a---y----b---x---x---x|';

const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: true, trailing: true }));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should handle a busy producer emitting a regular repeating sequence', () => {
const e1 = hot('abcdeabcdeabcdeabcdea|');
const subs = '^ !';
const expected = 'a----a----a----a----a|';

expectObservable(e1.pipe(throttleTime(50, rxTestScheduler, { leading: true, trailing: true }))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should emit the value if only a single one is given', () => {
const e1 = hot('-a--------------------|');
const t = time('----| ');
Expand All @@ -166,10 +175,10 @@ describe('throttleTime operator', () => {

describe('throttleTime(fn, { leading: false, trailing: true })', () => {
asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should immediately emit the last value in each time window', () => {
const e1 = hot('-a-xy-----b--x--cxxx--|');
const e1subs = '^ !';
const e1 = hot('-a-xy-----b--x--cxxx---|');
const e1subs = '^ !';
const t = time( '----| ');
const expected = '-----y--------x-----x-|';
const expected = '-----y--------x---x---x|';

const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: false, trailing: true }));

Expand All @@ -178,10 +187,10 @@ describe('throttleTime operator', () => {
});

it('should emit the last throttled value when complete', () => {
const e1 = hot('-a-xy-----b--x--cxx|');
const e1subs = '^ !';
const t = time('----| ');
const expected = '-----y--------x----(x|)';
const e1 = hot('-a-xy-----b--x-cxx|');
const e1subs = '^ !';
const t = time('----| ');
const expected = '-----y--------x---(x|)';

const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: false, trailing: true }));

Expand Down
5 changes: 4 additions & 1 deletion src/internal/operators/throttleTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> {
const throttled = this.throttled;
if (throttled) {
if (this.trailing && this._hasTrailingValue) {
this.add(this.throttled = this.scheduler.schedule<DispatchArg<T>>(dispatchNext, this.duration, { subscriber: this }));
this.destination.next(this._trailingValue);
this._trailingValue = null;
this._hasTrailingValue = false;
}
throttled.unsubscribe();
this.remove(throttled);
this.throttled = null;
if (throttled === this.throttled) {
this.throttled = null;
}
}
}
}
Expand Down