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(cdk/a11y): complete input modality streams on destroy #23522

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/cdk/a11y/input-modality/input-modality-detector.spec.ts
Expand Up @@ -194,4 +194,20 @@ describe('InputModalityDetector', () => {
dispatchMouseEvent(document, 'mousedown');
expect(detector.mostRecentModality).toBe('mouse');
}));

it('should complete the various observables on destroy', () => {
setupTest();

const modalityDetectedSpy = jasmine.createSpy('modalityDetected complete spy');
const modalityChangedSpy = jasmine.createSpy('modalityChanged complete spy');

detector.modalityDetected.subscribe({complete: modalityDetectedSpy});
detector.modalityChanged.subscribe({complete: modalityChangedSpy});

detector.ngOnDestroy();

expect(modalityDetectedSpy).toHaveBeenCalled();
expect(modalityChangedSpy).toHaveBeenCalled();
});

});
27 changes: 14 additions & 13 deletions src/cdk/a11y/input-modality/input-modality-detector.ts
Expand Up @@ -87,7 +87,7 @@ const modalityEventListenerOptions = normalizePassiveListenerOptions({
* update the input modality to keyboard, but in general this service's behavior is largely
* undefined.
*/
@Injectable({ providedIn: 'root' })
@Injectable({providedIn: 'root'})
export class InputModalityDetector implements OnDestroy {
/** Emits whenever an input modality is detected. */
readonly modalityDetected: Observable<InputModality>;
Expand Down Expand Up @@ -185,21 +185,22 @@ export class InputModalityDetector implements OnDestroy {

// If we're not in a browser, this service should do nothing, as there's no relevant input
// modality to detect.
if (!_platform.isBrowser) { return; }

// Add the event listeners used to detect the user's input modality.
ngZone.runOutsideAngular(() => {
document.addEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
document.addEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
document.addEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
});
if (_platform.isBrowser) {
ngZone.runOutsideAngular(() => {
document.addEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
document.addEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
document.addEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
});
}
}

ngOnDestroy() {
if (!this._platform.isBrowser) { return; }
this._modality.complete();

document.removeEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
document.removeEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
document.removeEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
if (this._platform.isBrowser) {
document.removeEventListener('keydown', this._onKeydown, modalityEventListenerOptions);
document.removeEventListener('mousedown', this._onMousedown, modalityEventListenerOptions);
document.removeEventListener('touchstart', this._onTouchstart, modalityEventListenerOptions);
}
}
}