Skip to content

Commit 172c741

Browse files
authored
refactor(cdk-experimental/popover-edit): Remove use of zone onStable (#28674)
1 parent 3e91f91 commit 172c741

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/cdk-experimental/popover-edit/edit-ref.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, OnDestroy, Self, NgZone} from '@angular/core';
9+
import {Injectable, OnDestroy, Self, afterNextRender, inject, Injector} from '@angular/core';
1010
import {ControlContainer} from '@angular/forms';
1111
import {Observable, Subject} from 'rxjs';
12-
import {take} from 'rxjs/operators';
1312

1413
import {EditEventDispatcher} from './edit-event-dispatcher';
1514

@@ -30,10 +29,11 @@ export class EditRef<FormValue> implements OnDestroy {
3029
/** The value to set the form back to on revert. */
3130
private _revertFormValue: FormValue;
3231

32+
private _injector = inject(Injector);
33+
3334
constructor(
3435
@Self() private readonly _form: ControlContainer,
3536
private readonly _editEventDispatcher: EditEventDispatcher<EditRef<FormValue>>,
36-
private readonly _ngZone: NgZone,
3737
) {
3838
this._editEventDispatcher.setActiveEditRef(this);
3939
}
@@ -44,14 +44,17 @@ export class EditRef<FormValue> implements OnDestroy {
4444
* applicable.
4545
*/
4646
init(previousFormValue: FormValue | undefined): void {
47-
// Wait for the zone to stabilize before caching the initial value.
47+
// Wait for the next render before caching the initial value.
4848
// This ensures that all form controls have been initialized.
49-
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
50-
this.updateRevertValue();
51-
if (previousFormValue) {
52-
this.reset(previousFormValue);
53-
}
54-
});
49+
afterNextRender(
50+
() => {
51+
this.updateRevertValue();
52+
if (previousFormValue) {
53+
this.reset(previousFormValue);
54+
}
55+
},
56+
{injector: this._injector},
57+
);
5558
}
5659

5760
ngOnDestroy(): void {

src/cdk-experimental/popover-edit/table-directives.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {FocusTrap} from '@angular/cdk/a11y';
99
import {OverlayRef, OverlaySizeConfig, PositionStrategy} from '@angular/cdk/overlay';
1010
import {TemplatePortal} from '@angular/cdk/portal';
1111
import {
12+
afterRender,
1213
AfterViewInit,
1314
Directive,
1415
ElementRef,
@@ -68,12 +69,18 @@ const MOUSE_MOVE_THROTTLE_TIME_MS = 10;
6869
export class CdkEditable implements AfterViewInit, OnDestroy {
6970
protected readonly destroyed = new Subject<void>();
7071

72+
private _rendered = new Subject();
73+
7174
constructor(
7275
protected readonly elementRef: ElementRef,
7376
protected readonly editEventDispatcher: EditEventDispatcher<EditRef<unknown>>,
7477
protected readonly focusDispatcher: FocusDispatcher,
7578
protected readonly ngZone: NgZone,
76-
) {}
79+
) {
80+
afterRender(() => {
81+
this._rendered.next();
82+
});
83+
}
7784

7885
ngAfterViewInit(): void {
7986
this._listenForTableEvents();
@@ -82,6 +89,7 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
8289
ngOnDestroy(): void {
8390
this.destroyed.next();
8491
this.destroyed.complete();
92+
this._rendered.complete();
8593
}
8694

8795
private _listenForTableEvents(): void {
@@ -126,7 +134,7 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
126134
// Keep track of rows within the table. This is used to know which rows with hover content
127135
// are first or last in the table. They are kept focusable in case focus enters from above
128136
// or below the table.
129-
this.ngZone.onStable
137+
this._rendered
130138
.pipe(
131139
// Optimization: ignore dom changes while focus is within the table as we already
132140
// ensure that rows above and below the focused/active row are tabbable.

0 commit comments

Comments
 (0)