Skip to content

Commit a4e15fa

Browse files
committed
race ANR / setTimeout
1 parent fd428ff commit a4e15fa

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/cdk/dialog/dialog-container.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,21 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
270270
break;
271271
case true:
272272
case 'first-tabbable':
273-
afterNextRender(
274-
() => {
275-
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
276-
// If we weren't able to find a focusable element in the dialog, then focus the dialog
277-
// container instead.
278-
if (!focusedSuccessfully) {
279-
this._focusDialogContainer();
280-
}
281-
},
282-
{injector: this._injector},
283-
);
273+
let timeoutRef: {};
274+
// TODO(mmalerba): Once hybrid mode is enabled in g3, we can change this to just
275+
// `afterNextRender`.
276+
new Promise<void>(r => {
277+
afterNextRender(r, {injector: this._injector});
278+
timeoutRef = setTimeout(r);
279+
}).then(() => {
280+
clearTimeout(timeoutRef as number);
281+
const focusedSuccessfully = this._focusTrap?.focusInitialElement();
282+
// If we weren't able to find a focusable element in the dialog, then focus the dialog
283+
// container instead.
284+
if (!focusedSuccessfully) {
285+
this._focusDialogContainer();
286+
}
287+
});
284288
break;
285289
case 'first-heading':
286290
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');

src/material/dialog/dialog.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('MDC-based MatDialog', () => {
165165
// callback should not be called before animation is complete
166166
expect(spy).not.toHaveBeenCalled();
167167

168-
flushMicrotasks();
168+
flush();
169169
expect(spy).toHaveBeenCalled();
170170
}));
171171

@@ -398,6 +398,7 @@ describe('MDC-based MatDialog', () => {
398398
dialogRef.keydownEvents().subscribe(spy);
399399

400400
viewContainerFixture.detectChanges();
401+
flush();
401402

402403
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
403404
let container = overlayContainerElement.querySelector('mat-dialog-container') as HTMLElement;
@@ -483,11 +484,11 @@ describe('MDC-based MatDialog', () => {
483484

484485
tick(500);
485486
viewContainerFixture.detectChanges();
486-
flushMicrotasks();
487487

488488
dialogRef = dialog.open(PizzaMsg, {maxWidth: '100px'});
489489

490490
viewContainerFixture.detectChanges();
491+
flush();
491492

492493
overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
493494

@@ -761,6 +762,7 @@ describe('MDC-based MatDialog', () => {
761762
};
762763

763764
dialog.open(PizzaMsg, {scrollStrategy});
765+
flush();
764766
expect(scrollStrategy.enable).toHaveBeenCalled();
765767
}));
766768

@@ -814,6 +816,7 @@ describe('MDC-based MatDialog', () => {
814816
it('should assign a unique id to each dialog', fakeAsync(() => {
815817
const one = dialog.open(PizzaMsg);
816818
const two = dialog.open(PizzaMsg);
819+
flush();
817820

818821
expect(one.id).toBeTruthy();
819822
expect(two.id).toBeTruthy();

0 commit comments

Comments
 (0)