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(delayWhen): no longer emits if duration selector is empty #5769

Merged
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
25 changes: 7 additions & 18 deletions spec/operators/delayWhen-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ describe('delayWhen operator', () => {
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
});

it('should delay by selector completes if selector does not emits', () => {
it('should delay, but not emit if the selector never emits a notification', () => {
const e1 = hot('--a--b--|');
const expected = '------a--(b|)';
const expected = '-----------|';
const subs = '^ !';
const selector = cold( '----|');
const selectorSubs = [' ^ !',
' ^ !'];
const selector = cold( '------|');
const selectorSubs = [' ^ !',
' ^ !'];

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

Expand All @@ -106,9 +106,9 @@ describe('delayWhen operator', () => {
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
});

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

const result = e1.pipe(delayWhen((x: any) => EMPTY));
Expand All @@ -117,17 +117,6 @@ describe('delayWhen operator', () => {
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.pipe(delayWhen((x: any) => EMPTY));

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

it('should not emit if selector never emits', () => {
const e1 = hot('--a--b--|');
const expected = '-';
Expand Down
71 changes: 3 additions & 68 deletions src/internal/operators/delayWhen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/** @prettier */
import { Observable } from '../Observable';
import { MonoTypeOperatorFunction } from '../types';
import { operate } from '../util/lift';
import { OperatorSubscriber } from './OperatorSubscriber';
import { concat } from '../observable/concat';
import { take } from './take';
import { ignoreElements } from './ignoreElements';
import { mapTo } from './mapTo';
import { mergeMap } from './mergeMap';

/* tslint:disable:max-line-length */
/** @deprecated In future versions, empty notifiers will no longer re-emit the source value on the output observable. */
export function delayWhen<T>(
delayDurationSelector: (value: T, index: number) => Observable<never>,
Expand All @@ -18,7 +17,6 @@ export function delayWhen<T>(
delayDurationSelector: (value: T, index: number) => Observable<any>,
subscriptionDelay?: Observable<any>
): MonoTypeOperatorFunction<T>;
/* tslint:disable:max-line-length */

/**
* Delays the emission of items from the source Observable by a given time span
Expand Down Expand Up @@ -89,68 +87,5 @@ export function delayWhen<T>(
concat(subscriptionDelay.pipe(take(1), ignoreElements()), source.pipe(delayWhen(delayDurationSelector)));
}

return operate((source, subscriber) => {
// An index to give to the projection function.
let index = 0;
// Whether or not the source has completed.
let isComplete = false;
// Tracks the number of actively delayed values we have.
let active = 0;

/**
* Checks to see if we can complete the result and completes it, if so.
*/
const checkComplete = () => isComplete && !active && subscriber.complete();

source.subscribe(
new OperatorSubscriber(
subscriber,
(value: T) => {
// Closed bit to guard reentrancy and
// synchronous next/complete (which both make the same calls right now)
let closed = false;

/**
* Notifies the consumer of the value.
*/
const notify = () => {
// Notify the consumer.
subscriber.next(value);

// Ensure our inner subscription is cleaned up
// as soon as possible. Once the first `next` fires,
// we have no more use for this subscription.
durationSubscriber?.unsubscribe();

if (!closed) {
active--;
closed = true;
checkComplete();
}
};

// We have to capture our duration subscriber so we can unsubscribe from
// it on the first next notification it gives us.
const durationSubscriber = new OperatorSubscriber(
subscriber,
notify,
// Errors are sent to consumer.
undefined,
// TODO(benlesh): I'm inclined to say this is _incorrect_ behavior.
// A completion should not be a notification. Note the deprecation above
notify
);

active++;
delayDurationSelector(value, index++).subscribe(durationSubscriber);
},
// Errors are passed through to consumer.
undefined,
() => {
isComplete = true;
checkComplete();
}
)
);
});
return mergeMap((value, index) => delayDurationSelector(value, index).pipe(take(1), mapTo(value)));
}