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(debounceTime): synchronous reentrancy of debounceTime no longer swallows the second value #3218

Merged
merged 1 commit into from
Jan 8, 2018
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: 16 additions & 0 deletions spec/operators/debounce-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,20 @@ describe('Observable.prototype.debounce', () => {
done(new Error('should not be called'));
});
});

it('should debounce correctly when synchronously reentered', () => {
const results = [];
const source = new Rx.Subject();

source.debounce(() => Observable.of(null)).subscribe(value => {
results.push(value);

if (value === 1) {
source.next(2);
}
});
source.next(1);

expect(results).to.deep.equal([1, 2]);
});
});
20 changes: 20 additions & 0 deletions spec/operators/debounceTime-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { VirtualTimeScheduler } from '../../src/scheduler/VirtualTimeScheduler';

declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
Expand Down Expand Up @@ -153,4 +155,22 @@ describe('Observable.prototype.debounceTime', () => {
expectObservable(e1.debounceTime(40, rxTestScheduler)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should debounce correctly when synchronously reentered', () => {
const results = [];
const source = new Rx.Subject();
const scheduler = new VirtualTimeScheduler();

source.debounceTime(0, scheduler).subscribe(value => {
results.push(value);

if (value === 1) {
source.next(2);
}
});
source.next(1);
scheduler.flush();

expect(results).to.deep.equal([1, 2]);
});
});
5 changes: 5 additions & 0 deletions src/operators/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
subscription.unsubscribe();
this.remove(subscription);
}
// This must be done *before* passing the value
// along to the destination because it's possible for
// the value to synchronously re-enter this operator
// recursively if the duration selector Observable
// emits synchronously
this.value = null;
this.hasValue = false;
super._next(value);
Expand Down
8 changes: 7 additions & 1 deletion src/operators/debounceTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ class DebounceTimeSubscriber<T> extends Subscriber<T> {
this.clearDebounce();

if (this.hasValue) {
this.destination.next(this.lastValue);
const { lastValue } = this;
// This must be done *before* passing the value
// along to the destination because it's possible for
// the value to synchronously re-enter this operator
// recursively when scheduled with things like
// VirtualScheduler/TestScheduler.
this.lastValue = null;
this.hasValue = false;
this.destination.next(lastValue);
}
}

Expand Down