From 2fbda3fda79ce6e4e6ff3b67a32d86719342ba4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20B=C3=BCrk?= Date: Mon, 7 May 2018 22:32:42 +0200 Subject: [PATCH] fix(delayWhen): Emit source value if duration selector completes synchronously This fixes an issue where delayWhen would not re-emit a source emission if the duration selector completed synchronously. fixes #3663 --- spec/operators/delayWhen-spec.ts | 23 +++++++++++++++++++++++ src/internal/operators/delayWhen.ts | 9 +-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/spec/operators/delayWhen-spec.ts b/spec/operators/delayWhen-spec.ts index 35bdb76f7d2..a2094124f51 100644 --- a/spec/operators/delayWhen-spec.ts +++ b/spec/operators/delayWhen-spec.ts @@ -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; @@ -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', () => { diff --git a/src/internal/operators/delayWhen.ts b/src/internal/operators/delayWhen.ts index f2c0d4a702f..ef7e834f0f8 100644 --- a/src/internal/operators/delayWhen.ts +++ b/src/internal/operators/delayWhen.ts @@ -79,7 +79,6 @@ class DelayWhenOperator implements Operator { class DelayWhenSubscriber extends OuterSubscriber { private completed: boolean = false; private delayNotifierSubscriptions: Array = []; - private values: Array = []; constructor(destination: Subscriber, private delayDurationSelector: (value: T) => Observable) { @@ -126,15 +125,11 @@ class DelayWhenSubscriber extends OuterSubscriber { 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, value: T): void { @@ -144,8 +139,6 @@ class DelayWhenSubscriber extends OuterSubscriber { this.add(notifierSubscription); this.delayNotifierSubscriptions.push(notifierSubscription); } - - this.values.push(value); } private tryComplete(): void {