refactor(core): extract dirty and markForRefresh from the private ViewRef. (#59122)

This allows better tree shaking from projects without the `element` package.

PR Close #59122
This commit is contained in:
Matthieu Riegler
2024-12-10 01:42:33 +01:00
committed by Miles Malerba
parent 31bbbe9665
commit 507ed58152
3 changed files with 21 additions and 20 deletions
@@ -281,7 +281,11 @@ export {compilePipe as ɵcompilePipe} from './render3/jit/pipe';
export {isNgModule as ɵisNgModule} from './render3/jit/util';
export {Profiler as ɵProfiler, ProfilerEvent as ɵProfilerEvent} from './render3/profiler_types';
export {GlobalDevModeUtils as ɵGlobalDevModeUtils} from './render3/util/global_utils';
export {ViewRef as ɵViewRef} from './render3/view_ref';
export {
ViewRef as ɵViewRef,
isViewDirty as ɵisViewDirty,
markForRefresh as ɵmarkForRefresh,
} from './render3/view_ref';
export {
bypassSanitizationTrustHtml as ɵbypassSanitizationTrustHtml,
bypassSanitizationTrustResourceUrl as ɵbypassSanitizationTrustResourceUrl,
+12 -17
View File
@@ -26,7 +26,6 @@ import {
LView,
LViewFlags,
PARENT,
REACTIVE_TEMPLATE_CONSUMER,
TVIEW,
} from './interfaces/view';
import {destroyLView, detachMovedView, detachViewFromDOM} from './node_manipulation';
@@ -35,6 +34,7 @@ import {
markViewForRefresh,
storeLViewOnDestroy,
updateAncestorTraversalFlagsOnAttach,
requiresRefreshOrTraversal,
} from './util/view_utils';
import {detachView, trackMovedView} from './view/container';
@@ -81,18 +81,6 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterfac
return this._lView[CONTEXT] as unknown as T;
}
/**
* Reports whether the given view is considered dirty according to the different marking mechanisms.
*/
get dirty(): boolean {
return (
!!(
this._lView[FLAGS] &
(LViewFlags.Dirty | LViewFlags.RefreshView | LViewFlags.HasChildViewsToRefresh)
) || !!this._lView[REACTIVE_TEMPLATE_CONSUMER]?.dirty
);
}
/**
* @deprecated Replacing the full context object is not supported. Modify the context
* directly, or consider using a `Proxy` if you need to replace the full object.
@@ -177,10 +165,6 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterfac
markViewDirty(this._cdRefInjectingView || this._lView, NotificationSource.MarkForCheck);
}
markForRefresh(): void {
markViewForRefresh(this._cdRefInjectingView || this._lView);
}
/**
* Detaches the view from the change detection tree.
*
@@ -382,3 +366,14 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterfac
updateAncestorTraversalFlagsOnAttach(this._lView);
}
}
/**
* Reports whether the given view is considered dirty according to the different marking mechanisms.
*/
export function isViewDirty(view: ViewRef<unknown>): boolean {
return requiresRefreshOrTraversal(view._lView) || !!(view._lView[FLAGS] & LViewFlags.Dirty);
}
export function markForRefresh(view: ViewRef<unknown>): void {
markViewForRefresh(view['_cdRefInjectingView'] || view._lView);
}
@@ -18,6 +18,8 @@ import {
ɵChangeDetectionScheduler as ChangeDetectionScheduler,
ɵNotificationSource as NotificationSource,
ɵViewRef as ViewRef,
ɵisViewDirty as isViewDirty,
ɵmarkForRefresh as markForRefresh,
OutputRef,
} from '@angular/core';
import {merge, Observable, ReplaySubject} from 'rxjs';
@@ -174,12 +176,12 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
this.componentRef!.setInput(this.inputMap.get(property) ?? property, value);
// `setInput` won't mark the view dirty if the input didn't change from its previous value.
if ((this.componentRef!.hostView as ViewRef<unknown>).dirty) {
if (isViewDirty(this.componentRef!.hostView as ViewRef<unknown>)) {
// `setInput` will have marked the view dirty already, but also mark it for refresh. This
// guarantees the view will be checked even if the input is being set from within change
// detection. This provides backwards compatibility, since we used to unconditionally
// schedule change detection in addition to the current zone run.
(this.componentRef!.changeDetectorRef as ViewRef<unknown>).markForRefresh();
markForRefresh(this.componentRef!.changeDetectorRef as ViewRef<unknown>);
// Notifying the scheduler with `NotificationSource.CustomElement` causes a `tick()` to be
// scheduled unconditionally, even if the scheduler is otherwise disabled.