Skip to content

Commit a9886a1

Browse files
authored
fix(cdk/a11y): complete input modality streams on destroy (#23522)
Fixes that we weren't completing the event streams from the `InputModalityDetector` when it is destroyed.
1 parent c608df8 commit a9886a1

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/cdk/a11y/input-modality/input-modality-detector.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,20 @@ describe('InputModalityDetector', () => {
194194
dispatchMouseEvent(document, 'mousedown');
195195
expect(detector.mostRecentModality).toBe('mouse');
196196
}));
197+
198+
it('should complete the various observables on destroy', () => {
199+
setupTest();
200+
201+
const modalityDetectedSpy = jasmine.createSpy('modalityDetected complete spy');
202+
const modalityChangedSpy = jasmine.createSpy('modalityChanged complete spy');
203+
204+
detector.modalityDetected.subscribe({complete: modalityDetectedSpy});
205+
detector.modalityChanged.subscribe({complete: modalityChangedSpy});
206+
207+
detector.ngOnDestroy();
208+
209+
expect(modalityDetectedSpy).toHaveBeenCalled();
210+
expect(modalityChangedSpy).toHaveBeenCalled();
211+
});
212+
197213
});

src/cdk/a11y/input-modality/input-modality-detector.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const modalityEventListenerOptions = normalizePassiveListenerOptions({
8787
* update the input modality to keyboard, but in general this service's behavior is largely
8888
* undefined.
8989
*/
90-
@Injectable({ providedIn: 'root' })
90+
@Injectable({providedIn: 'root'})
9191
export class InputModalityDetector implements OnDestroy {
9292
/** Emits whenever an input modality is detected. */
9393
readonly modalityDetected: Observable<InputModality>;
@@ -185,21 +185,22 @@ export class InputModalityDetector implements OnDestroy {
185185

186186
// If we're not in a browser, this service should do nothing, as there's no relevant input
187187
// modality to detect.
188-
if (!_platform.isBrowser) { return; }
189-
190-
// Add the event listeners used to detect the user's input modality.
191-
ngZone.runOutsideAngular(() => {
192-
document.addEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
193-
document.addEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
194-
document.addEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
195-
});
188+
if (_platform.isBrowser) {
189+
ngZone.runOutsideAngular(() => {
190+
document.addEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
191+
document.addEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
192+
document.addEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
193+
});
194+
}
196195
}
197196

198197
ngOnDestroy() {
199-
if (!this._platform.isBrowser) { return; }
198+
this._modality.complete();
200199

201-
document.removeEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
202-
document.removeEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
203-
document.removeEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
200+
if (this._platform.isBrowser) {
201+
document.removeEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
202+
document.removeEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
203+
document.removeEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
204+
}
204205
}
205206
}

0 commit comments

Comments
 (0)