refactor(core): Remove RootViewRef<T> because it is the same as ViewRef<T> (#52430)

`RootViewRef<T>` extends `ViewRef<T>` and overrides 3 methods with behavior
that is identical to `ViewRef<T>`. This commit removes `RootViewRef<T>`
because it is not needed.

PR Close #52430
This commit is contained in:
Andrew Scott 2023-10-27 15:49:14 -07:00 committed by Andrew Scott
parent 289a178ef0
commit 33da677ecc
16 changed files with 22 additions and 113 deletions

View file

@ -34,7 +34,7 @@ import {COMPILER_OPTIONS, CompilerOptions} from './linker/compiler';
import {ComponentFactory, ComponentRef} from './linker/component_factory';
import {ComponentFactoryResolver} from './linker/component_factory_resolver';
import {InternalNgModuleRef, NgModuleFactory, NgModuleRef} from './linker/ng_module_factory';
import {InternalViewRef, ViewRef} from './linker/view_ref';
import {ViewRef} from './linker/view_ref';
import {isComponentResourceResolutionQueueEmpty, resolveComponentResources} from './metadata/resource_loading';
import {assertNgModuleType} from './render3/assert';
import {ComponentFactory as R3ComponentFactory} from './render3/component_ref';
@ -44,6 +44,7 @@ import {setLocaleId} from './render3/i18n/i18n_locale_id';
import {setJitOptions} from './render3/jit/jit_options';
import {createNgModuleRefWithProviders, EnvironmentNgModuleRefAdapter, NgModuleFactory as R3NgModuleFactory} from './render3/ng_module_ref';
import {publishDefaultGlobalUtils as _publishDefaultGlobalUtils} from './render3/util/global_utils';
import {ViewRef as InternalViewRef} from './render3/view_ref';
import {TESTABILITY} from './testability/testability';
import {isPromise} from './util/lang';
import {stringify} from './util/stringify';
@ -825,7 +826,7 @@ export class ApplicationRef {
private _destroyed = false;
private _destroyListeners: Array<() => void> = [];
/** @internal */
_views: InternalViewRef[] = [];
_views: InternalViewRef<unknown>[] = [];
private readonly internalErrorHandler = inject(INTERNAL_APPLICATION_ERROR_HANDLER);
private readonly zoneIsStable = inject(ZONE_IS_STABLE_OBSERVABLE);
@ -1077,7 +1078,7 @@ export class ApplicationRef {
*/
attachView(viewRef: ViewRef): void {
(typeof ngDevMode === 'undefined' || ngDevMode) && this.warnIfDestroyed();
const view = (viewRef as InternalViewRef);
const view = (viewRef as InternalViewRef<unknown>);
this._views.push(view);
view.attachToAppRef(this);
}
@ -1087,7 +1088,7 @@ export class ApplicationRef {
*/
detachView(viewRef: ViewRef): void {
(typeof ngDevMode === 'undefined' || ngDevMode) && this.warnIfDestroyed();
const view = (viewRef as InternalViewRef);
const view = (viewRef as InternalViewRef<unknown>);
remove(this._views, view);
view.detachFromAppRef();
}

View file

@ -101,11 +101,6 @@ export abstract class EmbeddedViewRef<C> extends ViewRef {
abstract get rootNodes(): any[];
}
export interface InternalViewRef extends ViewRef {
detachFromAppRef(): void;
attachToAppRef(appRef: ViewRefTracker): void;
}
/**
* Interface for tracking root `ViewRef`s in `ApplicationRef`.
*

View file

@ -51,7 +51,7 @@ import {computeStaticStyling} from './styling/static_styling';
import {mergeHostAttrs, setUpAttributes} from './util/attrs_utils';
import {debugStringifyTypeForError, stringifyForError} from './util/stringify_utils';
import {getComponentLViewByIndex, getNativeByTNode, getTNode} from './util/view_utils';
import {RootViewRef, ViewRef} from './view_ref';
import {ViewRef} from './view_ref';
export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
/**
@ -319,7 +319,11 @@ export class ComponentRef<T> extends AbstractComponentRef<T> {
private _tNode: TElementNode|TContainerNode|TElementContainerNode) {
super();
this.instance = instance;
this.hostView = this.changeDetectorRef = new RootViewRef<T>(_rootLView);
this.hostView = this.changeDetectorRef = new ViewRef<T>(
_rootLView,
undefined, /* _cdRefInjectingView */
false, /* notifyErrorHandler */
);
this.componentType = componentType;
}

View file

@ -8,7 +8,7 @@
import {ChangeDetectorRef} from '../change_detection/change_detector_ref';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {EmbeddedViewRef, InternalViewRef, ViewRefTracker} from '../linker/view_ref';
import {EmbeddedViewRef, ViewRefTracker} from '../linker/view_ref';
import {removeFromArray} from '../util/array_utils';
import {assertEqual} from '../util/assert';
@ -27,7 +27,7 @@ import {storeLViewOnDestroy, updateAncestorTraversalFlagsOnAttach} from './util/
// the multiple @extends by making the annotation @implements instead
interface ChangeDetectorRefInterface extends ChangeDetectorRef {}
export class ViewRef<T> implements EmbeddedViewRef<T>, InternalViewRef, ChangeDetectorRefInterface {
export class ViewRef<T> implements EmbeddedViewRef<T>, ChangeDetectorRefInterface {
private _appRef: ViewRefTracker|null = null;
private _attachedToViewContainer = false;
@ -57,7 +57,7 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, InternalViewRef, ChangeDe
*
* This may be different from `_lView` if the `_cdRefInjectingView` is an embedded view.
*/
private _cdRefInjectingView?: LView) {}
private _cdRefInjectingView?: LView, private readonly notifyErrorHandler = true) {}
get context(): T {
return this._lView[CONTEXT] as unknown as T;
@ -284,7 +284,8 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, InternalViewRef, ChangeDe
* See {@link ChangeDetectorRef#detach} for more information.
*/
detectChanges(): void {
detectChangesInternal(this._lView[TVIEW], this._lView, this.context as unknown as {});
detectChangesInternal(
this._lView[TVIEW], this._lView, this.context as unknown as {}, this.notifyErrorHandler);
}
/**
@ -295,7 +296,8 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, InternalViewRef, ChangeDe
*/
checkNoChanges(): void {
if (ngDevMode) {
checkNoChangesInternal(this._lView[TVIEW], this._lView, this.context as unknown as {});
checkNoChangesInternal(
this._lView[TVIEW], this._lView, this.context as unknown as {}, this.notifyErrorHandler);
}
}
@ -322,30 +324,3 @@ export class ViewRef<T> implements EmbeddedViewRef<T>, InternalViewRef, ChangeDe
this._appRef = appRef;
}
}
/** @internal */
export class RootViewRef<T> extends ViewRef<T> {
constructor(public _view: LView) {
super(_view);
}
override detectChanges(): void {
const lView = this._view;
const tView = lView[TVIEW];
const context = lView[CONTEXT];
detectChangesInternal(tView, lView, context, false);
}
override checkNoChanges(): void {
if (ngDevMode) {
const lView = this._view;
const tView = lView[TVIEW];
const context = lView[CONTEXT];
checkNoChangesInternal(tView, lView, context, false);
}
}
override get context(): T {
return null!;
}
}

View file

@ -7,7 +7,7 @@
*/
import {ApplicationRef, ChangeDetectorRef, Component, ComponentRef, createComponent, ElementRef, EmbeddedViewRef, EnvironmentInjector, Injector, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
import {InternalViewRef} from '@angular/core/src/linker/view_ref';
import {ViewRef as InternalViewRef} from '@angular/core/src/render3/view_ref';
import {TestBed} from '@angular/core/testing';
@ -25,12 +25,12 @@ describe('ViewRef', () => {
create() {
this.componentRef = createComponent(DynamicComponent, {environmentInjector: this.injector});
(this.componentRef.hostView as InternalViewRef).attachToAppRef(this.appRef);
(this.componentRef.hostView as InternalViewRef<unknown>).attachToAppRef(this.appRef);
document.body.appendChild(this.componentRef.instance.elRef.nativeElement);
}
destroy() {
(this.componentRef.hostView as InternalViewRef).detachFromAppRef();
(this.componentRef.hostView as InternalViewRef<unknown>).detachFromAppRef();
}
}
@ -54,7 +54,7 @@ describe('ViewRef', () => {
@Component({template: ''})
class App {
constructor(changeDetectorRef: ChangeDetectorRef) {
(changeDetectorRef as InternalViewRef).onDestroy(() => called = true);
(changeDetectorRef as InternalViewRef<unknown>).onDestroy(() => called = true);
}
}

View file

@ -443,9 +443,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -815,9 +812,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -485,9 +485,6 @@
{
"name": "RootComponent"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -878,9 +875,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -368,9 +368,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -659,9 +656,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -419,9 +419,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -749,9 +746,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -506,9 +506,6 @@
{
"name": "RootComponent"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -902,9 +899,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -494,9 +494,6 @@
{
"name": "RootComponent"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -872,9 +869,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -281,9 +281,6 @@
{
"name": "RendererFactory2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -518,9 +515,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -428,9 +428,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -743,9 +740,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -629,9 +629,6 @@
{
"name": "ResolveStart"
},
{
"name": "RootViewRef"
},
{
"name": "RouteConfigLoadEnd"
},
@ -1172,9 +1169,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -332,9 +332,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -590,9 +587,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},

View file

@ -395,9 +395,6 @@
{
"name": "RendererStyleFlags2"
},
{
"name": "RootViewRef"
},
{
"name": "RuntimeError"
},
@ -788,9 +785,6 @@
{
"name": "detectChangesInViewIfAttached"
},
{
"name": "detectChangesInternal"
},
{
"name": "diPublicInInjector"
},