From fa098883a40c5d8d8618df5ba4cee073660e0827 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Sat, 19 Mar 2016 10:51:29 -0700 Subject: [PATCH] fix(bufferToggle): handle closingSelector completes immediately relates to #1487 --- spec/operators/bufferToggle-spec.ts | 12 ++++++++++++ src/operator/bufferToggle.ts | 25 +++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/spec/operators/bufferToggle-spec.ts b/spec/operators/bufferToggle-spec.ts index 4d74308e3f..1c6952f75a 100644 --- a/spec/operators/bufferToggle-spec.ts +++ b/spec/operators/bufferToggle-spec.ts @@ -380,4 +380,16 @@ describe('Observable.prototype.bufferToggle', () => { done.fail(); }); }); + + it('should handle empty closing observable', () => { + const e1 = hot('--a--^---b---c---d---e---f---g---h------|'); + const subs = '^ !'; + const e2 = cold('--x-----------y--------z---| '); + const expected = '--l-----------m--------n-----------|'; + + const result = e1.bufferToggle(e2, () => Observable.empty()); + + expectObservable(result).toBe(expected, {l: [], m: [], n: []}); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); }); \ No newline at end of file diff --git a/src/operator/bufferToggle.ts b/src/operator/bufferToggle.ts index 2969067f90..bbc23d3407 100644 --- a/src/operator/bufferToggle.ts +++ b/src/operator/bufferToggle.ts @@ -137,14 +137,14 @@ class BufferToggleSubscriber extends OuterSubscriber { private closeBuffer(context: BufferContext): void { const contexts = this.contexts; - if (contexts === null) { - return; + + if (contexts && context) { + const { buffer, subscription } = context; + this.destination.next(buffer); + contexts.splice(contexts.indexOf(context), 1); + this.remove(subscription); + subscription.unsubscribe(); } - const { buffer, subscription } = context; - this.destination.next(buffer); - contexts.splice(contexts.indexOf(context), 1); - this.remove(subscription); - subscription.unsubscribe(); } private trySubscribe(closingNotifier: any): void { @@ -156,10 +156,15 @@ class BufferToggleSubscriber extends OuterSubscriber { contexts.push(context); const innerSubscription = subscribeToResult(this, closingNotifier, context); - ( innerSubscription).context = context; - this.add(innerSubscription); - subscription.add(innerSubscription); + if (!innerSubscription.isUnsubscribed) { + ( innerSubscription).context = context; + + this.add(innerSubscription); + subscription.add(innerSubscription); + } else { + this.closeBuffer(context); + } } }