Skip to content

Commit 87bbfc5

Browse files
crisbetojelbourn
authored andcommitted
fix(input): AutofillMonitor stream not being completed when stopping monitoring (#9886)
Currently we only remove the event listeners and CSS classes when we stop monitoring an element, however any subscriptions are still live. These changes complete the subject since it won't be reused if we decide to start monitoring again.
1 parent 53417d4 commit 87bbfc5

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/lib/input/autofill.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ describe('AutofillMonitor', () => {
137137
autofillMonitor.stopMonitoring(inputEl);
138138
expect(inputEl.classlist).not.toContain('mat-input-autofilled');
139139
});
140+
141+
it('should complete the stream when monitoring is stopped', () => {
142+
const element = testComponent.input1.nativeElement;
143+
const autofillStream = autofillMonitor.monitor(element);
144+
const spy = jasmine.createSpy('autofillStream complete');
145+
146+
autofillStream.subscribe(undefined, undefined, spy);
147+
expect(spy).not.toHaveBeenCalled();
148+
149+
autofillMonitor.stopMonitoring(element);
150+
expect(spy).toHaveBeenCalled();
151+
});
152+
140153
});
141154

142155
describe('matAutofill', () => {

src/lib/input/autofill.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,18 @@ export class AutofillMonitor implements OnDestroy {
9797
*/
9898
stopMonitoring(element: Element) {
9999
const info = this._monitoredElements.get(element);
100+
100101
if (info) {
101102
info.unlisten();
103+
info.subject.complete();
102104
element.classList.remove('mat-input-autofill-monitored');
103105
element.classList.remove('mat-input-autofilled');
104106
this._monitoredElements.delete(element);
105107
}
106108
}
107109

108110
ngOnDestroy() {
109-
this._monitoredElements.forEach(info => {
110-
info.unlisten();
111-
info.subject.complete();
112-
});
111+
this._monitoredElements.forEach((_info, element) => this.stopMonitoring(element));
113112
}
114113
}
115114

@@ -119,13 +118,14 @@ export class AutofillMonitor implements OnDestroy {
119118
selector: '[matAutofill]',
120119
})
121120
export class MatAutofill implements OnDestroy, OnInit {
122-
@Output() matAutofill = new EventEmitter<AutofillEvent>();
121+
@Output() matAutofill: EventEmitter<AutofillEvent> = new EventEmitter<AutofillEvent>();
123122

124123
constructor(private _elementRef: ElementRef, private _autofillMonitor: AutofillMonitor) {}
125124

126125
ngOnInit() {
127-
this._autofillMonitor.monitor(this._elementRef.nativeElement)
128-
.subscribe(event => this.matAutofill.emit(event));
126+
this._autofillMonitor
127+
.monitor(this._elementRef.nativeElement)
128+
.subscribe(event => this.matAutofill.emit(event));
129129
}
130130

131131
ngOnDestroy() {

0 commit comments

Comments
 (0)