Skip to content

Commit d6da631

Browse files
refactor(compiler): Adds ingest and flags for defer details (#58833)
This adds TDeferDetailsFlags to indicate the presence of hydration triggers, and any future flags we add to defer. PR Close #58833
1 parent 22a6ff7 commit d6da631

File tree

9 files changed

+59
-2
lines changed

9 files changed

+59
-2
lines changed

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_deferred/defer_hydrate_order_template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function MyApp_Template(rf, ctx) {
22
if (rf & 1) {
33
$r3$.ɵɵtemplate(0, MyApp_Defer_0_Template, 1, 0)(1, MyApp_DeferPlaceholder_1_Template, 2, 0);
4-
$r3$.ɵɵdefer(2, 0, null, null, 1);
4+
$r3$.ɵɵdefer(2, 0, null, null, 1, null, null, null, null, 1);
55
$r3$.ɵɵdeferHydrateOnTimer(1337);
66
$r3$.ɵɵdeferPrefetchOnViewport(0, -1);
77
}

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_deferred/defer_with_hydrate_triggers_template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function MyApp_Template(rf, ctx) {
22
if (rf & 1) {
33
$r3$.ɵɵtext(0);
44
$r3$.ɵɵtemplate(1, MyApp_Defer_1_Template, 1, 1);
5-
$r3$.ɵɵdefer(2, 1);
5+
$r3$.ɵɵdefer(2, 1, null, null, null, null, null, null, null, 1);
66
$r3$.ɵɵdeferHydrateOnIdle();
77
$r3$.ɵɵdeferHydrateOnImmediate();
88
$r3$.ɵɵdeferHydrateOnTimer(1337);

packages/compiler/src/template/pipeline/ir/src/enums.ts

+9
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,12 @@ export const enum DeferOpModifierKind {
601601
PREFETCH = 'prefetch',
602602
HYDRATE = 'hydrate',
603603
}
604+
605+
export const enum TDeferDetailsFlags {
606+
Default = 0,
607+
608+
/**
609+
* Whether or not the defer block has hydrate triggers.
610+
*/
611+
HasHydrateTriggers = 1 << 0,
612+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
I18nParamValueFlags,
1919
Namespace,
2020
OpKind,
21+
TDeferDetailsFlags,
2122
TemplateKind,
2223
} from '../enums';
2324
import {SlotHandle} from '../handle';
@@ -941,6 +942,8 @@ export interface DeferOp extends Op<CreateOp>, ConsumesSlotOpTrait {
941942
*/
942943
resolverFn: o.Expression | null;
943944

945+
flags: TDeferDetailsFlags | null;
946+
944947
sourceSpan: ParseSourceSpan;
945948
}
946949

@@ -971,6 +974,7 @@ export function createDeferOp(
971974
errorSlot: null,
972975
ownResolverFn,
973976
resolverFn,
977+
flags: null,
974978
sourceSpan,
975979
...NEW_OP,
976980
...TRAIT_CONSUMES_SLOT,

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

+21
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ function ingestDeferBlock(unit: ViewCompilationUnit, deferBlock: t.DeferredBlock
665665
deferOp.placeholderMinimumTime = deferBlock.placeholder?.minimumTime ?? null;
666666
deferOp.loadingMinimumTime = deferBlock.loading?.minimumTime ?? null;
667667
deferOp.loadingAfterTime = deferBlock.loading?.afterTime ?? null;
668+
deferOp.flags = hasHydrateTriggers(deferBlock.hydrateTriggers);
668669
unit.create.push(deferOp);
669670

670671
// Configure all defer `on` conditions.
@@ -721,6 +722,26 @@ function ingestDeferBlock(unit: ViewCompilationUnit, deferBlock: t.DeferredBlock
721722
unit.update.push(deferWhenOps);
722723
}
723724

725+
function hasHydrateTriggers(
726+
triggers: Readonly<t.DeferredBlockTriggers>,
727+
): ir.TDeferDetailsFlags | null {
728+
if (
729+
!!(
730+
triggers.hover ||
731+
triggers.idle ||
732+
triggers.immediate ||
733+
triggers.interaction ||
734+
triggers.never ||
735+
triggers.timer ||
736+
triggers.viewport ||
737+
triggers.when
738+
)
739+
) {
740+
return ir.TDeferDetailsFlags.HasHydrateTriggers;
741+
}
742+
return null;
743+
}
744+
724745
function ingestDeferTriggers(
725746
modifier: ir.DeferOpModifierKind,
726747
triggers: Readonly<t.DeferredBlockTriggers>,

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

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ export function defer(
243243
placeholderConfig: o.Expression | null,
244244
enableTimerScheduling: boolean,
245245
sourceSpan: ParseSourceSpan | null,
246+
flags: ir.TDeferDetailsFlags | null,
246247
): ir.CreateOp {
247248
const args: Array<o.Expression> = [
248249
o.literal(selfSlot),
@@ -254,6 +255,7 @@ export function defer(
254255
loadingConfig ?? o.literal(null),
255256
placeholderConfig ?? o.literal(null),
256257
enableTimerScheduling ? o.importExpr(Identifiers.deferEnableTimerScheduling) : o.literal(null),
258+
o.literal(flags),
257259
];
258260

259261
let expr: o.Expression;

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

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ function reifyCreateOperations(unit: CompilationUnit, ops: ir.OpList<ir.CreateOp
263263
op.placeholderConfig,
264264
timerScheduling,
265265
op.sourceSpan,
266+
op.flags,
266267
),
267268
);
268269
break;

packages/core/src/defer/instructions.ts

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
TDeferBlockDetails,
4141
TriggerType,
4242
SSR_UNIQUE_ID,
43+
TDeferDetailsFlags,
4344
} from './interfaces';
4445
import {onTimer} from './timer_scheduler';
4546
import {
@@ -82,6 +83,9 @@ import {
8283
* placeholder block.
8384
* @param enableTimerScheduling Function that enables timer-related scheduling if `after`
8485
* or `minimum` parameters are setup on the `@loading` or `@placeholder` blocks.
86+
* @param flags A set of flags to define a particular behavior (e.g. to indicate that
87+
* hydrate triggers are present and regular triggers should be deactivated
88+
* in certain scenarios).
8589
*
8690
* @codeGenApi
8791
*/
@@ -95,6 +99,7 @@ export function ɵɵdefer(
9599
loadingConfigIndex?: number | null,
96100
placeholderConfigIndex?: number | null,
97101
enableTimerScheduling?: typeof ɵɵdeferEnableTimerScheduling,
102+
flags?: TDeferDetailsFlags | null,
98103
) {
99104
const lView = getLView();
100105
const tView = getTView();
@@ -118,6 +123,7 @@ export function ɵɵdefer(
118123
providers: null,
119124
hydrateTriggers: null,
120125
prefetchTriggers: null,
126+
flags: flags ?? TDeferDetailsFlags.Default,
121127
};
122128
enableTimerScheduling?.(tView, tDetails, placeholderConfigIndex, loadingConfigIndex);
123129
setTDeferBlockDetails(tView, adjustedIndex, tDetails);

packages/core/src/defer/interfaces.ts

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ export interface TDeferBlockDetails {
144144
* List of prefetch triggers for a given block
145145
*/
146146
prefetchTriggers: Set<DeferBlockTrigger> | null;
147+
148+
/**
149+
* Flags
150+
*/
151+
flags: TDeferDetailsFlags;
152+
}
153+
154+
export const enum TDeferDetailsFlags {
155+
Default = 0,
156+
157+
/**
158+
* Whether or not the defer block has hydrate triggers.
159+
*/
160+
HasHydrateTriggers = 1 << 0,
147161
}
148162

149163
/**

0 commit comments

Comments
 (0)