@@ -45,6 +45,38 @@ function shouldTriggerDeferBlock(injector: Injector): boolean {
4545 return isPlatformBrowser ( injector ) ;
4646}
4747
48+ /**
49+ * Reference to the timer-based scheduler implementation of defer block state
50+ * rendering method. It's used to make timer-based scheduling tree-shakable.
51+ * If `minimum` or `after` parameters are used, compiler generates an extra
52+ * argument for the `ɵɵdefer` instruction, which references a timer-based
53+ * implementation.
54+ */
55+ let applyDeferBlockStateWithSchedulingImpl : ( typeof applyDeferBlockState ) | null = null ;
56+
57+ /**
58+ * Enables timer-related scheduling if `after` or `minimum` parameters are setup
59+ * on the `@loading` or `@placeholder` blocks.
60+ */
61+ export function ɵɵdeferEnableTimerScheduling (
62+ tView : TView , tDetails : TDeferBlockDetails , placeholderConfigIndex ?: number | null ,
63+ loadingConfigIndex ?: number | null ) {
64+ const tViewConsts = tView . consts ;
65+ if ( placeholderConfigIndex != null ) {
66+ tDetails . placeholderBlockConfig =
67+ getConstant < DeferredPlaceholderBlockConfig > ( tViewConsts , placeholderConfigIndex ) ;
68+ }
69+ if ( loadingConfigIndex != null ) {
70+ tDetails . loadingBlockConfig =
71+ getConstant < DeferredLoadingBlockConfig > ( tViewConsts , loadingConfigIndex ) ;
72+ }
73+
74+ // Enable implementation that supports timer-based scheduling.
75+ if ( applyDeferBlockStateWithSchedulingImpl === null ) {
76+ applyDeferBlockStateWithSchedulingImpl = applyDeferBlockStateWithScheduling ;
77+ }
78+ }
79+
4880/**
4981 * Creates runtime data structures for defer blocks.
5082 *
@@ -58,39 +90,37 @@ function shouldTriggerDeferBlock(injector: Injector): boolean {
5890 * block.
5991 * @param placeholderConfigIndex Index in the constants array of the configuration of the
6092 * placeholder block.
93+ * @param enableTimerScheduling Function that enables timer-related scheduling if `after`
94+ * or `minimum` parameters are setup on the `@loading` or `@placeholder` blocks.
6195 *
6296 * @codeGenApi
6397 */
6498export function ɵɵdefer (
6599 index : number , primaryTmplIndex : number , dependencyResolverFn ?: DependencyResolverFn | null ,
66100 loadingTmplIndex ?: number | null , placeholderTmplIndex ?: number | null ,
67101 errorTmplIndex ?: number | null , loadingConfigIndex ?: number | null ,
68- placeholderConfigIndex ?: number | null ) {
102+ placeholderConfigIndex ?: number | null ,
103+ enableTimerScheduling ?: typeof ɵɵdeferEnableTimerScheduling ) {
69104 const lView = getLView ( ) ;
70105 const tView = getTView ( ) ;
71- const tViewConsts = tView . consts ;
72106 const adjustedIndex = index + HEADER_OFFSET ;
73107
74108 ɵɵtemplate ( index , null , 0 , 0 ) ;
75109
76110 if ( tView . firstCreatePass ) {
77- const deferBlockConfig : TDeferBlockDetails = {
111+ const tDetails : TDeferBlockDetails = {
78112 primaryTmplIndex,
79113 loadingTmplIndex : loadingTmplIndex ?? null ,
80114 placeholderTmplIndex : placeholderTmplIndex ?? null ,
81115 errorTmplIndex : errorTmplIndex ?? null ,
82- placeholderBlockConfig : placeholderConfigIndex != null ?
83- getConstant < DeferredPlaceholderBlockConfig > ( tViewConsts , placeholderConfigIndex ) :
84- null ,
85- loadingBlockConfig : loadingConfigIndex != null ?
86- getConstant < DeferredLoadingBlockConfig > ( tViewConsts , loadingConfigIndex ) :
87- null ,
116+ placeholderBlockConfig : null ,
117+ loadingBlockConfig : null ,
88118 dependencyResolverFn : dependencyResolverFn ?? null ,
89119 loadingState : DeferDependenciesLoadingState . NOT_STARTED ,
90120 loadingPromise : null ,
91121 } ;
92-
93- setTDeferBlockDetails ( tView , adjustedIndex , deferBlockConfig ) ;
122+ enableTimerScheduling ?. ( tView , tDetails , placeholderConfigIndex , loadingConfigIndex ) ;
123+ setTDeferBlockDetails ( tView , adjustedIndex , tDetails ) ;
94124 }
95125
96126 const tNode = getCurrentTNode ( ) ! ;
@@ -657,16 +687,68 @@ export function renderDeferBlockState(
657687 ngDevMode && assertTNodeForLView ( tNode , hostLView ) ;
658688
659689 const lDetails = getLDeferBlockDetails ( hostLView , tNode ) ;
660- const tDetails = getTDeferBlockDetails ( hostTView , tNode ) ;
661690
662691 ngDevMode && assertDefined ( lDetails , 'Expected a defer block state defined' ) ;
663692
664- const now = Date . now ( ) ;
665693 const currentState = lDetails [ DEFER_BLOCK_STATE ] ;
666694
667- if ( ! isValidStateChange ( currentState , newState ) ||
668- ! isValidStateChange ( lDetails [ NEXT_DEFER_BLOCK_STATE ] ?? - 1 , newState ) )
669- return ;
695+ if ( isValidStateChange ( currentState , newState ) &&
696+ isValidStateChange ( lDetails [ NEXT_DEFER_BLOCK_STATE ] ?? - 1 , newState ) ) {
697+ const tDetails = getTDeferBlockDetails ( hostTView , tNode ) ;
698+ const needsScheduling = getLoadingBlockAfter ( tDetails ) !== null ||
699+ getMinimumDurationForState ( tDetails , DeferBlockState . Loading ) !== null ||
700+ getMinimumDurationForState ( tDetails , DeferBlockState . Placeholder ) ;
701+
702+ if ( ngDevMode && needsScheduling ) {
703+ assertDefined (
704+ applyDeferBlockStateWithSchedulingImpl , 'Expected scheduling function to be defined' ) ;
705+ }
706+
707+ const applyStateFn =
708+ needsScheduling ? applyDeferBlockStateWithSchedulingImpl ! : applyDeferBlockState ;
709+ applyStateFn ( newState , lDetails , lContainer , tNode , hostLView ) ;
710+ }
711+ }
712+
713+ /**
714+ * Applies changes to the DOM to reflect a given state.
715+ */
716+ function applyDeferBlockState (
717+ newState : DeferBlockState , lDetails : LDeferBlockDetails , lContainer : LContainer , tNode : TNode ,
718+ hostLView : LView < unknown > ) {
719+ const stateTmplIndex = getTemplateIndexForState ( newState , hostLView , tNode ) ;
720+
721+ if ( stateTmplIndex !== null ) {
722+ lDetails [ DEFER_BLOCK_STATE ] = newState ;
723+ const hostTView = hostLView [ TVIEW ] ;
724+ const adjustedIndex = stateTmplIndex + HEADER_OFFSET ;
725+ const tNode = getTNode ( hostTView , adjustedIndex ) as TContainerNode ;
726+
727+ // There is only 1 view that can be present in an LContainer that
728+ // represents a defer block, so always refer to the first one.
729+ const viewIndex = 0 ;
730+
731+ removeLViewFromLContainer ( lContainer , viewIndex ) ;
732+ const dehydratedView = findMatchingDehydratedView ( lContainer , tNode . tView ! . ssrId ) ;
733+ const embeddedLView = createAndRenderEmbeddedLView ( hostLView , tNode , null , { dehydratedView} ) ;
734+ addLViewToLContainer (
735+ lContainer , embeddedLView , viewIndex , shouldAddViewToDom ( tNode , dehydratedView ) ) ;
736+ markViewDirty ( embeddedLView ) ;
737+ }
738+ }
739+
740+ /**
741+ * Extends the `applyDeferBlockState` with timer-based scheduling.
742+ * This function becomes available on a page if there are defer blocks
743+ * that use `after` or `minimum` parameters in the `@loading` or
744+ * `@placeholder` blocks.
745+ */
746+ function applyDeferBlockStateWithScheduling (
747+ newState : DeferBlockState , lDetails : LDeferBlockDetails , lContainer : LContainer , tNode : TNode ,
748+ hostLView : LView < unknown > ) {
749+ const now = Date . now ( ) ;
750+ const hostTView = hostLView [ TVIEW ] ;
751+ const tDetails = getTDeferBlockDetails ( hostTView , tNode ) ;
670752
671753 if ( lDetails [ STATE_IS_FROZEN_UNTIL ] === null || lDetails [ STATE_IS_FROZEN_UNTIL ] <= now ) {
672754 lDetails [ STATE_IS_FROZEN_UNTIL ] = null ;
@@ -690,7 +772,7 @@ export function renderDeferBlockState(
690772 lDetails [ NEXT_DEFER_BLOCK_STATE ] = null ;
691773 }
692774
693- applyDeferBlockStateToDom ( newState , lDetails , lContainer , hostLView , tNode ) ;
775+ applyDeferBlockState ( newState , lDetails , lContainer , tNode , hostLView ) ;
694776
695777 const duration = getMinimumDurationForState ( tDetails , newState ) ;
696778 if ( duration !== null ) {
@@ -720,8 +802,6 @@ function scheduleDeferBlockUpdate(
720802 renderDeferBlockState ( nextState , tNode , lContainer ) ;
721803 }
722804 } ;
723- // TODO: this needs refactoring to make `TimerScheduler` that is used inside
724- // of the `scheduleTimerTrigger` function tree-shakable.
725805 return scheduleTimerTrigger ( timeout , callback , hostLView , true ) ;
726806}
727807
@@ -739,33 +819,6 @@ function isValidStateChange(
739819 return currentState < newState ;
740820}
741821
742- /**
743- * Applies changes to the DOM to reflect a given state.
744- */
745- function applyDeferBlockStateToDom (
746- newState : DeferBlockState , lDetails : LDeferBlockDetails , lContainer : LContainer ,
747- hostLView : LView < unknown > , tNode : TNode ) {
748- const stateTmplIndex = getTemplateIndexForState ( newState , hostLView , tNode ) ;
749-
750- if ( stateTmplIndex !== null ) {
751- lDetails [ DEFER_BLOCK_STATE ] = newState ;
752- const hostTView = hostLView [ TVIEW ] ;
753- const adjustedIndex = stateTmplIndex + HEADER_OFFSET ;
754- const tNode = getTNode ( hostTView , adjustedIndex ) as TContainerNode ;
755-
756- // There is only 1 view that can be present in an LContainer that
757- // represents a defer block, so always refer to the first one.
758- const viewIndex = 0 ;
759-
760- removeLViewFromLContainer ( lContainer , viewIndex ) ;
761- const dehydratedView = findMatchingDehydratedView ( lContainer , tNode . tView ! . ssrId ) ;
762- const embeddedLView = createAndRenderEmbeddedLView ( hostLView , tNode , null , { dehydratedView} ) ;
763- addLViewToLContainer (
764- lContainer , embeddedLView , viewIndex , shouldAddViewToDom ( tNode , dehydratedView ) ) ;
765- markViewDirty ( embeddedLView ) ;
766- }
767- }
768-
769822/**
770823 * Trigger prefetching of dependencies for a defer block.
771824 *
0 commit comments