Skip to content

Commit b8e97b2

Browse files
crisbetommalerba
authored andcommitted
fix(accordion): complete accordion item emitters on destroy (#10858)
The `CdkAccordionItem` currently has a handful of event emitters that are being subscribed to internally, but are never being completed and their subscriptions are never dropped. These changes complete the emitters in order to avoid hanging subscriptions.
1 parent c976040 commit b8e97b2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/cdk/accordion/accordion-item.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,45 @@ describe('CdkAccordionItem', () => {
132132
expect(item.destroyed.emit).toHaveBeenCalled();
133133
});
134134
});
135+
136+
it('should emit to and complete the `destroyed` stream on destroy', () => {
137+
const emitSpy = jasmine.createSpy('emit spy');
138+
const completeSpy = jasmine.createSpy('complete spy');
139+
const subscription = item.destroyed.subscribe(emitSpy, undefined, completeSpy);
140+
141+
fixture.detectChanges();
142+
fixture.destroy();
143+
144+
expect(emitSpy).toHaveBeenCalled();
145+
expect(completeSpy).toHaveBeenCalled();
146+
147+
subscription.unsubscribe();
148+
});
149+
150+
it('should complete the `opened` stream on destroy', () => {
151+
const completeSpy = jasmine.createSpy('complete spy');
152+
const subscription = item.opened.subscribe(() => {}, undefined, completeSpy);
153+
154+
fixture.detectChanges();
155+
fixture.destroy();
156+
157+
expect(completeSpy).toHaveBeenCalled();
158+
159+
subscription.unsubscribe();
160+
});
161+
162+
it('should complete the `closed` stream on destroy', () => {
163+
const completeSpy = jasmine.createSpy('complete spy');
164+
const subscription = item.closed.subscribe(() => {}, undefined, completeSpy);
165+
166+
fixture.detectChanges();
167+
fixture.destroy();
168+
169+
expect(completeSpy).toHaveBeenCalled();
170+
171+
subscription.unsubscribe();
172+
});
173+
135174
});
136175

137176
describe('items without accordion', () => {

src/cdk/accordion/accordion-item.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ export class CdkAccordionItem implements OnDestroy {
109109

110110
/** Emits an event for the accordion item being destroyed. */
111111
ngOnDestroy() {
112+
this.opened.complete();
113+
this.closed.complete();
112114
this.destroyed.emit();
115+
this.destroyed.complete();
113116
this._removeUniqueSelectionListener();
114117
this._openCloseAllSubscription.unsubscribe();
115118
}

0 commit comments

Comments
 (0)