Skip to content

fix(cdk/a11y): live announcer promise never resolved if new announcement comes in #24700

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

Merged
merged 1 commit into from
Mar 30, 2022
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
10 changes: 10 additions & 0 deletions src/cdk/a11y/live-announcer/live-announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ describe('LiveAnnouncer', () => {
expect(spy).toHaveBeenCalled();
}));

it('should resolve the returned promise if another announcement is made before the timeout has expired', fakeAsync(() => {
const spy = jasmine.createSpy('announce spy');
announcer.announce('something').then(spy);
tick(10);
announcer.announce('something').then(spy);
tick(100);

expect(spy).toHaveBeenCalledTimes(2);
}));

it('should ensure that there is only one live element at a time', fakeAsync(() => {
fixture.destroy();

Expand Down
32 changes: 21 additions & 11 deletions src/cdk/a11y/live-announcer/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class LiveAnnouncer implements OnDestroy {
private _liveElement: HTMLElement;
private _document: Document;
private _previousTimeout: number;
private _currentPromise: Promise<void> | undefined;
private _currentResolve: (() => void) | undefined;

constructor(
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
Expand Down Expand Up @@ -115,17 +117,23 @@ export class LiveAnnouncer implements OnDestroy {
// second time without clearing and then using a non-zero delay.
// (using JAWS 17 at time of this writing).
return this._ngZone.runOutsideAngular(() => {
return new Promise(resolve => {
clearTimeout(this._previousTimeout);
this._previousTimeout = setTimeout(() => {
this._liveElement.textContent = message;
resolve();

if (typeof duration === 'number') {
this._previousTimeout = setTimeout(() => this.clear(), duration);
}
}, 100);
});
if (!this._currentPromise) {
this._currentPromise = new Promise(resolve => (this._currentResolve = resolve));
}

clearTimeout(this._previousTimeout);
this._previousTimeout = setTimeout(() => {
this._liveElement.textContent = message;

if (typeof duration === 'number') {
this._previousTimeout = setTimeout(() => this.clear(), duration);
}

this._currentResolve!();
this._currentPromise = this._currentResolve = undefined;
}, 100);

return this._currentPromise;
});
}

Expand All @@ -144,6 +152,8 @@ export class LiveAnnouncer implements OnDestroy {
clearTimeout(this._previousTimeout);
this._liveElement?.remove();
this._liveElement = null!;
this._currentResolve?.();
this._currentPromise = this._currentResolve = undefined;
}

private _createLiveElement(): HTMLElement {
Expand Down