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(groupBy): unsubscribe & cleanup GroupDurationSubscriber #2662

Merged
merged 6 commits into from
Jun 14, 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
33 changes: 33 additions & 0 deletions spec/operators/groupBy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
'-^--!',
'---^--!',
'------------^-!',
];
Copy link
Member

Choose a reason for hiding this comment

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

haha... so much cleaner than the for loop! 😄

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',
Expand Down
22 changes: 7 additions & 15 deletions src/operator/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,19 @@ class GroupDurationSubscriber<K, T> extends Subscriber<T> {
constructor(private key: K,
private group: Subject<T>,
private parent: GroupBySubscriber<any, K, T>) {
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);
}
}

Expand Down