Skip to content

Commit 0c8aaf0

Browse files
dylhunnthePunderWoman
authored andcommitted
refactor(compiler): Support externally provided defer deps fns (#54043)
In #53591, Andrew added local compliation support for defer blocks. However, this requires the ability to emit pre-generated static defer deps functions. We now also support that feature in Template Pipeline. PR Close #54043
1 parent a4f024f commit 0c8aaf0

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

packages/compiler/src/render3/view/compiler.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,19 @@ export function compileComponentFromMetadata(
214214
const templateTypeName = meta.name;
215215
const templateName = templateTypeName ? `${templateTypeName}_Template` : null;
216216

217+
218+
let allDeferrableDepsFn: o.ReadVarExpr|null = null;
219+
if (meta.deferBlocks.size > 0 && meta.deferrableTypes.size > 0 &&
220+
meta.deferBlockDepsEmitMode === DeferBlockDepsEmitMode.PerComponent) {
221+
const fnName = `${templateTypeName}_DeferFn`;
222+
allDeferrableDepsFn = createDeferredDepsFunction(constantPool, fnName, meta.deferrableTypes);
223+
}
224+
217225
// Template compilation is currently conditional as we're in the process of rewriting it.
218226
if (!USE_TEMPLATE_PIPELINE) {
219227
// This is the main path currently used in compilation, which compiles the template with the
220228
// legacy `TemplateDefinitionBuilder`.
221229

222-
let allDeferrableDepsFn: o.ReadVarExpr|null = null;
223-
if (meta.deferBlocks.size > 0 && meta.deferrableTypes.size > 0 &&
224-
meta.deferBlockDepsEmitMode === DeferBlockDepsEmitMode.PerComponent) {
225-
const fnName = `${templateTypeName}_DeferFn`;
226-
allDeferrableDepsFn = createDeferredDepsFunction(constantPool, fnName, meta.deferrableTypes);
227-
}
228-
229230
const template = meta.template;
230231
const templateBuilder = new TemplateDefinitionBuilder(
231232
constantPool, BindingScope.createRootScope(), 0, templateTypeName, null, null, templateName,
@@ -272,7 +273,7 @@ export function compileComponentFromMetadata(
272273
// ingested into IR:
273274
const tpl = ingestComponent(
274275
meta.name, meta.template.nodes, constantPool, meta.relativeContextFilePath,
275-
meta.i18nUseExternalIds, meta.deferBlocks);
276+
meta.i18nUseExternalIds, meta.deferBlocks, allDeferrableDepsFn);
276277

277278
// Then the IR is transformed to prepare it for cod egeneration.
278279
transform(tpl, CompilationJobKind.Tmpl);

packages/compiler/src/template/pipeline/ir/src/ops/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ export interface DeferOp extends Op<CreateOp>, ConsumesSlotOpTrait {
798798

799799
export function createDeferOp(
800800
xref: XrefId, main: XrefId, mainSlot: SlotHandle, metadata: R3DeferBlockMetadata,
801-
sourceSpan: ParseSourceSpan): DeferOp {
801+
resolverFn: o.Expression|null, sourceSpan: ParseSourceSpan): DeferOp {
802802
return {
803803
kind: OpKind.Defer,
804804
xref,
@@ -817,7 +817,7 @@ export function createDeferOp(
817817
errorView: null,
818818
errorSlot: null,
819819
metadata,
820-
resolverFn: null,
820+
resolverFn,
821821
sourceSpan,
822822
...NEW_OP,
823823
...TRAIT_CONSUMES_SLOT,

packages/compiler/src/template/pipeline/src/compilation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export class ComponentCompilationJob extends CompilationJob {
6767
constructor(
6868
componentName: string, pool: ConstantPool, compatibility: ir.CompatibilityMode,
6969
readonly relativeContextFilePath: string, readonly i18nUseExternalIds: boolean,
70-
readonly deferBlocksMeta: Map<t.DeferredBlock, R3DeferBlockMetadata>) {
70+
readonly deferBlocksMeta: Map<t.DeferredBlock, R3DeferBlockMetadata>,
71+
readonly allDeferrableDepsFn: o.ReadVarExpr|null) {
7172
super(componentName, pool, compatibility);
7273
this.root = new ViewCompilationUnit(this, this.allocateXrefId(), null);
7374
this.views.set(this.root.xref, this.root);

packages/compiler/src/template/pipeline/src/ingest.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ const NG_TEMPLATE_TAG_NAME = 'ng-template';
3939
export function ingestComponent(
4040
componentName: string, template: t.Node[], constantPool: ConstantPool,
4141
relativeContextFilePath: string, i18nUseExternalIds: boolean,
42-
deferBlocksMeta: Map<t.DeferredBlock, R3DeferBlockMetadata>): ComponentCompilationJob {
42+
deferBlocksMeta: Map<t.DeferredBlock, R3DeferBlockMetadata>,
43+
allDeferrableDepsFn: o.ReadVarExpr|null): ComponentCompilationJob {
4344
const job = new ComponentCompilationJob(
4445
componentName, constantPool, compatibilityMode, relativeContextFilePath, i18nUseExternalIds,
45-
deferBlocksMeta);
46+
deferBlocksMeta, allDeferrableDepsFn);
4647
ingestNodes(job.root, template);
4748
return job;
4849
}
@@ -462,8 +463,9 @@ function ingestDeferBlock(unit: ViewCompilationUnit, deferBlock: t.DeferredBlock
462463

463464
// Create the main defer op, and ops for all secondary views.
464465
const deferXref = unit.job.allocateXrefId();
465-
const deferOp =
466-
ir.createDeferOp(deferXref, main.xref, main.handle, blockMeta, deferBlock.sourceSpan);
466+
const deferOp = ir.createDeferOp(
467+
deferXref, main.xref, main.handle, blockMeta, unit.job.allDeferrableDepsFn,
468+
deferBlock.sourceSpan);
467469
deferOp.placeholderView = placeholder?.xref ?? null;
468470
deferOp.placeholderSlot = placeholder?.handle ?? null;
469471
deferOp.loadingSlot = loading?.handle ?? null;

packages/compiler/src/template/pipeline/src/phases/create_defer_deps_fns.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export function createDeferDepsFns(job: ComponentCompilationJob): void {
2020
if (op.metadata.deps.length === 0) {
2121
continue;
2222
}
23+
if (op.resolverFn !== null) {
24+
continue;
25+
}
2326
const dependencies: o.Expression[] = [];
2427
for (const dep of op.metadata.deps) {
2528
if (dep.isDeferrable) {

0 commit comments

Comments
 (0)