diff --git a/spec/operators/groupBy-spec.ts b/spec/operators/groupBy-spec.ts index 5a6d35d75f..6e4adf2771 100644 --- a/spec/operators/groupBy-spec.ts +++ b/spec/operators/groupBy-spec.ts @@ -903,6 +903,39 @@ describe('Observable.prototype.groupBy', () => { expectObservable(source, unsub).toBe(expected, expectedGroups); }); + it('should dispose a durationSelector after closing the group', + () => { + const obs = hot('-0-1--------2-|'); + const sub = '^ !' ; + let unsubs = [ + '-^--!', + '---^--!', + '------------^-!', + ]; + const dur = '---s'; + const durations = [ + cold(dur), + cold(dur), + cold(dur) + ]; + + const unsubscribedFrame = Rx.TestScheduler + .parseMarblesAsSubscriptions(sub) + .unsubscribedFrame; + + obs.groupBy( + (val: string) => val, + (val: string) => val, + (group: any) => durations[group.key] + ).subscribe(); + + rxTestScheduler.schedule(() => { + durations.forEach((d, i) => { + expectSubscriptions(d.subscriptions).toBe(unsubs[i]); + }); + }, unsubscribedFrame); + }); + it('should allow using a durationSelector, but keySelector throws', () => { const values = { a: ' foo', diff --git a/src/operator/groupBy.ts b/src/operator/groupBy.ts index c3f4ae8e8f..de801caf17 100644 --- a/src/operator/groupBy.ts +++ b/src/operator/groupBy.ts @@ -227,27 +227,19 @@ class GroupDurationSubscriber extends Subscriber { constructor(private key: K, private group: Subject, private parent: GroupBySubscriber) { - super(); + super(group); } protected _next(value: T): void { - this._complete(); - } - - protected _error(err: any): void { - const group = this.group; - if (!group.closed) { - group.error(err); - } - this.parent.removeGroup(this.key); + this.complete(); } - protected _complete(): void { - const group = this.group; - if (!group.closed) { - group.complete(); + protected _unsubscribe() { + const { parent, key } = this; + this.key = this.parent = null; + if (parent) { + parent.removeGroup(key); } - this.parent.removeGroup(this.key); } }