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(throttle): no longer emits more than necessary in sync/sync trailing case #6059

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion spec/operators/throttle-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { throttle, mergeMap, mapTo, take } from 'rxjs/operators';
import { of, concat, timer, Observable } from 'rxjs';

/** @test {throttle} */
describe('throttle operator', () => {
describe('throttle', () => {
it('should immediately emit the first value in each time window', () => {
const e1 = hot('-a-xy-----b--x--cxxx-|');
const e1subs = '^ !';
Expand All @@ -21,6 +21,20 @@ describe('throttle operator', () => {
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should handle sync source with sync notifier and trailing appropriately', () => {
let results: any[] = [];
const source = of(1).pipe(
throttle(() => of(1), { leading: false, trailing: true })
);

source.subscribe({
next: value => results.push(value),
complete: () => results.push('done')
});

expect(results).to.deep.equal([1, 'done'])
});

it('should simply mirror the source if values are not emitted often enough', () => {
const e1 = hot('-a--------b-----c----|');
const e1subs = '^ !';
Expand Down
13 changes: 9 additions & 4 deletions src/internal/operators/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ export function throttle<T>(

const send = () => {
if (hasValue) {
subscriber.next(sendValue!);
!isComplete && startThrottle(sendValue!);
// Ensure we clear out our value and hasValue flag
// before we emit, otherwise reentrant code can cause
// issues here.
hasValue = false;
const value = sendValue!;
sendValue = null;
// Emit the value.
subscriber.next(value);
!isComplete && startThrottle(value);
}
hasValue = false;
sendValue = null;
};

source.subscribe(
Expand Down