From 13c3f0e58176d61237f5adc20953dd3c95cf00e9 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 18 Mar 2016 12:07:59 -0700 Subject: [PATCH] fix(windowToggle): handle closingSelector completes immediately closes #1487 --- spec/operators/windowToggle-spec.ts | 18 ++++++++++++++++++ src/operator/windowToggle.ts | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/spec/operators/windowToggle-spec.ts b/spec/operators/windowToggle-spec.ts index a38e4a9a1f..f368f6c5e5 100644 --- a/spec/operators/windowToggle-spec.ts +++ b/spec/operators/windowToggle-spec.ts @@ -407,4 +407,22 @@ describe('Observable.prototype.windowToggle', () => { expectSubscriptions(e1.subscriptions).toBe(e1subs); expectSubscriptions(e2.subscriptions).toBe(e2subs); }); + + it ('should handle empty closing observable', () => { + const e1 = hot('--a--^---b---c---d---e---f---g---h------|'); + const e1subs = '^ !'; + const e2 = cold( '---o---------------o-----------| '); + const e2subs = '^ ! '; + const e3 = Observable.empty(); + const expected = '---x---------------y---------------|'; + const x = cold( '|'); + const y = cold( '|'); + const values = { x: x, y: y }; + + const result = e1.windowToggle(e2, () => e3); + + expectObservable(result).toBe(expected, values); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + expectSubscriptions(e2.subscriptions).toBe(e2subs); + }); }); \ No newline at end of file diff --git a/src/operator/windowToggle.ts b/src/operator/windowToggle.ts index cf9b05be4e..a8a1a32a7c 100644 --- a/src/operator/windowToggle.ts +++ b/src/operator/windowToggle.ts @@ -135,6 +135,10 @@ class WindowToggleSubscriber extends OuterSubscriber { subscription.add(innerSubscription); this.destination.next(window); + + if (innerSubscription.isUnsubscribed) { + this.closeWindow(this.contexts.length - 1); + } } } else { this.closeWindow(this.contexts.indexOf(outerValue)); @@ -152,6 +156,10 @@ class WindowToggleSubscriber extends OuterSubscriber { } closeWindow(index: number) { + if (index < 0) { + return; + } + const { contexts } = this; const context = contexts[index]; const { window, subscription } = context;