Skip to content

Commit 507ed58

Browse files
JeanMechemmalerba
authored andcommitted
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
1 parent 31bbbe9 commit 507ed58

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

packages/core/src/core_render3_private_export.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ export {compilePipe as ɵcompilePipe} from './render3/jit/pipe';
281281
export {isNgModule as ɵisNgModule} from './render3/jit/util';
282282
export {Profiler as ɵProfiler, ProfilerEvent as ɵProfilerEvent} from './render3/profiler_types';
283283
export {GlobalDevModeUtils as ɵGlobalDevModeUtils} from './render3/util/global_utils';
284-
export {ViewRef as ɵViewRef} from './render3/view_ref';
284+
export {
285+
ViewRef as ɵViewRef,
286+
isViewDirty as ɵisViewDirty,
287+
markForRefresh as ɵmarkForRefresh,
288+
} from './render3/view_ref';
285289
export {
286290
bypassSanitizationTrustHtml as ɵbypassSanitizationTrustHtml,
287291
bypassSanitizationTrustResourceUrl as ɵbypassSanitizationTrustResourceUrl,

packages/core/src/render3/view_ref.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
LView,
2727
LViewFlags,
2828
PARENT,
29-
REACTIVE_TEMPLATE_CONSUMER,
3029
TVIEW,
3130
} from './interfaces/view';
3231
import {destroyLView, detachMovedView, detachViewFromDOM} from './node_manipulation';
@@ -35,6 +34,7 @@ import {
3534
markViewForRefresh,
3635
storeLViewOnDestroy,
3736
updateAncestorTraversalFlagsOnAttach,
37+
requiresRefreshOrTraversal,
3838
} from './util/view_utils';
3939
import {detachView, trackMovedView} from './view/container';
4040

@@ -81,18 +81,6 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterfac
8181
return this._lView[CONTEXT] as unknown as T;
8282
}
8383

84-
/**
85-
* Reports whether the given view is considered dirty according to the different marking mechanisms.
86-
*/
87-
get dirty(): boolean {
88-
return (
89-
!!(
90-
this._lView[FLAGS] &
91-
(LViewFlags.Dirty | LViewFlags.RefreshView | LViewFlags.HasChildViewsToRefresh)
92-
) || !!this._lView[REACTIVE_TEMPLATE_CONSUMER]?.dirty
93-
);
94-
}
95-
9684
/**
9785
* @deprecated Replacing the full context object is not supported. Modify the context
9886
* 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
177165
markViewDirty(this._cdRefInjectingView || this._lView, NotificationSource.MarkForCheck);
178166
}
179167

180-
markForRefresh(): void {
181-
markViewForRefresh(this._cdRefInjectingView || this._lView);
182-
}
183-
184168
/**
185169
* Detaches the view from the change detection tree.
186170
*
@@ -382,3 +366,14 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterfac
382366
updateAncestorTraversalFlagsOnAttach(this._lView);
383367
}
384368
}
369+
370+
/**
371+
* Reports whether the given view is considered dirty according to the different marking mechanisms.
372+
*/
373+
export function isViewDirty(view: ViewRef<unknown>): boolean {
374+
return requiresRefreshOrTraversal(view._lView) || !!(view._lView[FLAGS] & LViewFlags.Dirty);
375+
}
376+
377+
export function markForRefresh(view: ViewRef<unknown>): void {
378+
markViewForRefresh(view['_cdRefInjectingView'] || view._lView);
379+
}

packages/elements/src/component-factory-strategy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
ɵChangeDetectionScheduler as ChangeDetectionScheduler,
1919
ɵNotificationSource as NotificationSource,
2020
ɵViewRef as ViewRef,
21+
ɵisViewDirty as isViewDirty,
22+
ɵmarkForRefresh as markForRefresh,
2123
OutputRef,
2224
} from '@angular/core';
2325
import {merge, Observable, ReplaySubject} from 'rxjs';
@@ -174,12 +176,12 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
174176
this.componentRef!.setInput(this.inputMap.get(property) ?? property, value);
175177

176178
// `setInput` won't mark the view dirty if the input didn't change from its previous value.
177-
if ((this.componentRef!.hostView as ViewRef<unknown>).dirty) {
179+
if (isViewDirty(this.componentRef!.hostView as ViewRef<unknown>)) {
178180
// `setInput` will have marked the view dirty already, but also mark it for refresh. This
179181
// guarantees the view will be checked even if the input is being set from within change
180182
// detection. This provides backwards compatibility, since we used to unconditionally
181183
// schedule change detection in addition to the current zone run.
182-
(this.componentRef!.changeDetectorRef as ViewRef<unknown>).markForRefresh();
184+
markForRefresh(this.componentRef!.changeDetectorRef as ViewRef<unknown>);
183185

184186
// Notifying the scheduler with `NotificationSource.CustomElement` causes a `tick()` to be
185187
// scheduled unconditionally, even if the scheduler is otherwise disabled.

0 commit comments

Comments
 (0)