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): correctly handle synchronous duration observable #2589

Merged
merged 1 commit into from
May 3, 2017
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
13 changes: 13 additions & 0 deletions spec/operators/delayWhen-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Rx from '../../dist/cjs/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expect } from 'chai';

declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
Expand Down Expand Up @@ -215,4 +216,16 @@ describe('Observable.prototype.delayWhen', () => {
expectSubscriptions(selector.subscriptions).toBe([]);
expectSubscriptions(subDelay.subscriptions).toBe(subDelaySub);
});

it('should complete when duration selector returns synchronous observable', () => {
let next: boolean = false;
let complete: boolean = false;

Rx.Observable.of(1)
.delayWhen(() => Rx.Observable.of(2))
.subscribe(() => next = true, null, () => complete = true);

expect(next).to.be.true;
expect(complete).to.be.true;
});
});
11 changes: 7 additions & 4 deletions src/operator/delayWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function delayWhen<T>(this: Observable<T>, delayDurationSelector: (value:
subscriptionDelay?: Observable<any>): Observable<T> {
if (subscriptionDelay) {
return new SubscriptionDelayObservable(this, subscriptionDelay)
.lift(new DelayWhenOperator(delayDurationSelector));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, these sorts of formatting corrections should be in a separate commit for easier review.

.lift(new DelayWhenOperator(delayDurationSelector));
}
return this.lift(new DelayWhenOperator(delayDurationSelector));
}
Expand Down Expand Up @@ -112,7 +112,7 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
this.tryDelay(delayNotifier, value);
}
} catch (err) {
this.destination.error(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

this.destination.error(err);
}
}

Expand All @@ -138,9 +138,12 @@ class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {

private tryDelay(delayNotifier: Observable<any>, value: T): void {
const notifierSubscription = subscribeToResult(this, delayNotifier, value);
this.add(notifierSubscription);

this.delayNotifierSubscriptions.push(notifierSubscription);
if (notifierSubscription && !notifierSubscription.closed) {
this.add(notifierSubscription);
this.delayNotifierSubscriptions.push(notifierSubscription);
}

this.values.push(value);
}

Expand Down