Skip to content

Commit

Permalink
refactor(compiler): Adds ingest and flags for defer details (#58833)
Browse files Browse the repository at this point in the history
This adds TDeferDetailsFlags to indicate the presence of hydration triggers, and any future flags we add to defer.

PR Close #58833
  • Loading branch information
thePunderWoman authored and pkozlowski-opensource committed Nov 27, 2024
1 parent 22a6ff7 commit d6da631
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function MyApp_Template(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵtemplate(0, MyApp_Defer_0_Template, 1, 0)(1, MyApp_DeferPlaceholder_1_Template, 2, 0);
$r3$.ɵɵdefer(2, 0, null, null, 1);
$r3$.ɵɵdefer(2, 0, null, null, 1, null, null, null, null, 1);
$r3$.ɵɵdeferHydrateOnTimer(1337);
$r3$.ɵɵdeferPrefetchOnViewport(0, -1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function MyApp_Template(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵtext(0);
$r3$.ɵɵtemplate(1, MyApp_Defer_1_Template, 1, 1);
$r3$.ɵɵdefer(2, 1);
$r3$.ɵɵdefer(2, 1, null, null, null, null, null, null, null, 1);
$r3$.ɵɵdeferHydrateOnIdle();
$r3$.ɵɵdeferHydrateOnImmediate();
$r3$.ɵɵdeferHydrateOnTimer(1337);
Expand Down
9 changes: 9 additions & 0 deletions packages/compiler/src/template/pipeline/ir/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,12 @@ export const enum DeferOpModifierKind {
PREFETCH = 'prefetch',
HYDRATE = 'hydrate',
}

export const enum TDeferDetailsFlags {
Default = 0,

/**
* Whether or not the defer block has hydrate triggers.
*/
HasHydrateTriggers = 1 << 0,
}
4 changes: 4 additions & 0 deletions packages/compiler/src/template/pipeline/ir/src/ops/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
I18nParamValueFlags,
Namespace,
OpKind,
TDeferDetailsFlags,
TemplateKind,
} from '../enums';
import {SlotHandle} from '../handle';
Expand Down Expand Up @@ -941,6 +942,8 @@ export interface DeferOp extends Op<CreateOp>, ConsumesSlotOpTrait {
*/
resolverFn: o.Expression | null;

flags: TDeferDetailsFlags | null;

sourceSpan: ParseSourceSpan;
}

Expand Down Expand Up @@ -971,6 +974,7 @@ export function createDeferOp(
errorSlot: null,
ownResolverFn,
resolverFn,
flags: null,
sourceSpan,
...NEW_OP,
...TRAIT_CONSUMES_SLOT,
Expand Down
21 changes: 21 additions & 0 deletions packages/compiler/src/template/pipeline/src/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ function ingestDeferBlock(unit: ViewCompilationUnit, deferBlock: t.DeferredBlock
deferOp.placeholderMinimumTime = deferBlock.placeholder?.minimumTime ?? null;
deferOp.loadingMinimumTime = deferBlock.loading?.minimumTime ?? null;
deferOp.loadingAfterTime = deferBlock.loading?.afterTime ?? null;
deferOp.flags = hasHydrateTriggers(deferBlock.hydrateTriggers);
unit.create.push(deferOp);

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

function hasHydrateTriggers(
triggers: Readonly<t.DeferredBlockTriggers>,
): ir.TDeferDetailsFlags | null {
if (
!!(
triggers.hover ||
triggers.idle ||
triggers.immediate ||
triggers.interaction ||
triggers.never ||
triggers.timer ||
triggers.viewport ||
triggers.when
)
) {
return ir.TDeferDetailsFlags.HasHydrateTriggers;
}
return null;
}

function ingestDeferTriggers(
modifier: ir.DeferOpModifierKind,
triggers: Readonly<t.DeferredBlockTriggers>,
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/template/pipeline/src/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export function defer(
placeholderConfig: o.Expression | null,
enableTimerScheduling: boolean,
sourceSpan: ParseSourceSpan | null,
flags: ir.TDeferDetailsFlags | null,
): ir.CreateOp {
const args: Array<o.Expression> = [
o.literal(selfSlot),
Expand All @@ -254,6 +255,7 @@ export function defer(
loadingConfig ?? o.literal(null),
placeholderConfig ?? o.literal(null),
enableTimerScheduling ? o.importExpr(Identifiers.deferEnableTimerScheduling) : o.literal(null),
o.literal(flags),
];

let expr: o.Expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ function reifyCreateOperations(unit: CompilationUnit, ops: ir.OpList<ir.CreateOp
op.placeholderConfig,
timerScheduling,
op.sourceSpan,
op.flags,
),
);
break;
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/defer/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
TDeferBlockDetails,
TriggerType,
SSR_UNIQUE_ID,
TDeferDetailsFlags,
} from './interfaces';
import {onTimer} from './timer_scheduler';
import {
Expand Down Expand Up @@ -82,6 +83,9 @@ import {
* placeholder block.
* @param enableTimerScheduling Function that enables timer-related scheduling if `after`
* or `minimum` parameters are setup on the `@loading` or `@placeholder` blocks.
* @param flags A set of flags to define a particular behavior (e.g. to indicate that
* hydrate triggers are present and regular triggers should be deactivated
* in certain scenarios).
*
* @codeGenApi
*/
Expand All @@ -95,6 +99,7 @@ export function ɵɵdefer(
loadingConfigIndex?: number | null,
placeholderConfigIndex?: number | null,
enableTimerScheduling?: typeof ɵɵdeferEnableTimerScheduling,
flags?: TDeferDetailsFlags | null,
) {
const lView = getLView();
const tView = getTView();
Expand All @@ -118,6 +123,7 @@ export function ɵɵdefer(
providers: null,
hydrateTriggers: null,
prefetchTriggers: null,
flags: flags ?? TDeferDetailsFlags.Default,
};
enableTimerScheduling?.(tView, tDetails, placeholderConfigIndex, loadingConfigIndex);
setTDeferBlockDetails(tView, adjustedIndex, tDetails);
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/defer/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ export interface TDeferBlockDetails {
* List of prefetch triggers for a given block
*/
prefetchTriggers: Set<DeferBlockTrigger> | null;

/**
* Flags
*/
flags: TDeferDetailsFlags;
}

export const enum TDeferDetailsFlags {
Default = 0,

/**
* Whether or not the defer block has hydrate triggers.
*/
HasHydrateTriggers = 1 << 0,
}

/**
Expand Down

0 comments on commit d6da631

Please sign in to comment.