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.

fixes #3663
  • Loading branch information
Airblader committed May 7, 2018
1 parent 7e4cef4 commit 1aa845d
Show file tree
Hide file tree
Showing 3 changed files with 7,727 additions and 8 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
9 changes: 1 addition & 8 deletions src/internal/operators/delayWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class DelayWhenOperator<T> implements Operator<T, T> {
class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
private completed: boolean = false;
private delayNotifierSubscriptions: Array<Subscription> = [];
private values: Array<T> = [];

constructor(destination: Subscriber<T>,
private delayDurationSelector: (value: T) => Observable<any>) {
Expand Down Expand Up @@ -126,15 +125,11 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
subscription.unsubscribe();

const subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
let value: T = null;

if (subscriptionIdx !== -1) {
value = this.values[subscriptionIdx];
this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
this.values.splice(subscriptionIdx, 1);
}

return value;
return subscription.outerValue;
}

private tryDelay(delayNotifier: Observable<any>, value: T): void {
Expand All @@ -144,8 +139,6 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
this.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
}

this.values.push(value);
}

private tryComplete(): void {
Expand Down
Loading

0 comments on commit 1aa845d

Please sign in to comment.