Skip to content

Commit 2c43af7

Browse files
Airbladerbenlesh
authored andcommittedMay 31, 2018
fix(delayWhen): Emit source value if duration selector completes synchronously (#3664)
* 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 * docs(delayWhen): Deprecate completion of notifier triggering source emission This deprecates the behavior that the completion of the notifier observable will cause the source emission to be emitted on the output observable.
1 parent d7bfc9d commit 2c43af7

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed
 

‎spec/operators/delayWhen-spec.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { of } from 'rxjs';
1+
import { of, EMPTY } from 'rxjs';
22
import { delayWhen } from 'rxjs/operators';
33
import { TestScheduler } from 'rxjs/testing';
44
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
@@ -106,6 +106,28 @@ describe('delayWhen operator', () => {
106106
expectObservable(result).toBe(expected);
107107
expectSubscriptions(e1.subscriptions).toBe(subs);
108108
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
109+
});
110+
111+
it('should emit if the selector completes synchronously', () => {
112+
const e1 = hot('a--|');
113+
const expected = 'a--|';
114+
const subs = '^ !';
115+
116+
const result = e1.pipe(delayWhen((x: any) => EMPTY));
117+
118+
expectObservable(result).toBe(expected);
119+
expectSubscriptions(e1.subscriptions).toBe(subs);
120+
});
121+
122+
it('should emit if the source completes synchronously and the selector completes synchronously', () => {
123+
const e1 = hot('(a|)');
124+
const expected = '(a|)';
125+
const subs = '(^!)';
126+
127+
const result = e1.pipe(delayWhen((x: any) => EMPTY));
128+
129+
expectObservable(result).toBe(expected);
130+
expectSubscriptions(e1.subscriptions).toBe(subs);
109131
});
110132

111133
it('should not emit if selector never emits', () => {

‎src/internal/operators/delayWhen.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { InnerSubscriber } from '../InnerSubscriber';
77
import { subscribeToResult } from '../util/subscribeToResult';
88
import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
99

10+
/* tslint:disable:max-line-length */
11+
/** @deprecated In future versions, empty notifiers will no longer re-emit the source value on the output observable. */
12+
export function delayWhen<T>(delayDurationSelector: (value: T) => Observable<never>, subscriptionDelay?: Observable<any>): MonoTypeOperatorFunction<T>;
13+
export function delayWhen<T>(delayDurationSelector: (value: T) => Observable<any>, subscriptionDelay?: Observable<any>): MonoTypeOperatorFunction<T>;
14+
/* tslint:disable:max-line-length */
15+
1016
/**
1117
* Delays the emission of items from the source Observable by a given time span
1218
* determined by the emissions of another Observable.
@@ -22,6 +28,8 @@ import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
2228
* argument, and should return an Observable, called the "duration" Observable.
2329
* The source value is emitted on the output Observable only when the duration
2430
* Observable emits a value or completes.
31+
* The completion of the notifier triggering the emission of the source value
32+
* is deprecated behavior and will be removed in future versions.
2533
*
2634
* Optionally, `delayWhen` takes a second argument, `subscriptionDelay`, which
2735
* is an Observable. When `subscriptionDelay` emits its first value or
@@ -79,7 +87,6 @@ class DelayWhenOperator<T> implements Operator<T, T> {
7987
class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
8088
private completed: boolean = false;
8189
private delayNotifierSubscriptions: Array<Subscription> = [];
82-
private values: Array<T> = [];
8390

8491
constructor(destination: Subscriber<T>,
8592
private delayDurationSelector: (value: T) => Observable<any>) {
@@ -126,15 +133,11 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
126133
subscription.unsubscribe();
127134

128135
const subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
129-
let value: T = null;
130-
131136
if (subscriptionIdx !== -1) {
132-
value = this.values[subscriptionIdx];
133137
this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
134-
this.values.splice(subscriptionIdx, 1);
135138
}
136139

137-
return value;
140+
return subscription.outerValue;
138141
}
139142

140143
private tryDelay(delayNotifier: Observable<any>, value: T): void {
@@ -144,8 +147,6 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
144147
this.add(notifierSubscription);
145148
this.delayNotifierSubscriptions.push(notifierSubscription);
146149
}
147-
148-
this.values.push(value);
149150
}
150151

151152
private tryComplete(): void {

0 commit comments

Comments
 (0)
Please sign in to comment.