refactor(core): Cleanup circular dependency between ViewEngine and Ivy `Renderer2`. (#39621)

`Renderer2` is declared in ViewEngine but it sub-classed in Ivy. This creates a circular
dependency between ViewEngine `Renderer2` which needs to declare `__NG_ELEMENT_ID__` and
ivy factory which needs to create it. The workaround used to be to pass the `Renderer2`
through stack but that created a very convoluted code. This refactoring simply bundles the
two files together and removes the stack workaround making the code simpler to follow.

PR Close #39621
This commit is contained in:
Misko Hevery 2020-11-09 20:08:59 -08:00 committed by atscott
parent 24b57d8b41
commit 585875c3f4
3 changed files with 26 additions and 64 deletions

View File

@ -149,7 +149,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/linker/view_ref.ts"
@ -163,7 +162,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -181,7 +179,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -206,7 +203,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -233,7 +229,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/assert.ts",
"packages/core/src/render3/definition.ts",
@ -883,7 +878,6 @@
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts",
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/linker/view_ref.ts"
@ -995,16 +989,10 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/renderer.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/node.ts",
@ -1012,14 +1000,12 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/renderer.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -1029,7 +1015,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -1038,7 +1023,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -1048,7 +1032,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/container.ts",
"packages/core/src/render3/interfaces/view.ts",
@ -1056,20 +1039,17 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/interfaces/type_checks.ts",
"packages/core/src/render3/interfaces/renderer.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/assert.ts",
"packages/core/src/render3/interfaces/injector.ts",
@ -1078,7 +1058,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/assert.ts",
"packages/core/src/render3/interfaces/node.ts",
@ -1086,14 +1065,12 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/interfaces/node.ts",
"packages/core/src/render3/interfaces/renderer.ts"
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/util/view_utils.ts",
"packages/core/src/render3/interfaces/context.ts",
@ -1101,7 +1078,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/util/view_utils.ts",
"packages/core/src/render3/interfaces/node.ts",
@ -1109,7 +1085,6 @@
],
[
"packages/core/src/render/api.ts",
"packages/core/src/render3/view_engine_compatibility.ts",
"packages/core/src/render3/state.ts",
"packages/core/src/render3/util/view_utils.ts",
"packages/core/src/render3/interfaces/renderer.ts"

View File

@ -8,7 +8,11 @@
import {InjectionToken} from '../di/injection_token';
import {ViewEncapsulation} from '../metadata/view';
import {injectRenderer2 as render3InjectRenderer2} from '../render3/view_engine_compatibility';
import {isProceduralRenderer} from '../render3/interfaces/renderer';
import {isLView} from '../render3/interfaces/type_checks';
import {LView, RENDERER} from '../render3/interfaces/view';
import {getCurrentTNode, getLView} from '../render3/state';
import {getComponentLViewByIndex} from '../render3/util/view_utils';
import {noop} from '../util/noop';
@ -286,6 +290,25 @@ export abstract class Renderer2 {
}
export const SWITCH_RENDERER2_FACTORY__POST_R3__ = render3InjectRenderer2;
export const SWITCH_RENDERER2_FACTORY__POST_R3__ = injectRenderer2;
const SWITCH_RENDERER2_FACTORY__PRE_R3__ = noop;
const SWITCH_RENDERER2_FACTORY: typeof render3InjectRenderer2 = SWITCH_RENDERER2_FACTORY__PRE_R3__;
const SWITCH_RENDERER2_FACTORY: typeof injectRenderer2 = SWITCH_RENDERER2_FACTORY__PRE_R3__;
/** Returns a Renderer2 (or throws when application was bootstrapped with Renderer3) */
function getOrCreateRenderer2(lView: LView): Renderer2 {
const renderer = lView[RENDERER];
if (ngDevMode && !isProceduralRenderer(renderer)) {
throw new Error('Cannot inject Renderer2 when the application uses Renderer3!');
}
return renderer as Renderer2;
}
/** Injects a Renderer2 for the current component. */
export function injectRenderer2(): Renderer2 {
// We need the Renderer to be based on the component that it's being injected into, however since
// DI happens before we've entered its view, `getLView` will return the parent view instead.
const lView = getLView();
const tNode = getCurrentTNode()!;
const nodeAtIndex = getComponentLViewByIndex(tNode.index, lView);
return getOrCreateRenderer2(isLView(nodeAtIndex) ? nodeAtIndex : lView);
}

View File

@ -1,36 +0,0 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Renderer2} from '../render/api';
import {isProceduralRenderer} from './interfaces/renderer';
import {isLView} from './interfaces/type_checks';
import {LView, RENDERER} from './interfaces/view';
import {getCurrentTNode, getLView} from './state';
import {getComponentLViewByIndex} from './util/view_utils';
/** Returns a Renderer2 (or throws when application was bootstrapped with Renderer3) */
function getOrCreateRenderer2(view: LView): Renderer2 {
const renderer = view[RENDERER];
if (isProceduralRenderer(renderer)) {
return renderer as Renderer2;
} else {
throw new Error('Cannot inject Renderer2 when the application uses Renderer3!');
}
}
/** Injects a Renderer2 for the current component. */
export function injectRenderer2(): Renderer2 {
// We need the Renderer to be based on the component that it's being injected into, however since
// DI happens before we've entered its view, `getLView` will return the parent view instead.
const lView = getLView();
const tNode = getCurrentTNode()!;
const nodeAtIndex = getComponentLViewByIndex(tNode.index, lView);
return getOrCreateRenderer2(isLView(nodeAtIndex) ? nodeAtIndex : lView);
}