Skip to content

Commit b77952a

Browse files
authored
refactor(material/sidenav): Remove use of zone onStable to schedule drawer validation (#28682)
1 parent 5799c14 commit b77952a

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/material/sidenav/drawer.spec.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
discardPeriodicTasks,
88
flush,
99
} from '@angular/core/testing';
10-
import {Component, ElementRef, ViewChild} from '@angular/core';
10+
import {Component, ElementRef, ErrorHandler, ViewChild} from '@angular/core';
1111
import {By} from '@angular/platform-browser';
1212
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
1313
import {MatDrawer, MatSidenavModule, MatDrawerContainer} from './index';
@@ -461,21 +461,29 @@ describe('MatDrawer', () => {
461461
});
462462

463463
it('should throw when multiple drawers have the same position', fakeAsync(() => {
464+
const errorHandler = jasmine.createSpyObj(['handleError']);
465+
466+
TestBed.configureTestingModule({
467+
imports: [DrawerDynamicPosition],
468+
providers: [
469+
{
470+
provide: ErrorHandler,
471+
useValue: errorHandler,
472+
},
473+
],
474+
}).compileComponents();
475+
464476
const fixture = TestBed.createComponent(DrawerDynamicPosition);
465477
fixture.detectChanges();
466478
tick();
467479

468480
const testComponent: DrawerDynamicPosition = fixture.debugElement.componentInstance;
469481
testComponent.drawer1Position = 'end';
470482

471-
expect(() => {
472-
try {
473-
fixture.detectChanges();
474-
tick(0);
475-
} catch {
476-
tick(0);
477-
}
478-
}).toThrow();
483+
fixture.detectChanges();
484+
tick();
485+
486+
expect(errorHandler.handleError).toHaveBeenCalled();
479487
}));
480488

481489
it('should not throw when drawers swap positions', () => {

src/material/sidenav/drawer.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {DOCUMENT} from '@angular/common';
2222
import {
2323
AfterContentChecked,
2424
AfterContentInit,
25+
afterNextRender,
26+
AfterRenderPhase,
2527
AfterViewInit,
28+
ANIMATION_MODULE_TYPE,
2629
ChangeDetectionStrategy,
2730
ChangeDetectorRef,
2831
Component,
@@ -32,8 +35,10 @@ import {
3235
ElementRef,
3336
EventEmitter,
3437
forwardRef,
38+
inject,
3539
Inject,
3640
InjectionToken,
41+
Injector,
3742
Input,
3843
NgZone,
3944
OnDestroy,
@@ -42,18 +47,17 @@ import {
4247
QueryList,
4348
ViewChild,
4449
ViewEncapsulation,
45-
ANIMATION_MODULE_TYPE,
4650
} from '@angular/core';
4751
import {fromEvent, merge, Observable, Subject} from 'rxjs';
4852
import {
4953
debounceTime,
54+
distinctUntilChanged,
5055
filter,
5156
map,
57+
mapTo,
5258
startWith,
5359
take,
5460
takeUntil,
55-
distinctUntilChanged,
56-
mapTo,
5761
} from 'rxjs/operators';
5862
import {matDrawerAnimations} from './drawer-animations';
5963

@@ -750,6 +754,8 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
750754
return this._userContent || this._content;
751755
}
752756

757+
private _injector = inject(Injector);
758+
753759
constructor(
754760
@Optional() private _dir: Directionality,
755761
private _element: ElementRef<HTMLElement>,
@@ -933,9 +939,12 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
933939
// NOTE: We need to wait for the microtask queue to be empty before validating,
934940
// since both drawers may be swapping positions at the same time.
935941
drawer.onPositionChanged.pipe(takeUntil(this._drawers.changes)).subscribe(() => {
936-
this._ngZone.onMicrotaskEmpty.pipe(take(1)).subscribe(() => {
937-
this._validateDrawers();
938-
});
942+
afterNextRender(
943+
() => {
944+
this._validateDrawers();
945+
},
946+
{injector: this._injector, phase: AfterRenderPhase.Read},
947+
);
939948
});
940949
}
941950

0 commit comments

Comments
 (0)