66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { AfterRenderPhase , AfterRenderRef } from './api' ;
10- import { NgZone } from '../../zone' ;
11- import { inject } from '../../di/injector_compatibility' ;
12- import { ɵɵdefineInjectable } from '../../di/interface/defs' ;
13- import { ErrorHandler } from '../../error_handler' ;
9+ import { TracingAction , TracingService , TracingSnapshot } from '../../application/tracing' ;
1410import {
1511 ChangeDetectionScheduler ,
1612 NotificationSource ,
1713} from '../../change_detection/scheduling/zoneless_scheduling' ;
14+ import { inject } from '../../di/injector_compatibility' ;
15+ import { ɵɵdefineInjectable } from '../../di/interface/defs' ;
16+ import { ErrorHandler } from '../../error_handler' ;
1817import { type DestroyRef } from '../../linker/destroy_ref' ;
19- import { TracingAction , TracingService , TracingSnapshot } from '../../application/tracing' ;
18+ import { NgZone } from '../../zone' ;
19+ import { AFTER_RENDER_SEQUENCES_TO_ADD , FLAGS , LView , LViewFlags } from '../interfaces/view' ;
2020import { profiler } from '../profiler' ;
2121import { ProfilerEvent } from '../profiler_types' ;
22+ import { markAncestorsForTraversal } from '../util/view_utils' ;
23+ import { AfterRenderPhase , AfterRenderRef } from './api' ;
2224
2325export class AfterRenderManager {
2426 impl : AfterRenderImpl | null = null ;
@@ -111,7 +113,7 @@ export class AfterRenderImpl {
111113 this . sequences . add ( sequence ) ;
112114 }
113115 if ( this . deferredRegistrations . size > 0 ) {
114- this . scheduler . notify ( NotificationSource . DeferredRenderHook ) ;
116+ this . scheduler . notify ( NotificationSource . RenderHook ) ;
115117 }
116118 this . deferredRegistrations . clear ( ) ;
117119
@@ -121,16 +123,28 @@ export class AfterRenderImpl {
121123 }
122124
123125 register ( sequence : AfterRenderSequence ) : void {
124- if ( ! this . executing ) {
125- this . sequences . add ( sequence ) ;
126- // Trigger an `ApplicationRef.tick()` if one is not already pending/running, because we have a
127- // new render hook that needs to run.
128- this . scheduler . notify ( NotificationSource . RenderHook ) ;
126+ const { view} = sequence ;
127+ if ( view !== undefined ) {
128+ // Delay adding it to the manager, add it to the view instead.
129+ ( view [ AFTER_RENDER_SEQUENCES_TO_ADD ] ??= [ ] ) . push ( sequence ) ;
130+
131+ // Mark the view for traversal to ensure we eventually schedule the afterNextRender.
132+ markAncestorsForTraversal ( view ) ;
133+ view [ FLAGS ] |= LViewFlags . HasChildViewsToRefresh ;
134+ } else if ( ! this . executing ) {
135+ this . addSequence ( sequence ) ;
129136 } else {
130137 this . deferredRegistrations . add ( sequence ) ;
131138 }
132139 }
133140
141+ addSequence ( sequence : AfterRenderSequence ) : void {
142+ this . sequences . add ( sequence ) ;
143+ // Trigger an `ApplicationRef.tick()` if one is not already pending/running, because we have a
144+ // new render hook that needs to run.
145+ this . scheduler . notify ( NotificationSource . RenderHook ) ;
146+ }
147+
134148 unregister ( sequence : AfterRenderSequence ) : void {
135149 if ( this . executing && this . sequences . has ( sequence ) ) {
136150 // We can't remove an `AfterRenderSequence` in the middle of iteration.
@@ -185,6 +199,7 @@ export class AfterRenderSequence implements AfterRenderRef {
185199 constructor (
186200 readonly impl : AfterRenderImpl ,
187201 readonly hooks : AfterRenderHooks ,
202+ readonly view : LView | undefined ,
188203 public once : boolean ,
189204 destroyRef : DestroyRef | null ,
190205 public snapshot : TracingSnapshot | null = null ,
@@ -207,5 +222,9 @@ export class AfterRenderSequence implements AfterRenderRef {
207222 destroy ( ) : void {
208223 this . impl . unregister ( this ) ;
209224 this . unregisterOnDestroy ?.( ) ;
225+ const scheduled = this . view ?. [ AFTER_RENDER_SEQUENCES_TO_ADD ] ;
226+ if ( scheduled ) {
227+ this . view [ AFTER_RENDER_SEQUENCES_TO_ADD ] = scheduled . filter ( ( s ) => s !== this ) ;
228+ }
210229 }
211230}
0 commit comments