Skip to content

Commit

Permalink
fix(delayWhen): Emit source value if duration selector completes sync…
Browse files Browse the repository at this point in the history
…hronously

This fixes an issue where delayWhen would not re-emit a source emission if the duration selector
completed synchronously.

ReactiveX#3663
  • Loading branch information
Airblader committed May 7, 2018
1 parent 7e4cef4 commit 0031850
Show file tree
Hide file tree
Showing 3 changed files with 7,735 additions and 5 deletions.
23 changes: 23 additions & 0 deletions spec/operators/delayWhen-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Rx from 'rxjs/Rx';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { expect } from 'chai';
import { EMPTY } from 'rxjs';

declare function asDiagram(arg: string): Function;

Expand Down Expand Up @@ -104,6 +105,28 @@ describe('Observable.prototype.delayWhen', () => {
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
});

it('should emit if the selector completes synchronously', () => {
const e1 = hot('a--|');
const expected = 'a--|';
const subs = '^ !';

const result = e1.delayWhen((x: any) => EMPTY);

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

it('should emit if the source completes synchronously and the selector completes synchronously', () => {
const e1 = hot('(a|)');
const expected = '(a|)';
const subs = '(^!)';

const result = e1.delayWhen((x: any) => EMPTY);

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

it('should not emit if selector never emits', () => {
Expand Down
14 changes: 9 additions & 5 deletions src/internal/operators/delayWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
private tryDelay(delayNotifier: Observable<any>, value: T): void {
const notifierSubscription = subscribeToResult(this, delayNotifier, value);

if (notifierSubscription && !notifierSubscription.closed) {
this.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
if (notifierSubscription) {
if (notifierSubscription.closed) {
this.destination.next(value);
this.tryComplete();
} else {
this.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
this.values.push(value);
}
}

this.values.push(value);
}

private tryComplete(): void {
Expand Down
Loading

0 comments on commit 0031850

Please sign in to comment.