Skip to content

Commit

Permalink
refactor(cdk-experimental/popover-edit): Remove use of zone onStable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba authored Mar 11, 2024
1 parent 3e91f91 commit 172c741
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 13 additions & 10 deletions src/cdk-experimental/popover-edit/edit-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable, OnDestroy, Self, NgZone} from '@angular/core';
import {Injectable, OnDestroy, Self, afterNextRender, inject, Injector} from '@angular/core';
import {ControlContainer} from '@angular/forms';
import {Observable, Subject} from 'rxjs';
import {take} from 'rxjs/operators';

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

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

private _injector = inject(Injector);

constructor(
@Self() private readonly _form: ControlContainer,
private readonly _editEventDispatcher: EditEventDispatcher<EditRef<FormValue>>,
private readonly _ngZone: NgZone,
) {
this._editEventDispatcher.setActiveEditRef(this);
}
Expand All @@ -44,14 +44,17 @@ export class EditRef<FormValue> implements OnDestroy {
* applicable.
*/
init(previousFormValue: FormValue | undefined): void {
// Wait for the zone to stabilize before caching the initial value.
// Wait for the next render before caching the initial value.
// This ensures that all form controls have been initialized.
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
this.updateRevertValue();
if (previousFormValue) {
this.reset(previousFormValue);
}
});
afterNextRender(
() => {
this.updateRevertValue();
if (previousFormValue) {
this.reset(previousFormValue);
}
},
{injector: this._injector},
);
}

ngOnDestroy(): void {
Expand Down
12 changes: 10 additions & 2 deletions src/cdk-experimental/popover-edit/table-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {FocusTrap} from '@angular/cdk/a11y';
import {OverlayRef, OverlaySizeConfig, PositionStrategy} from '@angular/cdk/overlay';
import {TemplatePortal} from '@angular/cdk/portal';
import {
afterRender,
AfterViewInit,
Directive,
ElementRef,
Expand Down Expand Up @@ -68,12 +69,18 @@ const MOUSE_MOVE_THROTTLE_TIME_MS = 10;
export class CdkEditable implements AfterViewInit, OnDestroy {
protected readonly destroyed = new Subject<void>();

private _rendered = new Subject();

constructor(
protected readonly elementRef: ElementRef,
protected readonly editEventDispatcher: EditEventDispatcher<EditRef<unknown>>,
protected readonly focusDispatcher: FocusDispatcher,
protected readonly ngZone: NgZone,
) {}
) {
afterRender(() => {
this._rendered.next();
});
}

ngAfterViewInit(): void {
this._listenForTableEvents();
Expand All @@ -82,6 +89,7 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
ngOnDestroy(): void {
this.destroyed.next();
this.destroyed.complete();
this._rendered.complete();
}

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

0 comments on commit 172c741

Please sign in to comment.